RC4.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /**
  3. * Pure-PHP implementation of RC4.
  4. *
  5. * Uses mcrypt, if available, and an internal implementation, otherwise.
  6. *
  7. * PHP version 5
  8. *
  9. * Useful resources are as follows:
  10. *
  11. * - {@link http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt ARCFOUR Algorithm}
  12. * - {@link http://en.wikipedia.org/wiki/RC4 - Wikipedia: RC4}
  13. *
  14. * RC4 is also known as ARCFOUR or ARC4. The reason is elaborated upon at Wikipedia. This class is named RC4 and not
  15. * ARCFOUR or ARC4 because RC4 is how it is referred to in the SSH1 specification.
  16. *
  17. * Here's a short example of how to use this library:
  18. * <code>
  19. * <?php
  20. * include 'vendor/autoload.php';
  21. *
  22. * $rc4 = new \phpseclib\Crypt\RC4();
  23. *
  24. * $rc4->setKey('abcdefgh');
  25. *
  26. * $size = 10 * 1024;
  27. * $plaintext = '';
  28. * for ($i = 0; $i < $size; $i++) {
  29. * $plaintext.= 'a';
  30. * }
  31. *
  32. * echo $rc4->decrypt($rc4->encrypt($plaintext));
  33. * ?>
  34. * </code>
  35. *
  36. * @category Crypt
  37. * @package RC4
  38. * @author Jim Wigginton <terrafrost@php.net>
  39. * @copyright 2007 Jim Wigginton
  40. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  41. * @link http://phpseclib.sourceforge.net
  42. */
  43. namespace phpseclib\Crypt;
  44. /**
  45. * Pure-PHP implementation of RC4.
  46. *
  47. * @package RC4
  48. * @author Jim Wigginton <terrafrost@php.net>
  49. * @access public
  50. */
  51. class RC4 extends Base
  52. {
  53. /**#@+
  54. * @access private
  55. * @see \phpseclib\Crypt\RC4::_crypt()
  56. */
  57. const ENCRYPT = 0;
  58. const DECRYPT = 1;
  59. /**#@-*/
  60. /**
  61. * Block Length of the cipher
  62. *
  63. * RC4 is a stream cipher
  64. * so we the block_size to 0
  65. *
  66. * @see \phpseclib\Crypt\Base::block_size
  67. * @var int
  68. * @access private
  69. */
  70. var $block_size = 0;
  71. /**
  72. * Key Length (in bytes)
  73. *
  74. * @see \phpseclib\Crypt\RC4::setKeyLength()
  75. * @var int
  76. * @access private
  77. */
  78. var $key_length = 128; // = 1024 bits
  79. /**
  80. * The mcrypt specific name of the cipher
  81. *
  82. * @see \phpseclib\Crypt\Base::cipher_name_mcrypt
  83. * @var string
  84. * @access private
  85. */
  86. var $cipher_name_mcrypt = 'arcfour';
  87. /**
  88. * Holds whether performance-optimized $inline_crypt() can/should be used.
  89. *
  90. * @see \phpseclib\Crypt\Base::inline_crypt
  91. * @var mixed
  92. * @access private
  93. */
  94. var $use_inline_crypt = false; // currently not available
  95. /**
  96. * The Key
  97. *
  98. * @see self::setKey()
  99. * @var string
  100. * @access private
  101. */
  102. var $key = "\0";
  103. /**
  104. * The Key Stream for decryption and encryption
  105. *
  106. * @see self::setKey()
  107. * @var array
  108. * @access private
  109. */
  110. var $stream;
  111. /**
  112. * Default Constructor.
  113. *
  114. * @see \phpseclib\Crypt\Base::__construct()
  115. * @return \phpseclib\Crypt\RC4
  116. * @access public
  117. */
  118. function __construct()
  119. {
  120. parent::__construct(Base::MODE_STREAM);
  121. }
  122. /**
  123. * Test for engine validity
  124. *
  125. * This is mainly just a wrapper to set things up for \phpseclib\Crypt\Base::isValidEngine()
  126. *
  127. * @see \phpseclib\Crypt\Base::__construct()
  128. * @param int $engine
  129. * @access public
  130. * @return bool
  131. */
  132. function isValidEngine($engine)
  133. {
  134. switch ($engine) {
  135. case Base::ENGINE_OPENSSL:
  136. switch (strlen($this->key)) {
  137. case 5:
  138. $this->cipher_name_openssl = 'rc4-40';
  139. break;
  140. case 8:
  141. $this->cipher_name_openssl = 'rc4-64';
  142. break;
  143. case 16:
  144. $this->cipher_name_openssl = 'rc4';
  145. break;
  146. default:
  147. return false;
  148. }
  149. }
  150. return parent::isValidEngine($engine);
  151. }
  152. /**
  153. * RC4 does not use an IV
  154. *
  155. * @access public
  156. * @return bool
  157. */
  158. function usesIV()
  159. {
  160. return false;
  161. }
  162. /**
  163. * Sets the key length
  164. *
  165. * Keys can be between 1 and 256 bytes long.
  166. *
  167. * @access public
  168. * @param int $length
  169. * @throws \LengthException if the key length is invalid
  170. */
  171. function setKeyLength($length)
  172. {
  173. if ($length < 8 || $length > 2048) {
  174. throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys between 1 and 256 bytes are supported');
  175. }
  176. $this->key_length = $length >> 3;
  177. parent::setKeyLength($length);
  178. }
  179. /**
  180. * Sets the key length
  181. *
  182. * Keys can be between 1 and 256 bytes long.
  183. *
  184. * @access public
  185. * @param int $length
  186. * @throws \LengthException if the key length is invalid
  187. */
  188. function setKey($key)
  189. {
  190. $length = strlen($key);
  191. if ($length < 1 || $length > 256) {
  192. throw new \LengthException('Key size of ' . $length . ' bytes is not supported by RC4. Keys must be between 1 and 256 bytes long');
  193. }
  194. parent::setKey($key);
  195. }
  196. /**
  197. * Encrypts a message.
  198. *
  199. * @see \phpseclib\Crypt\Base::decrypt()
  200. * @see self::_crypt()
  201. * @access public
  202. * @param string $plaintext
  203. * @return string $ciphertext
  204. */
  205. function encrypt($plaintext)
  206. {
  207. if ($this->engine != Base::ENGINE_INTERNAL) {
  208. return parent::encrypt($plaintext);
  209. }
  210. return $this->_crypt($plaintext, self::ENCRYPT);
  211. }
  212. /**
  213. * Decrypts a message.
  214. *
  215. * $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)).
  216. * At least if the continuous buffer is disabled.
  217. *
  218. * @see \phpseclib\Crypt\Base::encrypt()
  219. * @see self::_crypt()
  220. * @access public
  221. * @param string $ciphertext
  222. * @return string $plaintext
  223. */
  224. function decrypt($ciphertext)
  225. {
  226. if ($this->engine != Base::ENGINE_INTERNAL) {
  227. return parent::decrypt($ciphertext);
  228. }
  229. return $this->_crypt($ciphertext, self::DECRYPT);
  230. }
  231. /**
  232. * Encrypts a block
  233. *
  234. * @access private
  235. * @param string $in
  236. */
  237. function _encryptBlock($in)
  238. {
  239. // RC4 does not utilize this method
  240. }
  241. /**
  242. * Decrypts a block
  243. *
  244. * @access private
  245. * @param string $in
  246. */
  247. function _decryptBlock($in)
  248. {
  249. // RC4 does not utilize this method
  250. }
  251. /**
  252. * Setup the key (expansion)
  253. *
  254. * @see \phpseclib\Crypt\Base::_setupKey()
  255. * @access private
  256. */
  257. function _setupKey()
  258. {
  259. $key = $this->key;
  260. $keyLength = strlen($key);
  261. $keyStream = range(0, 255);
  262. $j = 0;
  263. for ($i = 0; $i < 256; $i++) {
  264. $j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
  265. $temp = $keyStream[$i];
  266. $keyStream[$i] = $keyStream[$j];
  267. $keyStream[$j] = $temp;
  268. }
  269. $this->stream = array();
  270. $this->stream[self::DECRYPT] = $this->stream[self::ENCRYPT] = array(
  271. 0, // index $i
  272. 0, // index $j
  273. $keyStream
  274. );
  275. }
  276. /**
  277. * Encrypts or decrypts a message.
  278. *
  279. * @see self::encrypt()
  280. * @see self::decrypt()
  281. * @access private
  282. * @param string $text
  283. * @param int $mode
  284. * @return string $text
  285. */
  286. function _crypt($text, $mode)
  287. {
  288. if ($this->changed) {
  289. $this->_setup();
  290. $this->changed = false;
  291. }
  292. $stream = &$this->stream[$mode];
  293. if ($this->continuousBuffer) {
  294. $i = &$stream[0];
  295. $j = &$stream[1];
  296. $keyStream = &$stream[2];
  297. } else {
  298. $i = $stream[0];
  299. $j = $stream[1];
  300. $keyStream = $stream[2];
  301. }
  302. $len = strlen($text);
  303. for ($k = 0; $k < $len; ++$k) {
  304. $i = ($i + 1) & 255;
  305. $ksi = $keyStream[$i];
  306. $j = ($j + $ksi) & 255;
  307. $ksj = $keyStream[$j];
  308. $keyStream[$i] = $ksj;
  309. $keyStream[$j] = $ksi;
  310. $text[$k] = $text[$k] ^ chr($keyStream[($ksj + $ksi) & 255]);
  311. }
  312. return $text;
  313. }
  314. }