4 * Pure-PHP implementation of RC4.
6 * Uses mcrypt, if available, and an internal implementation, otherwise.
10 * Useful resources are as follows:
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}
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.
18 * Here's a short example of how to use this library:
21 * include 'Crypt/RC4.php';
23 * $rc4 = new Crypt_RC4();
25 * $rc4->setKey('abcdefgh');
29 * for ($i = 0; $i < $size; $i++) {
33 * echo $rc4->decrypt($rc4->encrypt($plaintext));
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:
44 * The above copyright notice and this permission notice shall be included in
45 * all copies or substantial portions of the Software.
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
57 * @author Jim Wigginton <terrafrost@php.net>
58 * @copyright 2007 Jim Wigginton
59 * @license http://www.opensource.org/licenses/mit-license.html MIT License
60 * @link http://phpseclib.sourceforge.net
68 if (!class_exists('Crypt_Base')) {
69 include_once 'Base.php';
74 * @see Crypt_RC4::Crypt_RC4()
77 * Toggles the internal implementation
79 define('CRYPT_RC4_MODE_INTERNAL', CRYPT_MODE_INTERNAL);
81 * Toggles the mcrypt implementation
83 define('CRYPT_RC4_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
88 * @see Crypt_RC4::_crypt()
90 define('CRYPT_RC4_ENCRYPT', 0);
91 define('CRYPT_RC4_DECRYPT', 1);
95 * Pure-PHP implementation of RC4.
98 * @author Jim Wigginton <terrafrost@php.net>
101 class Crypt_RC4 extends Crypt_Base
104 * Block Length of the cipher
106 * RC4 is a stream cipher
107 * so we the block_size to 0
109 * @see Crypt_Base::block_size
116 * The default password key_size used by setPassword()
118 * @see Crypt_Base::password_key_size
119 * @see Crypt_Base::setPassword()
123 var $password_key_size = 128; // = 1024 bits
126 * The namespace used by the cipher for its constants.
128 * @see Crypt_Base::const_namespace
132 var $const_namespace = 'RC4';
135 * The mcrypt specific name of the cipher
137 * @see Crypt_Base::cipher_name_mcrypt
141 var $cipher_name_mcrypt = 'arcfour';
144 * Holds whether performance-optimized $inline_crypt() can/should be used.
146 * @see Crypt_Base::inline_crypt
150 var $use_inline_crypt = false; // currently not available
155 * @see Crypt_RC4::setKey()
162 * The Key Stream for decryption and encryption
164 * @see Crypt_RC4::setKey()
171 * Default Constructor.
173 * Determines whether or not the mcrypt extension should be used.
175 * @see Crypt_Base::Crypt_Base()
181 parent::Crypt_Base(CRYPT_MODE_STREAM);
187 * Some protocols, such as WEP, prepend an "initialization vector" to the key, effectively creating a new key [1].
188 * If you need to use an initialization vector in this manner, feel free to prepend it to the key, yourself, before
191 * [1] WEP's initialization vectors (IV's) are used in a somewhat insecure way. Since, in that protocol,
192 * the IV's are relatively easy to predict, an attack described by
193 * {@link http://www.drizzle.com/~aboba/IEEE/rc4_ksaproc.pdf Scott Fluhrer, Itsik Mantin, and Adi Shamir}
194 * can be used to quickly guess at the rest of the key. The following links elaborate:
196 * {@link http://www.rsa.com/rsalabs/node.asp?id=2009 http://www.rsa.com/rsalabs/node.asp?id=2009}
197 * {@link http://en.wikipedia.org/wiki/Related_key_attack http://en.wikipedia.org/wiki/Related_key_attack}
200 * @see Crypt_RC4::setKey()
210 * Keys can be between 1 and 256 bytes long. If they are longer then 256 bytes, the first 256 bytes will
211 * be used. If no key is explicitly set, it'll be assumed to be a single null byte.
214 * @see Crypt_Base::setKey()
217 function setKey($key)
219 parent::setKey(substr($key, 0, 256));
223 * Encrypts a message.
225 * @see Crypt_Base::decrypt()
226 * @see Crypt_RC4::_crypt()
228 * @param String $plaintext
229 * @return String $ciphertext
231 function encrypt($plaintext)
233 if ($this->engine == CRYPT_MODE_MCRYPT) {
234 return parent::encrypt($plaintext);
236 return $this->_crypt($plaintext, CRYPT_RC4_ENCRYPT);
240 * Decrypts a message.
242 * $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)).
243 * At least if the continuous buffer is disabled.
245 * @see Crypt_Base::encrypt()
246 * @see Crypt_RC4::_crypt()
248 * @param String $ciphertext
249 * @return String $plaintext
251 function decrypt($ciphertext)
253 if ($this->engine == CRYPT_MODE_MCRYPT) {
254 return parent::decrypt($ciphertext);
256 return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT);
261 * Setup the key (expansion)
263 * @see Crypt_Base::_setupKey()
269 $keyLength = strlen($key);
270 $keyStream = range(0, 255);
272 for ($i = 0; $i < 256; $i++) {
273 $j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
274 $temp = $keyStream[$i];
275 $keyStream[$i] = $keyStream[$j];
276 $keyStream[$j] = $temp;
279 $this->stream = array();
280 $this->stream[CRYPT_RC4_DECRYPT] = $this->stream[CRYPT_RC4_ENCRYPT] = array(
288 * Encrypts or decrypts a message.
290 * @see Crypt_RC4::encrypt()
291 * @see Crypt_RC4::decrypt()
293 * @param String $text
294 * @param Integer $mode
295 * @return String $text
297 function _crypt($text, $mode)
299 if ($this->changed) {
301 $this->changed = false;
304 $stream = &$this->stream[$mode];
305 if ($this->continuousBuffer) {
308 $keyStream = &$stream[2];
312 $keyStream = $stream[2];
315 $len = strlen($text);
316 for ($k = 0; $k < $len; ++$k) {
318 $ksi = $keyStream[$i];
319 $j = ($j + $ksi) & 255;
320 $ksj = $keyStream[$j];
322 $keyStream[$i] = $ksj;
323 $keyStream[$j] = $ksi;
324 $text[$k] = $text[$k] ^ chr($keyStream[($ksj + $ksi) & 255]);