RC4.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Pure-PHP implementation of RC4.
  5. *
  6. * Uses mcrypt, if available, and an internal implementation, otherwise.
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * Useful resources are as follows:
  11. *
  12. * - {@link http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt ARCFOUR Algorithm}
  13. * - {@link http://en.wikipedia.org/wiki/RC4 - Wikipedia: RC4}
  14. *
  15. * RC4 is also known as ARCFOUR or ARC4. The reason is elaborated upon at Wikipedia. This class is named RC4 and not
  16. * ARCFOUR or ARC4 because RC4 is how it is referred to in the SSH1 specification.
  17. *
  18. * Here's a short example of how to use this library:
  19. * <code>
  20. * <?php
  21. * include('Crypt/RC4.php');
  22. *
  23. * $rc4 = new Crypt_RC4();
  24. *
  25. * $rc4->setKey('abcdefgh');
  26. *
  27. * $size = 10 * 1024;
  28. * $plaintext = '';
  29. * for ($i = 0; $i < $size; $i++) {
  30. * $plaintext.= 'a';
  31. * }
  32. *
  33. * echo $rc4->decrypt($rc4->encrypt($plaintext));
  34. * ?>
  35. * </code>
  36. *
  37. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  38. * of this software and associated documentation files (the "Software"), to deal
  39. * in the Software without restriction, including without limitation the rights
  40. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  41. * copies of the Software, and to permit persons to whom the Software is
  42. * furnished to do so, subject to the following conditions:
  43. *
  44. * The above copyright notice and this permission notice shall be included in
  45. * all copies or substantial portions of the Software.
  46. *
  47. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  48. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  49. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  50. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  51. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  52. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  53. * THE SOFTWARE.
  54. *
  55. * @category Crypt
  56. * @package Crypt_RC4
  57. * @author Jim Wigginton <terrafrost@php.net>
  58. * @copyright MMVII Jim Wigginton
  59. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  60. * @link http://phpseclib.sourceforge.net
  61. */
  62. /**
  63. * Include Crypt_Base
  64. *
  65. * Base cipher class
  66. */
  67. if (!class_exists('Crypt_Base')) {
  68. require_once('Base.php');
  69. }
  70. /**#@+
  71. * @access private
  72. * @see Crypt_RC4::Crypt_RC4()
  73. */
  74. /**
  75. * Toggles the internal implementation
  76. */
  77. define('CRYPT_RC4_MODE_INTERNAL', CRYPT_MODE_INTERNAL);
  78. /**
  79. * Toggles the mcrypt implementation
  80. */
  81. define('CRYPT_RC4_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
  82. /**#@-*/
  83. /**#@+
  84. * @access private
  85. * @see Crypt_RC4::_crypt()
  86. */
  87. define('CRYPT_RC4_ENCRYPT', 0);
  88. define('CRYPT_RC4_DECRYPT', 1);
  89. /**#@-*/
  90. /**
  91. * Pure-PHP implementation of RC4.
  92. *
  93. * @author Jim Wigginton <terrafrost@php.net>
  94. * @version 0.1.0
  95. * @access public
  96. * @package Crypt_RC4
  97. */
  98. class Crypt_RC4 extends Crypt_Base {
  99. /**
  100. * Block Length of the cipher
  101. *
  102. * RC4 is a stream cipher
  103. * so we the block_size to 0
  104. *
  105. * @see Crypt_Base::block_size
  106. * @var Integer
  107. * @access private
  108. */
  109. var $block_size = 0;
  110. /**
  111. * The default password key_size used by setPassword()
  112. *
  113. * @see Crypt_Base::password_key_size
  114. * @see Crypt_Base::setPassword()
  115. * @var Integer
  116. * @access private
  117. */
  118. var $password_key_size = 128; // = 1024 bits
  119. /**
  120. * The namespace used by the cipher for its constants.
  121. *
  122. * @see Crypt_Base::const_namespace
  123. * @var String
  124. * @access private
  125. */
  126. var $const_namespace = 'RC4';
  127. /**
  128. * The mcrypt specific name of the cipher
  129. *
  130. * @see Crypt_Base::cipher_name_mcrypt
  131. * @var String
  132. * @access private
  133. */
  134. var $cipher_name_mcrypt = 'arcfour';
  135. /**
  136. * Holds whether performance-optimized $inline_crypt() can/should be used.
  137. *
  138. * @see Crypt_Base::inline_crypt
  139. * @var mixed
  140. * @access private
  141. */
  142. var $use_inline_crypt = false; // currently not available
  143. /**
  144. * The Key
  145. *
  146. * @see Crypt_RC4::setKey()
  147. * @var String
  148. * @access private
  149. */
  150. var $key = "\0";
  151. /**
  152. * The Key Stream for decryption and encryption
  153. *
  154. * @see Crypt_RC4::setKey()
  155. * @var Array
  156. * @access private
  157. */
  158. var $stream;
  159. /**
  160. * Default Constructor.
  161. *
  162. * Determines whether or not the mcrypt extension should be used.
  163. *
  164. * @see Crypt_Base::Crypt_Base()
  165. * @return Crypt_RC4
  166. * @access public
  167. */
  168. function Crypt_RC4()
  169. {
  170. parent::Crypt_Base(CRYPT_MODE_STREAM);
  171. }
  172. /**
  173. * Dummy function.
  174. *
  175. * Some protocols, such as WEP, prepend an "initialization vector" to the key, effectively creating a new key [1].
  176. * If you need to use an initialization vector in this manner, feel free to prepend it to the key, yourself, before
  177. * calling setKey().
  178. *
  179. * [1] WEP's initialization vectors (IV's) are used in a somewhat insecure way. Since, in that protocol,
  180. * the IV's are relatively easy to predict, an attack described by
  181. * {@link http://www.drizzle.com/~aboba/IEEE/rc4_ksaproc.pdf Scott Fluhrer, Itsik Mantin, and Adi Shamir}
  182. * can be used to quickly guess at the rest of the key. The following links elaborate:
  183. *
  184. * {@link http://www.rsa.com/rsalabs/node.asp?id=2009 http://www.rsa.com/rsalabs/node.asp?id=2009}
  185. * {@link http://en.wikipedia.org/wiki/Related_key_attack http://en.wikipedia.org/wiki/Related_key_attack}
  186. *
  187. * @param String $iv
  188. * @see Crypt_RC4::setKey()
  189. * @access public
  190. */
  191. function setIV($iv)
  192. {
  193. }
  194. /**
  195. * Sets the key.
  196. *
  197. * Keys can be between 1 and 256 bytes long. If they are longer then 256 bytes, the first 256 bytes will
  198. * be used. If no key is explicitly set, it'll be assumed to be a single null byte.
  199. *
  200. * @access public
  201. * @see Crypt_Base::setKey()
  202. * @param String $key
  203. */
  204. function setKey($key)
  205. {
  206. parent::setKey(substr($key, 0, 256));
  207. }
  208. /**
  209. * Encrypts a message.
  210. *
  211. * @see Crypt_Base::decrypt()
  212. * @see Crypt_RC4::_crypt()
  213. * @access public
  214. * @param String $plaintext
  215. * @return String $ciphertext
  216. */
  217. function encrypt($plaintext)
  218. {
  219. if ($this->engine == CRYPT_MODE_MCRYPT) {
  220. return parent::encrypt($plaintext);
  221. }
  222. return $this->_crypt($plaintext, CRYPT_RC4_ENCRYPT);
  223. }
  224. /**
  225. * Decrypts a message.
  226. *
  227. * $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)).
  228. * Atleast if the continuous buffer is disabled.
  229. *
  230. * @see Crypt_Base::encrypt()
  231. * @see Crypt_RC4::_crypt()
  232. * @access public
  233. * @param String $ciphertext
  234. * @return String $plaintext
  235. */
  236. function decrypt($ciphertext)
  237. {
  238. if ($this->engine == CRYPT_MODE_MCRYPT) {
  239. return parent::decrypt($ciphertext);
  240. }
  241. return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT);
  242. }
  243. /**
  244. * Setup the key (expansion)
  245. *
  246. * @see Crypt_Base::_setupKey()
  247. * @access private
  248. */
  249. function _setupKey()
  250. {
  251. $key = $this->key;
  252. $keyLength = strlen($key);
  253. $keyStream = array();
  254. for ($i = 0; $i < 256; $i++) {
  255. $keyStream[$i] = $i;
  256. }
  257. $j = 0;
  258. for ($i = 0; $i < 256; $i++) {
  259. $j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
  260. $temp = $keyStream[$i];
  261. $keyStream[$i] = $keyStream[$j];
  262. $keyStream[$j] = $temp;
  263. }
  264. $this->stream = array();
  265. $this->stream[CRYPT_RC4_DECRYPT] = $this->stream[CRYPT_RC4_ENCRYPT] = array(
  266. 0, // index $i
  267. 0, // index $j
  268. $keyStream
  269. );
  270. }
  271. /**
  272. * Encrypts or decrypts a message.
  273. *
  274. * @see Crypt_RC4::encrypt()
  275. * @see Crypt_RC4::decrypt()
  276. * @access private
  277. * @param String $text
  278. * @param Integer $mode
  279. * @return String $text
  280. */
  281. function _crypt($text, $mode)
  282. {
  283. if ($this->changed) {
  284. $this->_setup();
  285. $this->changed = false;
  286. }
  287. $stream = &$this->stream[$mode];
  288. if ($this->continuousBuffer) {
  289. $i = &$stream[0];
  290. $j = &$stream[1];
  291. $keyStream = &$stream[2];
  292. } else {
  293. $i = $stream[0];
  294. $j = $stream[1];
  295. $keyStream = $stream[2];
  296. }
  297. $len = strlen($text);
  298. for ($k = 0; $k < $len; ++$k) {
  299. $i = ($i + 1) & 255;
  300. $ksi = $keyStream[$i];
  301. $j = ($j + $ksi) & 255;
  302. $ksj = $keyStream[$j];
  303. $keyStream[$i] = $ksj;
  304. $keyStream[$j] = $ksi;
  305. $text[$k] = chr(ord($text[$k]) ^ $keyStream[($ksj + $ksi) & 255]);
  306. }
  307. return $text;
  308. }
  309. }
  310. // vim: ts=4:sw=4:et:
  311. // vim6: fdl=1: