2010-05-14 14:06:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP implementation of RC4.
|
|
|
|
*
|
|
|
|
* Uses mcrypt, if available, and an internal implementation, otherwise.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* 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
|
2014-03-30 06:55:33 +00:00
|
|
|
* ARCFOUR or ARC4 because RC4 is how it is referred to in the SSH1 specification.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
|
|
|
* Here's a short example of how to use this library:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
2015-07-06 19:44:12 +00:00
|
|
|
* include 'Crypt/RC4.php';
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
|
|
|
* $rc4 = new Crypt_RC4();
|
|
|
|
*
|
|
|
|
* $rc4->setKey('abcdefgh');
|
|
|
|
*
|
|
|
|
* $size = 10 * 1024;
|
|
|
|
* $plaintext = '';
|
|
|
|
* for ($i = 0; $i < $size; $i++) {
|
|
|
|
* $plaintext.= 'a';
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* echo $rc4->decrypt($rc4->encrypt($plaintext));
|
|
|
|
* ?>
|
|
|
|
* </code>
|
|
|
|
*
|
2011-05-19 18:02:11 +00:00
|
|
|
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2014-03-30 06:55:33 +00:00
|
|
|
*
|
2011-05-19 18:02:11 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2014-03-30 06:55:33 +00:00
|
|
|
*
|
2011-05-19 18:02:11 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @category Crypt
|
|
|
|
* @package Crypt_RC4
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
2015-07-06 19:44:12 +00:00
|
|
|
* @copyright 2007 Jim Wigginton
|
2014-03-30 06:55:33 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include Crypt_Base
|
|
|
|
*
|
|
|
|
* Base cipher class
|
2010-05-14 14:06:17 +00:00
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
if (!class_exists('Crypt_Base')) {
|
|
|
|
include_once 'Base.php';
|
|
|
|
}
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* @access private
|
|
|
|
* @see Crypt_RC4::_crypt()
|
|
|
|
*/
|
|
|
|
define('CRYPT_RC4_ENCRYPT', 0);
|
|
|
|
define('CRYPT_RC4_DECRYPT', 1);
|
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP implementation of RC4.
|
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @package Crypt_RC4
|
2010-05-14 14:06:17 +00:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
class Crypt_RC4 extends Crypt_Base
|
|
|
|
{
|
2010-05-14 14:06:17 +00:00
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* Block Length of the cipher
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* RC4 is a stream cipher
|
|
|
|
* so we the block_size to 0
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::block_size
|
|
|
|
* @var Integer
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
var $block_size = 0;
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* The default password key_size used by setPassword()
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::password_key_size
|
|
|
|
* @see Crypt_Base::setPassword()
|
|
|
|
* @var Integer
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
var $password_key_size = 128; // = 1024 bits
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* The namespace used by the cipher for its constants.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::const_namespace
|
|
|
|
* @var String
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
var $const_namespace = 'RC4';
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* The mcrypt specific name of the cipher
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::cipher_name_mcrypt
|
|
|
|
* @var String
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
var $cipher_name_mcrypt = 'arcfour';
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* Holds whether performance-optimized $inline_crypt() can/should be used.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::inline_crypt
|
|
|
|
* @var mixed
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
var $use_inline_crypt = false; // currently not available
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* The Key
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_RC4::setKey()
|
|
|
|
* @var String
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
var $key = "\0";
|
2010-05-14 14:06:17 +00:00
|
|
|
|
2011-05-19 18:02:11 +00:00
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* The Key Stream for decryption and encryption
|
2011-05-19 18:02:11 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_RC4::setKey()
|
|
|
|
* @var Array
|
2011-05-19 18:02:11 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
var $stream;
|
2011-05-19 18:02:11 +00:00
|
|
|
|
2010-05-14 14:06:17 +00:00
|
|
|
/**
|
|
|
|
* Default Constructor.
|
|
|
|
*
|
|
|
|
* Determines whether or not the mcrypt extension should be used.
|
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::Crypt_Base()
|
2010-05-14 14:06:17 +00:00
|
|
|
* @return Crypt_RC4
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function Crypt_RC4()
|
|
|
|
{
|
2014-03-30 06:55:33 +00:00
|
|
|
parent::Crypt_Base(CRYPT_MODE_STREAM);
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
2015-10-04 13:24:43 +00:00
|
|
|
/**
|
|
|
|
* Test for engine validity
|
|
|
|
*
|
|
|
|
* This is mainly just a wrapper to set things up for Crypt_Base::isValidEngine()
|
|
|
|
*
|
|
|
|
* @see Crypt_Base::Crypt_Base()
|
|
|
|
* @param Integer $engine
|
|
|
|
* @access public
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
function isValidEngine($engine)
|
|
|
|
{
|
|
|
|
switch ($engine) {
|
|
|
|
case CRYPT_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);
|
|
|
|
}
|
|
|
|
|
2010-05-14 14:06:17 +00:00
|
|
|
/**
|
|
|
|
* Dummy function.
|
|
|
|
*
|
|
|
|
* Some protocols, such as WEP, prepend an "initialization vector" to the key, effectively creating a new key [1].
|
|
|
|
* If you need to use an initialization vector in this manner, feel free to prepend it to the key, yourself, before
|
|
|
|
* calling setKey().
|
|
|
|
*
|
|
|
|
* [1] WEP's initialization vectors (IV's) are used in a somewhat insecure way. Since, in that protocol,
|
|
|
|
* the IV's are relatively easy to predict, an attack described by
|
|
|
|
* {@link http://www.drizzle.com/~aboba/IEEE/rc4_ksaproc.pdf Scott Fluhrer, Itsik Mantin, and Adi Shamir}
|
|
|
|
* can be used to quickly guess at the rest of the key. The following links elaborate:
|
|
|
|
*
|
|
|
|
* {@link http://www.rsa.com/rsalabs/node.asp?id=2009 http://www.rsa.com/rsalabs/node.asp?id=2009}
|
|
|
|
* {@link http://en.wikipedia.org/wiki/Related_key_attack http://en.wikipedia.org/wiki/Related_key_attack}
|
|
|
|
*
|
|
|
|
* @param String $iv
|
|
|
|
* @see Crypt_RC4::setKey()
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function setIV($iv)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* Sets the key.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* Keys can be between 1 and 256 bytes long. If they are longer then 256 bytes, the first 256 bytes will
|
|
|
|
* be used. If no key is explicitly set, it'll be assumed to be a single null byte.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
|
|
|
* @access public
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::setKey()
|
|
|
|
* @param String $key
|
2010-05-14 14:06:17 +00:00
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
function setKey($key)
|
2010-05-14 14:06:17 +00:00
|
|
|
{
|
2014-03-30 06:55:33 +00:00
|
|
|
parent::setKey(substr($key, 0, 256));
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encrypts a message.
|
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::decrypt()
|
2010-05-14 14:06:17 +00:00
|
|
|
* @see Crypt_RC4::_crypt()
|
|
|
|
* @access public
|
|
|
|
* @param String $plaintext
|
2014-03-30 06:55:33 +00:00
|
|
|
* @return String $ciphertext
|
2010-05-14 14:06:17 +00:00
|
|
|
*/
|
|
|
|
function encrypt($plaintext)
|
|
|
|
{
|
2015-10-04 13:24:43 +00:00
|
|
|
if ($this->engine != CRYPT_ENGINE_INTERNAL) {
|
2014-03-30 06:55:33 +00:00
|
|
|
return parent::encrypt($plaintext);
|
|
|
|
}
|
2010-05-14 14:06:17 +00:00
|
|
|
return $this->_crypt($plaintext, CRYPT_RC4_ENCRYPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrypts a message.
|
|
|
|
*
|
|
|
|
* $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)).
|
2015-07-06 19:44:12 +00:00
|
|
|
* At least if the continuous buffer is disabled.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::encrypt()
|
2010-05-14 14:06:17 +00:00
|
|
|
* @see Crypt_RC4::_crypt()
|
|
|
|
* @access public
|
|
|
|
* @param String $ciphertext
|
2014-03-30 06:55:33 +00:00
|
|
|
* @return String $plaintext
|
2010-05-14 14:06:17 +00:00
|
|
|
*/
|
|
|
|
function decrypt($ciphertext)
|
|
|
|
{
|
2015-10-04 13:24:43 +00:00
|
|
|
if ($this->engine != CRYPT_ENGINE_INTERNAL) {
|
2014-03-30 06:55:33 +00:00
|
|
|
return parent::decrypt($ciphertext);
|
|
|
|
}
|
2010-05-14 14:06:17 +00:00
|
|
|
return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT);
|
|
|
|
}
|
|
|
|
|
2014-03-30 06:55:33 +00:00
|
|
|
|
2010-05-14 14:06:17 +00:00
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* Setup the key (expansion)
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::_setupKey()
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
function _setupKey()
|
2010-05-14 14:06:17 +00:00
|
|
|
{
|
2014-03-30 06:55:33 +00:00
|
|
|
$key = $this->key;
|
|
|
|
$keyLength = strlen($key);
|
2015-07-06 19:44:12 +00:00
|
|
|
$keyStream = range(0, 255);
|
2014-03-30 06:55:33 +00:00
|
|
|
$j = 0;
|
|
|
|
for ($i = 0; $i < 256; $i++) {
|
|
|
|
$j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
|
2010-05-14 14:06:17 +00:00
|
|
|
$temp = $keyStream[$i];
|
|
|
|
$keyStream[$i] = $keyStream[$j];
|
|
|
|
$keyStream[$j] = $temp;
|
|
|
|
}
|
|
|
|
|
2014-03-30 06:55:33 +00:00
|
|
|
$this->stream = array();
|
|
|
|
$this->stream[CRYPT_RC4_DECRYPT] = $this->stream[CRYPT_RC4_ENCRYPT] = array(
|
|
|
|
0, // index $i
|
|
|
|
0, // index $j
|
|
|
|
$keyStream
|
|
|
|
);
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* Encrypts or decrypts a message.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_RC4::encrypt()
|
|
|
|
* @see Crypt_RC4::decrypt()
|
|
|
|
* @access private
|
|
|
|
* @param String $text
|
|
|
|
* @param Integer $mode
|
|
|
|
* @return String $text
|
2010-05-14 14:06:17 +00:00
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
function _crypt($text, $mode)
|
2010-05-14 14:06:17 +00:00
|
|
|
{
|
2014-03-30 06:55:33 +00:00
|
|
|
if ($this->changed) {
|
|
|
|
$this->_setup();
|
|
|
|
$this->changed = false;
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 06:55:33 +00:00
|
|
|
$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];
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 06:55:33 +00:00
|
|
|
$len = strlen($text);
|
|
|
|
for ($k = 0; $k < $len; ++$k) {
|
|
|
|
$i = ($i + 1) & 255;
|
|
|
|
$ksi = $keyStream[$i];
|
|
|
|
$j = ($j + $ksi) & 255;
|
|
|
|
$ksj = $keyStream[$j];
|
2010-05-14 14:06:17 +00:00
|
|
|
|
2014-03-30 06:55:33 +00:00
|
|
|
$keyStream[$i] = $ksj;
|
|
|
|
$keyStream[$j] = $ksi;
|
2015-07-06 19:44:12 +00:00
|
|
|
$text[$k] = $text[$k] ^ chr($keyStream[($ksj + $ksi) & 255]);
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 06:55:33 +00:00
|
|
|
return $text;
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
2014-03-30 06:55:33 +00:00
|
|
|
}
|