2007-07-02 06:19:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2009-06-09 06:00:38 +02:00
|
|
|
* Pure-PHP implementation of RC4.
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
|
|
|
* 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
|
2013-05-08 16:34:07 +02:00
|
|
|
* ARCFOUR or ARC4 because RC4 is how it is referred to in the SSH1 specification.
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
|
|
|
* Here's a short example of how to use this library:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
2014-06-01 23:28:49 +02:00
|
|
|
* include 'Crypt/RC4.php';
|
2007-07-02 06:19:55 +02: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-04-18 14:17:40 +02: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:
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2011-04-18 14:17:40 +02:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2011-04-18 14:17:40 +02: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.
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2013-12-10 20:10:37 +01:00
|
|
|
* @category Crypt
|
|
|
|
* @package Crypt_RC4
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
2014-12-10 00:02:44 +01:00
|
|
|
* @copyright 2007 Jim Wigginton
|
2013-12-10 20:10:37 +01:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
2007-07-02 06:19:55 +02:00
|
|
|
*/
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
/**
|
|
|
|
* Include Crypt_Base
|
|
|
|
*
|
|
|
|
* Base cipher class
|
|
|
|
*/
|
|
|
|
if (!class_exists('Crypt_Base')) {
|
2013-12-06 01:03:34 +01:00
|
|
|
include_once 'Base.php';
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2007-07-02 06:19:55 +02:00
|
|
|
/**#@+
|
|
|
|
* @access private
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::_crypt()
|
2007-07-02 06:19:55 +02:00
|
|
|
*/
|
|
|
|
define('CRYPT_RC4_ENCRYPT', 0);
|
|
|
|
define('CRYPT_RC4_DECRYPT', 1);
|
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP implementation of RC4.
|
|
|
|
*
|
2013-12-11 18:33:18 +01:00
|
|
|
* @package Crypt_RC4
|
2007-07-02 06:19:55 +02:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
2013-12-03 19:34:41 +01:00
|
|
|
class Crypt_RC4 extends Crypt_Base
|
|
|
|
{
|
2007-07-02 06:19:55 +02:00
|
|
|
/**
|
2013-05-20 08:19:38 +02:00
|
|
|
* Block Length of the cipher
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2014-01-18 18:29:25 +01:00
|
|
|
* RC4 is a stream cipher
|
2013-05-20 08:19:38 +02:00
|
|
|
* so we the block_size to 0
|
|
|
|
*
|
|
|
|
* @see Crypt_Base::block_size
|
2015-09-02 01:37:54 +02:00
|
|
|
* @var int
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
var $block_size = 0;
|
2007-07-02 06:19:55 +02:00
|
|
|
|
|
|
|
/**
|
2015-10-05 04:06:17 +02:00
|
|
|
* Key Length (in bytes)
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2015-09-17 17:46:45 +02:00
|
|
|
* @see Crypt_RC4::setKeyLength()
|
2015-09-02 01:37:54 +02:00
|
|
|
* @var int
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2015-10-05 04:06:17 +02:00
|
|
|
var $key_length = 128; // = 1024 bits
|
2007-07-02 06:19:55 +02:00
|
|
|
|
|
|
|
/**
|
2013-05-20 08:19:38 +02:00
|
|
|
* The namespace used by the cipher for its constants.
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_Base::const_namespace
|
2015-09-02 01:37:54 +02:00
|
|
|
* @var string
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
var $const_namespace = 'RC4';
|
|
|
|
|
2007-07-02 06:19:55 +02:00
|
|
|
/**
|
2013-05-20 08:19:38 +02:00
|
|
|
* The mcrypt specific name of the cipher
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_Base::cipher_name_mcrypt
|
2015-09-02 01:37:54 +02:00
|
|
|
* @var string
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
var $cipher_name_mcrypt = 'arcfour';
|
2007-07-02 06:19:55 +02:00
|
|
|
|
|
|
|
/**
|
2013-05-20 08:19:38 +02:00
|
|
|
* Holds whether performance-optimized $inline_crypt() can/should be used.
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_Base::inline_crypt
|
|
|
|
* @var mixed
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
var $use_inline_crypt = false; // currently not available
|
2007-07-02 06:19:55 +02:00
|
|
|
|
|
|
|
/**
|
2013-05-20 08:19:38 +02:00
|
|
|
* The Key
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::setKey()
|
2015-09-02 01:37:54 +02:00
|
|
|
* @var string
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
var $key = "\0";
|
2007-07-02 06:19:55 +02:00
|
|
|
|
2011-05-08 05:09:11 +02:00
|
|
|
/**
|
2013-05-20 08:19:38 +02:00
|
|
|
* The Key Stream for decryption and encryption
|
2011-05-08 05:09:11 +02:00
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::setKey()
|
2015-09-02 01:37:54 +02:00
|
|
|
* @var array
|
2011-05-08 05:09:11 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
var $stream;
|
2011-05-08 05:09:11 +02:00
|
|
|
|
2007-07-02 06:19:55 +02:00
|
|
|
/**
|
|
|
|
* Default Constructor.
|
|
|
|
*
|
|
|
|
* Determines whether or not the mcrypt extension should be used.
|
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_Base::Crypt_Base()
|
2007-07-02 06:19:55 +02:00
|
|
|
* @return Crypt_RC4
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function Crypt_RC4()
|
|
|
|
{
|
2013-05-20 08:19:38 +02:00
|
|
|
parent::Crypt_Base(CRYPT_MODE_STREAM);
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
|
|
|
|
2014-11-29 14:39:21 +01:00
|
|
|
/**
|
|
|
|
* Test for engine validity
|
|
|
|
*
|
|
|
|
* This is mainly just a wrapper to set things up for Crypt_Base::isValidEngine()
|
|
|
|
*
|
|
|
|
* @see Crypt_Base::Crypt_Base()
|
2015-09-02 01:37:54 +02:00
|
|
|
* @param int $engine
|
2014-11-29 14:39:21 +01:00
|
|
|
* @access public
|
2015-09-02 01:37:54 +02:00
|
|
|
* @return bool
|
2014-11-29 14:39:21 +01:00
|
|
|
*/
|
|
|
|
function isValidEngine($engine)
|
|
|
|
{
|
|
|
|
switch ($engine) {
|
2014-12-13 20:58:41 +01:00
|
|
|
case CRYPT_ENGINE_OPENSSL:
|
2014-11-29 14:39:21 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-07-23 07:21:39 +02: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}
|
|
|
|
*
|
2015-09-02 01:37:54 +02:00
|
|
|
* @param string $iv
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::setKey()
|
2007-07-23 07:21:39 +02:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function setIV($iv)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-25 04:05:24 +02:00
|
|
|
/**
|
2015-09-17 17:46:45 +02:00
|
|
|
* Sets the key length
|
2013-05-25 04:05:24 +02:00
|
|
|
*
|
2015-09-17 17:46:45 +02:00
|
|
|
* Keys can be between 1 and 256 bytes long.
|
2013-05-25 04:05:24 +02:00
|
|
|
*
|
|
|
|
* @access public
|
2015-09-17 17:46:45 +02:00
|
|
|
* @param int $length
|
2013-05-25 04:05:24 +02:00
|
|
|
*/
|
2015-09-17 17:46:45 +02:00
|
|
|
function setKeyLength($length)
|
2013-05-25 04:05:24 +02:00
|
|
|
{
|
2015-09-17 17:46:45 +02:00
|
|
|
if ($length < 8) {
|
2015-10-05 04:06:17 +02:00
|
|
|
$this->key_length = 1;
|
2015-09-17 17:46:45 +02:00
|
|
|
} elseif ($length > 2048) {
|
2016-03-18 18:00:41 +01:00
|
|
|
$this->key_length = 256;
|
2015-09-17 17:46:45 +02:00
|
|
|
} else {
|
2015-10-05 04:06:17 +02:00
|
|
|
$this->key_length = $length >> 3;
|
2015-09-17 17:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
parent::setKeyLength($length);
|
2013-05-25 04:05:24 +02:00
|
|
|
}
|
|
|
|
|
2007-07-02 06:19:55 +02:00
|
|
|
/**
|
|
|
|
* Encrypts a message.
|
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_Base::decrypt()
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::_crypt()
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access public
|
2015-09-02 01:37:54 +02:00
|
|
|
* @param string $plaintext
|
|
|
|
* @return string $ciphertext
|
2007-07-02 06:19:55 +02:00
|
|
|
*/
|
2009-05-27 18:15:23 +02:00
|
|
|
function encrypt($plaintext)
|
|
|
|
{
|
2014-12-31 06:33:12 +01:00
|
|
|
if ($this->engine != CRYPT_ENGINE_INTERNAL) {
|
2013-05-20 08:19:38 +02:00
|
|
|
return parent::encrypt($plaintext);
|
|
|
|
}
|
2007-07-02 06:19:55 +02:00
|
|
|
return $this->_crypt($plaintext, CRYPT_RC4_ENCRYPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrypts a message.
|
|
|
|
*
|
|
|
|
* $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)).
|
2014-03-06 00:41:20 +01:00
|
|
|
* At least if the continuous buffer is disabled.
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_Base::encrypt()
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::_crypt()
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access public
|
2015-09-02 01:37:54 +02:00
|
|
|
* @param string $ciphertext
|
|
|
|
* @return string $plaintext
|
2007-07-02 06:19:55 +02:00
|
|
|
*/
|
2009-05-27 18:15:23 +02:00
|
|
|
function decrypt($ciphertext)
|
|
|
|
{
|
2014-12-31 06:33:12 +01:00
|
|
|
if ($this->engine != CRYPT_ENGINE_INTERNAL) {
|
2013-05-20 08:19:38 +02:00
|
|
|
return parent::decrypt($ciphertext);
|
|
|
|
}
|
2007-07-02 06:19:55 +02:00
|
|
|
return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT);
|
|
|
|
}
|
|
|
|
|
2013-05-25 04:05:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the key (expansion)
|
|
|
|
*
|
|
|
|
* @see Crypt_Base::_setupKey()
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _setupKey()
|
|
|
|
{
|
|
|
|
$key = $this->key;
|
|
|
|
$keyLength = strlen($key);
|
2014-06-16 16:19:14 +02:00
|
|
|
$keyStream = range(0, 255);
|
2013-05-25 04:05:24 +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();
|
|
|
|
$this->stream[CRYPT_RC4_DECRYPT] = $this->stream[CRYPT_RC4_ENCRYPT] = array(
|
|
|
|
0, // index $i
|
|
|
|
0, // index $j
|
|
|
|
$keyStream
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-07-02 06:19:55 +02:00
|
|
|
/**
|
|
|
|
* Encrypts or decrypts a message.
|
|
|
|
*
|
2015-10-11 19:22:07 +02:00
|
|
|
* @see self::encrypt()
|
|
|
|
* @see self::decrypt()
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access private
|
2015-09-02 01:37:54 +02:00
|
|
|
* @param string $text
|
|
|
|
* @param int $mode
|
|
|
|
* @return string $text
|
2007-07-02 06:19:55 +02:00
|
|
|
*/
|
2009-05-27 18:15:23 +02:00
|
|
|
function _crypt($text, $mode)
|
|
|
|
{
|
2013-05-20 08:19:38 +02:00
|
|
|
if ($this->changed) {
|
|
|
|
$this->_setup();
|
|
|
|
$this->changed = false;
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
$stream = &$this->stream[$mode];
|
2007-07-02 06:19:55 +02:00
|
|
|
if ($this->continuousBuffer) {
|
2013-05-20 08:19:38 +02:00
|
|
|
$i = &$stream[0];
|
|
|
|
$j = &$stream[1];
|
|
|
|
$keyStream = &$stream[2];
|
|
|
|
} else {
|
|
|
|
$i = $stream[0];
|
|
|
|
$j = $stream[1];
|
|
|
|
$keyStream = $stream[2];
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
$len = strlen($text);
|
|
|
|
for ($k = 0; $k < $len; ++$k) {
|
|
|
|
$i = ($i + 1) & 255;
|
|
|
|
$ksi = $keyStream[$i];
|
|
|
|
$j = ($j + $ksi) & 255;
|
|
|
|
$ksj = $keyStream[$j];
|
2007-07-23 07:21:39 +02:00
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
$keyStream[$i] = $ksj;
|
|
|
|
$keyStream[$j] = $ksi;
|
2014-06-16 16:19:14 +02:00
|
|
|
$text[$k] = $text[$k] ^ chr($keyStream[($ksj + $ksi) & 255]);
|
2007-07-23 07:21:39 +02:00
|
|
|
}
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
return $text;
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
2012-06-11 10:34:07 +02:00
|
|
|
}
|