| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Pure-PHP implementation of RC4. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Uses mcrypt, if available, and an internal implementation, otherwise. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |  * PHP version 5 | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Useful resources are as follows: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  - {@link http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt ARCFOUR Algorithm} | 
					
						
							|  |  |  |  *  - {@link http://en.wikipedia.org/wiki/RC4 - Wikipedia: RC4} | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * RC4 is also known as ARCFOUR or ARC4.  The reason is elaborated upon at Wikipedia.  This class is named RC4 and not | 
					
						
							|  |  |  |  * ARCFOUR or ARC4 because RC4 is how it is referred to in the SSH1 specification. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Here's a short example of how to use this library: | 
					
						
							|  |  |  |  * <code> | 
					
						
							|  |  |  |  * <?php | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |  *    include 'vendor/autoload.php'; | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |  *    $rc4 = new \phpseclib\Crypt\RC4(); | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  *    $rc4->setKey('abcdefgh'); | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *    $size = 10 * 1024; | 
					
						
							|  |  |  |  *    $plaintext = ''; | 
					
						
							|  |  |  |  *    for ($i = 0; $i < $size; $i++) { | 
					
						
							|  |  |  |  *        $plaintext.= 'a'; | 
					
						
							|  |  |  |  *    } | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *    echo $rc4->decrypt($rc4->encrypt($plaintext)); | 
					
						
							|  |  |  |  * ?>
 | 
					
						
							|  |  |  |  * </code> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |  * @category  Crypt | 
					
						
							|  |  |  |  * @package   RC4 | 
					
						
							|  |  |  |  * @author    Jim Wigginton <terrafrost@php.net> | 
					
						
							|  |  |  |  * @copyright 2007 Jim Wigginton | 
					
						
							|  |  |  |  * @license   http://www.opensource.org/licenses/mit-license.html  MIT License | 
					
						
							|  |  |  |  * @link      http://phpseclib.sourceforge.net | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  | namespace phpseclib\Crypt; | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Pure-PHP implementation of RC4. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |  * @package RC4 | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |  * @author  Jim Wigginton <terrafrost@php.net> | 
					
						
							|  |  |  |  * @access  public | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  | class RC4 extends Base | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /**#@+
 | 
					
						
							|  |  |  |      * @access private | 
					
						
							|  |  |  |      * @see \phpseclib\Crypt\RC4::_crypt() | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     const ENCRYPT = 0; | 
					
						
							|  |  |  |     const DECRYPT = 1; | 
					
						
							|  |  |  |     /**#@-*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Block Length of the cipher | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * RC4 is a stream cipher | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * so we the block_size to 0 | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @see \phpseclib\Crypt\Base::block_size | 
					
						
							|  |  |  |      * @var int | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * @access private | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     var $block_size = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * Key Length (in bytes) | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @see \phpseclib\Crypt\RC4::setKeyLength() | 
					
						
							|  |  |  |      * @var int | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * @access private | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |     var $key_length = 128; // = 1024 bits
 | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * The mcrypt specific name of the cipher | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @see \phpseclib\Crypt\Base::cipher_name_mcrypt | 
					
						
							|  |  |  |      * @var string | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * @access private | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     var $cipher_name_mcrypt = 'arcfour'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Holds whether performance-optimized $inline_crypt() can/should be used. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @see \phpseclib\Crypt\Base::inline_crypt | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * @var mixed | 
					
						
							|  |  |  |      * @access private | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     var $use_inline_crypt = false; // currently not available
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * The Key | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @see self::setKey() | 
					
						
							|  |  |  |      * @var string | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * @access private | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     var $key = "\0"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * The Key Stream for decryption and encryption | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @see self::setKey() | 
					
						
							|  |  |  |      * @var array | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * @access private | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     var $stream; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Default Constructor. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @see \phpseclib\Crypt\Base::__construct() | 
					
						
							|  |  |  |      * @return \phpseclib\Crypt\RC4 | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * @access public | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |     function __construct() | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |         parent::__construct(Base::MODE_STREAM); | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * Test for engine validity | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * This is mainly just a wrapper to set things up for \phpseclib\Crypt\Base::isValidEngine() | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @see \phpseclib\Crypt\Base::__construct() | 
					
						
							|  |  |  |      * @param int $engine | 
					
						
							|  |  |  |      * @access public | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function isValidEngine($engine) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         switch ($engine) { | 
					
						
							|  |  |  |             case Base::ENGINE_OPENSSL: | 
					
						
							|  |  |  |                 switch (strlen($this->key)) { | 
					
						
							|  |  |  |                     case 5: | 
					
						
							|  |  |  |                         $this->cipher_name_openssl = 'rc4-40'; | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case 8: | 
					
						
							|  |  |  |                         $this->cipher_name_openssl = 'rc4-64'; | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case 16: | 
					
						
							|  |  |  |                         $this->cipher_name_openssl = 'rc4'; | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     default: | 
					
						
							|  |  |  |                         return false; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return parent::isValidEngine($engine); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * RC4 does not use an IV | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @access public | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function usesIV() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Sets the key length | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * Keys can be between 1 and 256 bytes long. | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @access public | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @param int $length | 
					
						
							|  |  |  |      * @throws \LengthException if the key length is invalid | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |     function setKeyLength($length) | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |         if ($length < 8 || $length > 2048) { | 
					
						
							|  |  |  |             throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys between 1 and 256 bytes are supported'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->key_length = $length >> 3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parent::setKeyLength($length); | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * Sets the key length | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * Keys can be between 1 and 256 bytes long. | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @access public | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @param int $length | 
					
						
							|  |  |  |      * @throws \LengthException if the key length is invalid | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     function setKey($key) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |         $length = strlen($key); | 
					
						
							|  |  |  |         if ($length < 1 || $length > 256) { | 
					
						
							|  |  |  |             throw new \LengthException('Key size of ' . $length . ' bytes is not supported by RC4. Keys must be between 1 and 256 bytes long'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parent::setKey($key); | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Encrypts a message. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @see \phpseclib\Crypt\Base::decrypt() | 
					
						
							|  |  |  |      * @see self::_crypt() | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * @access public | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @param string $plaintext | 
					
						
							|  |  |  |      * @return string $ciphertext | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     function encrypt($plaintext) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |         if ($this->engine != Base::ENGINE_INTERNAL) { | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |             return parent::encrypt($plaintext); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |         return $this->_crypt($plaintext, self::ENCRYPT); | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Decrypts a message. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)). | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * At least if the continuous buffer is disabled. | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @see \phpseclib\Crypt\Base::encrypt() | 
					
						
							|  |  |  |      * @see self::_crypt() | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * @access public | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @param string $ciphertext | 
					
						
							|  |  |  |      * @return string $plaintext | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     function decrypt($ciphertext) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |         if ($this->engine != Base::ENGINE_INTERNAL) { | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |             return parent::decrypt($ciphertext); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |         return $this->_crypt($ciphertext, self::DECRYPT); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Encrypts a block | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @access private | 
					
						
							|  |  |  |      * @param string $in | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function _encryptBlock($in) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // RC4 does not utilize this method
 | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Decrypts a block | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @access private | 
					
						
							|  |  |  |      * @param string $in | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function _decryptBlock($in) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // RC4 does not utilize this method
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Setup the key (expansion) | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @see \phpseclib\Crypt\Base::_setupKey() | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * @access private | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function _setupKey() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $key = $this->key; | 
					
						
							|  |  |  |         $keyLength = strlen($key); | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |         $keyStream = range(0, 255); | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |         $j = 0; | 
					
						
							|  |  |  |         for ($i = 0; $i < 256; $i++) { | 
					
						
							|  |  |  |             $j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255; | 
					
						
							|  |  |  |             $temp = $keyStream[$i]; | 
					
						
							|  |  |  |             $keyStream[$i] = $keyStream[$j]; | 
					
						
							|  |  |  |             $keyStream[$j] = $temp; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->stream = array(); | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |         $this->stream[self::DECRYPT] = $this->stream[self::ENCRYPT] = array( | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |             0, // index $i
 | 
					
						
							|  |  |  |             0, // index $j
 | 
					
						
							|  |  |  |             $keyStream | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Encrypts or decrypts a message. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @see self::encrypt() | 
					
						
							|  |  |  |      * @see self::decrypt() | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      * @access private | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |      * @param string $text | 
					
						
							|  |  |  |      * @param int $mode | 
					
						
							|  |  |  |      * @return string $text | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     function _crypt($text, $mode) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($this->changed) { | 
					
						
							|  |  |  |             $this->_setup(); | 
					
						
							|  |  |  |             $this->changed = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $stream = &$this->stream[$mode]; | 
					
						
							|  |  |  |         if ($this->continuousBuffer) { | 
					
						
							|  |  |  |             $i = &$stream[0]; | 
					
						
							|  |  |  |             $j = &$stream[1]; | 
					
						
							|  |  |  |             $keyStream = &$stream[2]; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $i = $stream[0]; | 
					
						
							|  |  |  |             $j = $stream[1]; | 
					
						
							|  |  |  |             $keyStream = $stream[2]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $len = strlen($text); | 
					
						
							|  |  |  |         for ($k = 0; $k < $len; ++$k) { | 
					
						
							|  |  |  |             $i = ($i + 1) & 255; | 
					
						
							|  |  |  |             $ksi = $keyStream[$i]; | 
					
						
							|  |  |  |             $j = ($j + $ksi) & 255; | 
					
						
							|  |  |  |             $ksj = $keyStream[$j]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $keyStream[$i] = $ksj; | 
					
						
							|  |  |  |             $keyStream[$j] = $ksi; | 
					
						
							| 
									
										
										
										
											2016-06-17 22:44:12 +02:00
										 |  |  |             $text[$k] = $text[$k] ^ chr($keyStream[($ksj + $ksi) & 255]); | 
					
						
							| 
									
										
										
										
											2013-10-05 00:16:07 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $text; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |