php实现的rc4加密解密类定义与用法示例

这篇文章主要介绍了php实现的rc4加密解密类定义与用法,结合完整实例形式给出了php rc4加密解密类文件class.rc4crypt.php的定义及相关使用技巧,需要的朋友可以参考下。

本文实例讲述了php实现的rc4加密解密类,分享给大家供大家参考,具体如下:

class.rc4crypt.php文件:

  1. <?php
  2. /*
  3. * By julying.com
  4. */
  5. define('CRYPT_RC4_MODE_INTERNAL', 1);
  6. define('CRYPT_RC4_MODE_MCRYPT', 2);
  7. define('CRYPT_RC4_ENCRYPT', 0);
  8. define('CRYPT_RC4_DECRYPT', 1);
  9. class Crypt_RC4 {
  10. /**
  11. * The Key
  12. *
  13. * @see Crypt_RC4::setKey()
  14. * @var String
  15. * @access private
  16. */
  17. var $key = "\0";
  18. /**
  19. * The Key Stream for encryption
  20. *
  21. * If CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT, this will be equal to the mcrypt object
  22. *
  23. * @see Crypt_RC4::setKey()
  24. * @var Array
  25. * @access private
  26. */
  27. var $encryptStream = false;
  28. /**
  29. * The Key Stream for decryption
  30. *
  31. * If CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT, this will be equal to the mcrypt object
  32. *
  33. * @see Crypt_RC4::setKey()
  34. * @var Array
  35. * @access private
  36. */
  37. var $decryptStream = false;
  38. /**
  39. * The $i and $j indexes for encryption
  40. *
  41. * @see Crypt_RC4::_crypt()
  42. * @var Integer
  43. * @access private
  44. */
  45. var $encryptIndex = 0;
  46. /**
  47. * The $i and $j indexes for decryption
  48. *
  49. * @see Crypt_RC4::_crypt()
  50. * @var Integer
  51. * @access private
  52. */
  53. var $decryptIndex = 0;
  54. /**
  55. * MCrypt parameters
  56. *
  57. * @see Crypt_RC4::setMCrypt()
  58. * @var Array
  59. * @access private
  60. */
  61. var $mcrypt = array('', '');
  62. /**
  63. * The Encryption Algorithm
  64. *
  65. * Only used if CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT. Only possible values are MCRYPT_RC4 or MCRYPT_ARCFOUR.
  66. *
  67. * @see Crypt_RC4::Crypt_RC4()
  68. * @var Integer
  69. * @access private
  70. */
  71. var $mode;
  72. /**
  73. * Default Constructor.
  74. *
  75. * Determines whether or not the mcrypt extension should be used.
  76. *
  77. * @param optional Integer $mode
  78. * @return Crypt_RC4
  79. * @access public
  80. */
  81. var $continuousBuffer ;
  82. function Crypt_RC4()
  83. {
  84. if ( !defined('CRYPT_RC4_MODE') ) {
  85. switch (true) {
  86. case extension_loaded('mcrypt') && (defined('MCRYPT_ARCFOUR') || defined('MCRYPT_RC4')):
  87. // i'd check to see if rc4 was supported, by doing in_array('arcfour', mcrypt_list_algorithms('')),
  88. // but since that can be changed after the object has been created, there doesn't seem to be
  89. // a lot of point...
  90. define('CRYPT_RC4_MODE', CRYPT_RC4_MODE_MCRYPT);
  91. break;
  92. default:
  93. define('CRYPT_RC4_MODE', CRYPT_RC4_MODE_INTERNAL);
  94. }
  95. }
  96. switch ( CRYPT_RC4_MODE ) {
  97. case CRYPT_RC4_MODE_MCRYPT:
  98. switch (true) {
  99. case defined('MCRYPT_ARCFOUR'):
  100. $this->mode = MCRYPT_ARCFOUR;
  101. break;
  102. case defined('MCRYPT_RC4');
  103. $this->mode = MCRYPT_RC4;
  104. }
  105. }
  106. }
  107. /**
  108. * Sets the key.
  109. *
  110. * Keys can be between 1 and 256 bytes long. If they are longer then 256 bytes, the first 256 bytes will
  111. * be used. If no key is explicitly set, it'll be assumed to be a single null byte.
  112. *
  113. * @access public
  114. * @param String $key
  115. */
  116. function setKey($key)
  117. {
  118. $this->key = $key;
  119. if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
  120. return;
  121. }
  122. $keyLength = strlen($key);
  123. $keyStream = array();
  124. for ($i = 0; $i < 256; $i++) {
  125. $keyStream[$i] = $i;
  126. }
  127. $j = 0;
  128. for ($i = 0; $i < 256; $i++) {
  129. $j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
  130. $temp = $keyStream[$i];
  131. $keyStream[$i] = $keyStream[$j];
  132. $keyStream[$j] = $temp;
  133. }
  134. $this->encryptIndex = $this->decryptIndex = array(0, 0);
  135. $this->encryptStream = $this->decryptStream = $keyStream;
  136. }
  137. /**
  138. * Dummy function.
  139. *
  140. * Some protocols, such as WEP, prepend an "initialization vector" to the key, effectively creating a new key [1].
  141. * If you need to use an initialization vector in this manner, feel free to prepend it to the key, yourself, before
  142. * calling setKey().
  143. *
  144. * [1] WEP's initialization vectors (IV's) are used in a somewhat insecure way. Since, in that protocol,
  145. * the IV's are relatively easy to predict, an attack described by
  146. * {@link http://www.drizzle.com/~aboba/IEEE/rc4_ksaproc.pdf Scott Fluhrer, Itsik Mantin, and Adi Shamir}
  147. * can be used to quickly guess at the rest of the key. The following links elaborate:
  148. *
  149. * {@link http://www.rsa.com/rsalabs/node.asp?id=2009 http://www.rsa.com/rsalabs/node.asp?id=2009}
  150. * {@link http://en.wikipedia.org/wiki/Related_key_attack http://en.wikipedia.org/wiki/Related_key_attack}
  151. *
  152. * @param String $iv
  153. * @see Crypt_RC4::setKey()
  154. * @access public
  155. */
  156. function setIV($iv)
  157. {
  158. }
  159. /**
  160. * Sets MCrypt parameters. (optional)
  161. *
  162. * If MCrypt is being used, empty strings will be used, unless otherwise specified.
  163. *
  164. * @link http://php.net/function.mcrypt-module-open#function.mcrypt-module-open
  165. * @access public
  166. * @param optional Integer $algorithm_directory
  167. * @param optional Integer $mode_directory
  168. */
  169. function setMCrypt($algorithm_directory = '', $mode_directory = '')
  170. {
  171. if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
  172. $this->mcrypt = array($algorithm_directory, $mode_directory);
  173. $this->_closeMCrypt();
  174. }
  175. }
  176. /**
  177. * Encrypts a message.
  178. *
  179. * @see Crypt_RC4::_crypt()
  180. * @access public
  181. * @param String $plaintext
  182. */
  183. function encrypt($plaintext)
  184. {
  185. return self::toHex($this->_crypt($plaintext, CRYPT_RC4_ENCRYPT));
  186. }
  187. /**
  188. * Decrypts a message.
  189. *
  190. * $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)).
  191. * Atleast if the continuous buffer is disabled.
  192. *
  193. * @see Crypt_RC4::_crypt()
  194. * @access public
  195. * @param String $ciphertext
  196. */
  197. function decrypt($ciphertext)
  198. {
  199. $ciphertext = self::fromHex($ciphertext);
  200. return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT);
  201. }
  202. /**
  203. * Encrypts or decrypts a message.
  204. *
  205. * @see Crypt_RC4::encrypt()
  206. * @see Crypt_RC4::decrypt()
  207. * @access private
  208. * @param String $text
  209. * @param Integer $mode
  210. */
  211. function _crypt($text, $mode)
  212. {
  213. if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
  214. $keyStream = $mode == CRYPT_RC4_ENCRYPT ? 'encryptStream' : 'decryptStream';
  215. if ($this->$keyStream === false) {
  216. $this->$keyStream = mcrypt_module_open($this->mode, $this->mcrypt[0], MCRYPT_MODE_STREAM, $this->mcrypt[1]);
  217. mcrypt_generic_init($this->$keyStream, $this->key, '');
  218. } else if (!$this->continuousBuffer) {
  219. mcrypt_generic_init($this->$keyStream, $this->key, '');
  220. }
  221. $newText = mcrypt_generic($this->$keyStream, $text);
  222. if (!$this->continuousBuffer) {
  223. mcrypt_generic_deinit($this->$keyStream);
  224. }
  225. return $newText;
  226. }
  227. if ($this->encryptStream === false) {
  228. $this->setKey($this->key);
  229. }
  230. switch ($mode) {
  231. case CRYPT_RC4_ENCRYPT:
  232. $keyStream = $this->encryptStream;
  233. list($i, $j) = $this->encryptIndex;
  234. break;
  235. case CRYPT_RC4_DECRYPT:
  236. $keyStream = $this->decryptStream;
  237. list($i, $j) = $this->decryptIndex;
  238. }
  239. $newText = '';
  240. for ($k = 0; $k < strlen($text); $k++) {
  241. $i = ($i + 1) & 255;
  242. $j = ($j + $keyStream[$i]) & 255;
  243. $temp = $keyStream[$i];
  244. $keyStream[$i] = $keyStream[$j];
  245. $keyStream[$j] = $temp;
  246. $temp = $keyStream[($keyStream[$i] + $keyStream[$j]) & 255];
  247. $newText.= chr(ord($text[$k]) ^ $temp);
  248. }
  249. if ($this->continuousBuffer) {
  250. switch ($mode) {
  251. case CRYPT_RC4_ENCRYPT:
  252. $this->encryptStream = $keyStream;
  253. $this->encryptIndex = array($i, $j);
  254. break;
  255. case CRYPT_RC4_DECRYPT:
  256. $this->decryptStream = $keyStream;
  257. $this->decryptIndex = array($i, $j);
  258. }
  259. }
  260. return $newText;
  261. }
  262. /**
  263. * Treat consecutive "packets" as if they are a continuous buffer.
  264. *
  265. * Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
  266. * will yield different outputs:
  267. *
  268. * <code>
  269. * echo $rc4->encrypt(substr($plaintext, 0, 8));
  270. * echo $rc4->encrypt(substr($plaintext, 8, 8));
  271. * </code>
  272. * <code>
  273. * echo $rc4->encrypt($plaintext);
  274. * </code>
  275. *
  276. * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
  277. * another, as demonstrated with the following:
  278. *
  279. * <code>
  280. * $rc4->encrypt(substr($plaintext, 0, 8));
  281. * echo $rc4->decrypt($des->encrypt(substr($plaintext, 8, 8)));
  282. * </code>
  283. * <code>
  284. * echo $rc4->decrypt($des->encrypt(substr($plaintext, 8, 8)));
  285. * </code>
  286. *
  287. * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
  288. * outputs. The reason is due to the fact that the initialization vector's change after every encryption /
  289. * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
  290. *
  291. * Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
  292. * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
  293. * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
  294. * however, they are also less intuitive and more likely to cause you problems.
  295. *
  296. * @see Crypt_RC4::disableContinuousBuffer()
  297. * @access public
  298. */
  299. function enableContinuousBuffer()
  300. {
  301. $this->continuousBuffer = true;
  302. }
  303. /**
  304. * Treat consecutive packets as if they are a discontinuous buffer.
  305. *
  306. * The default behavior.
  307. *
  308. * @see Crypt_RC4::enableContinuousBuffer()
  309. * @access public
  310. */
  311. function disableContinuousBuffer()
  312. {
  313. if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_INTERNAL ) {
  314. $this->encryptIndex = $this->decryptIndex = array(0, 0);
  315. $this->setKey($this->key);
  316. }
  317. $this->continuousBuffer = false;
  318. }
  319. /**
  320. * Dummy function.
  321. *
  322. * Since RC4 is a stream cipher and not a block cipher, no padding is necessary. The only reason this function is
  323. * included is so that you can switch between a block cipher and a stream cipher transparently.
  324. *
  325. * @see Crypt_RC4::disablePadding()
  326. * @access public
  327. */
  328. function enablePadding()
  329. {
  330. }
  331. /**
  332. * Dummy function.
  333. *
  334. * @see Crypt_RC4::enablePadding()
  335. * @access public
  336. */
  337. function disablePadding()
  338. {
  339. }
  340. /**
  341. * Class destructor.
  342. *
  343. * Will be called, automatically, if you're using PHP5. If you're using PHP4, call it yourself. Only really
  344. * needs to be called if mcrypt is being used.
  345. *
  346. * @access public
  347. */
  348. function __destruct()
  349. {
  350. if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
  351. $this->_closeMCrypt();
  352. }
  353. }
  354. /**
  355. * Properly close the MCrypt objects.
  356. *
  357. * @access prviate
  358. */
  359. function _closeMCrypt()
  360. {
  361. if ( $this->encryptStream !== false ) {
  362. if ( $this->continuousBuffer ) {
  363. mcrypt_generic_deinit($this->encryptStream);
  364. }
  365. mcrypt_module_close($this->encryptStream);
  366. $this->encryptStream = false;
  367. }
  368. if ( $this->decryptStream !== false ) {
  369. if ( $this->continuousBuffer ) {
  370. mcrypt_generic_deinit($this->decryptStream);
  371. }
  372. mcrypt_module_close($this->decryptStream);
  373. $this->decryptStream = false;
  374. }
  375. }
  376. // @function fromHex 把十六进制数转换成字符串
  377. function toHex($sa , $len = 0){
  378. $buf = "";
  379. if( $len == 0 )
  380. $len = strlen($sa) ;
  381. for ($i = 0; $i < $len; $i++)
  382. {
  383. $val = dechex(ord($sa{$i}));
  384. if(strlen($val)< 2)
  385. $val = "0".$val;
  386. $buf .= $val;
  387. }
  388. return $buf;
  389. }
  390. // @function fromHex 把十六进制数转换成字符串
  391. function fromHex($sa){
  392. $buf = "";
  393. $len = strlen($sa) ;
  394. for($i = 0; $i < $len; $i += 2){
  395. $val = chr(hexdec(substr($sa, $i, 2)));
  396. $buf .= $val;
  397. }
  398. return $buf;
  399. }
  400. }

使用方法:

  1. include('class.rc4crypt.php');
  2. $rc4 = new Crypt_RC4();
  3. $rc4 -> setKey('21sd54a1w5q');
  4. $text = 'www.phpfensi.com';
  5. echo $x = $rc4->encrypt($text);//加密
  6. echo '<br />';
  7. echo $rc4->decrypt( $x) ;//解密

运行结果:

7907bb7c6694f179e9642ebd

www.phpfensi.com