Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../vendor/phpsecli.../phpsecli.../phpsecli.../Crypt
File: RC4.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Pure-PHP implementation of RC4.
[3] Fix | Delete
*
[4] Fix | Delete
* Uses mcrypt, if available, and an internal implementation, otherwise.
[5] Fix | Delete
*
[6] Fix | Delete
* PHP versions 4 and 5
[7] Fix | Delete
*
[8] Fix | Delete
* Useful resources are as follows:
[9] Fix | Delete
*
[10] Fix | Delete
* - {@link http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt ARCFOUR Algorithm}
[11] Fix | Delete
* - {@link http://en.wikipedia.org/wiki/RC4 - Wikipedia: RC4}
[12] Fix | Delete
*
[13] Fix | Delete
* RC4 is also known as ARCFOUR or ARC4. The reason is elaborated upon at Wikipedia. This class is named RC4 and not
[14] Fix | Delete
* ARCFOUR or ARC4 because RC4 is how it is referred to in the SSH1 specification.
[15] Fix | Delete
*
[16] Fix | Delete
* Here's a short example of how to use this library:
[17] Fix | Delete
* <code>
[18] Fix | Delete
* <?php
[19] Fix | Delete
* include 'Crypt/RC4.php';
[20] Fix | Delete
*
[21] Fix | Delete
* $rc4 = new Crypt_RC4();
[22] Fix | Delete
*
[23] Fix | Delete
* $rc4->setKey('abcdefgh');
[24] Fix | Delete
*
[25] Fix | Delete
* $size = 10 * 1024;
[26] Fix | Delete
* $plaintext = '';
[27] Fix | Delete
* for ($i = 0; $i < $size; $i++) {
[28] Fix | Delete
* $plaintext.= 'a';
[29] Fix | Delete
* }
[30] Fix | Delete
*
[31] Fix | Delete
* echo $rc4->decrypt($rc4->encrypt($plaintext));
[32] Fix | Delete
* ?>
[33] Fix | Delete
* </code>
[34] Fix | Delete
*
[35] Fix | Delete
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
[36] Fix | Delete
* of this software and associated documentation files (the "Software"), to deal
[37] Fix | Delete
* in the Software without restriction, including without limitation the rights
[38] Fix | Delete
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
[39] Fix | Delete
* copies of the Software, and to permit persons to whom the Software is
[40] Fix | Delete
* furnished to do so, subject to the following conditions:
[41] Fix | Delete
*
[42] Fix | Delete
* The above copyright notice and this permission notice shall be included in
[43] Fix | Delete
* all copies or substantial portions of the Software.
[44] Fix | Delete
*
[45] Fix | Delete
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
[46] Fix | Delete
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
[47] Fix | Delete
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
[48] Fix | Delete
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
[49] Fix | Delete
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
[50] Fix | Delete
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
[51] Fix | Delete
* THE SOFTWARE.
[52] Fix | Delete
*
[53] Fix | Delete
* @category Crypt
[54] Fix | Delete
* @package Crypt_RC4
[55] Fix | Delete
* @author Jim Wigginton <terrafrost@php.net>
[56] Fix | Delete
* @copyright 2007 Jim Wigginton
[57] Fix | Delete
* @license http://www.opensource.org/licenses/mit-license.html MIT License
[58] Fix | Delete
* @link http://phpseclib.sourceforge.net
[59] Fix | Delete
*/
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* Include Crypt_Base
[63] Fix | Delete
*
[64] Fix | Delete
* Base cipher class
[65] Fix | Delete
*/
[66] Fix | Delete
if (!class_exists('Crypt_Base')) {
[67] Fix | Delete
include_once 'Base.php';
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
/**#@+
[71] Fix | Delete
* @access private
[72] Fix | Delete
* @see self::_crypt()
[73] Fix | Delete
*/
[74] Fix | Delete
define('CRYPT_RC4_ENCRYPT', 0);
[75] Fix | Delete
define('CRYPT_RC4_DECRYPT', 1);
[76] Fix | Delete
/**#@-*/
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* Pure-PHP implementation of RC4.
[80] Fix | Delete
*
[81] Fix | Delete
* @package Crypt_RC4
[82] Fix | Delete
* @author Jim Wigginton <terrafrost@php.net>
[83] Fix | Delete
* @access public
[84] Fix | Delete
*/
[85] Fix | Delete
class Crypt_RC4 extends Crypt_Base
[86] Fix | Delete
{
[87] Fix | Delete
/**
[88] Fix | Delete
* Block Length of the cipher
[89] Fix | Delete
*
[90] Fix | Delete
* RC4 is a stream cipher
[91] Fix | Delete
* so we the block_size to 0
[92] Fix | Delete
*
[93] Fix | Delete
* @see Crypt_Base::block_size
[94] Fix | Delete
* @var int
[95] Fix | Delete
* @access private
[96] Fix | Delete
*/
[97] Fix | Delete
var $block_size = 0;
[98] Fix | Delete
[99] Fix | Delete
/**
[100] Fix | Delete
* Key Length (in bytes)
[101] Fix | Delete
*
[102] Fix | Delete
* @see Crypt_RC4::setKeyLength()
[103] Fix | Delete
* @var int
[104] Fix | Delete
* @access private
[105] Fix | Delete
*/
[106] Fix | Delete
var $key_length = 128; // = 1024 bits
[107] Fix | Delete
[108] Fix | Delete
/**
[109] Fix | Delete
* The namespace used by the cipher for its constants.
[110] Fix | Delete
*
[111] Fix | Delete
* @see Crypt_Base::const_namespace
[112] Fix | Delete
* @var string
[113] Fix | Delete
* @access private
[114] Fix | Delete
*/
[115] Fix | Delete
var $const_namespace = 'RC4';
[116] Fix | Delete
[117] Fix | Delete
/**
[118] Fix | Delete
* The mcrypt specific name of the cipher
[119] Fix | Delete
*
[120] Fix | Delete
* @see Crypt_Base::cipher_name_mcrypt
[121] Fix | Delete
* @var string
[122] Fix | Delete
* @access private
[123] Fix | Delete
*/
[124] Fix | Delete
var $cipher_name_mcrypt = 'arcfour';
[125] Fix | Delete
[126] Fix | Delete
/**
[127] Fix | Delete
* Holds whether performance-optimized $inline_crypt() can/should be used.
[128] Fix | Delete
*
[129] Fix | Delete
* @see Crypt_Base::inline_crypt
[130] Fix | Delete
* @var mixed
[131] Fix | Delete
* @access private
[132] Fix | Delete
*/
[133] Fix | Delete
var $use_inline_crypt = false; // currently not available
[134] Fix | Delete
[135] Fix | Delete
/**
[136] Fix | Delete
* The Key
[137] Fix | Delete
*
[138] Fix | Delete
* @see self::setKey()
[139] Fix | Delete
* @var string
[140] Fix | Delete
* @access private
[141] Fix | Delete
*/
[142] Fix | Delete
var $key;
[143] Fix | Delete
[144] Fix | Delete
/**
[145] Fix | Delete
* The Key Stream for decryption and encryption
[146] Fix | Delete
*
[147] Fix | Delete
* @see self::setKey()
[148] Fix | Delete
* @var array
[149] Fix | Delete
* @access private
[150] Fix | Delete
*/
[151] Fix | Delete
var $stream;
[152] Fix | Delete
[153] Fix | Delete
/**
[154] Fix | Delete
* Default Constructor.
[155] Fix | Delete
*
[156] Fix | Delete
* Determines whether or not the mcrypt extension should be used.
[157] Fix | Delete
*
[158] Fix | Delete
* @see Crypt_Base::Crypt_Base()
[159] Fix | Delete
* @return Crypt_RC4
[160] Fix | Delete
* @access public
[161] Fix | Delete
*/
[162] Fix | Delete
function __construct()
[163] Fix | Delete
{
[164] Fix | Delete
parent::__construct(CRYPT_MODE_STREAM);
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
/**
[168] Fix | Delete
* PHP4 compatible Default Constructor.
[169] Fix | Delete
*
[170] Fix | Delete
* @see self::__construct()
[171] Fix | Delete
* @access public
[172] Fix | Delete
*/
[173] Fix | Delete
function Crypt_RC4()
[174] Fix | Delete
{
[175] Fix | Delete
$this->__construct();
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
/**
[179] Fix | Delete
* Test for engine validity
[180] Fix | Delete
*
[181] Fix | Delete
* This is mainly just a wrapper to set things up for Crypt_Base::isValidEngine()
[182] Fix | Delete
*
[183] Fix | Delete
* @see Crypt_Base::Crypt_Base()
[184] Fix | Delete
* @param int $engine
[185] Fix | Delete
* @access public
[186] Fix | Delete
* @return bool
[187] Fix | Delete
*/
[188] Fix | Delete
function isValidEngine($engine)
[189] Fix | Delete
{
[190] Fix | Delete
if ($engine == CRYPT_ENGINE_OPENSSL) {
[191] Fix | Delete
if (version_compare(PHP_VERSION, '5.3.7') >= 0) {
[192] Fix | Delete
$this->cipher_name_openssl = 'rc4-40';
[193] Fix | Delete
} else {
[194] Fix | Delete
switch (strlen($this->key)) {
[195] Fix | Delete
case 5:
[196] Fix | Delete
$this->cipher_name_openssl = 'rc4-40';
[197] Fix | Delete
break;
[198] Fix | Delete
case 8:
[199] Fix | Delete
$this->cipher_name_openssl = 'rc4-64';
[200] Fix | Delete
break;
[201] Fix | Delete
case 16:
[202] Fix | Delete
$this->cipher_name_openssl = 'rc4';
[203] Fix | Delete
break;
[204] Fix | Delete
default:
[205] Fix | Delete
return false;
[206] Fix | Delete
}
[207] Fix | Delete
}
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
return parent::isValidEngine($engine);
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
/**
[214] Fix | Delete
* Dummy function.
[215] Fix | Delete
*
[216] Fix | Delete
* Some protocols, such as WEP, prepend an "initialization vector" to the key, effectively creating a new key [1].
[217] Fix | Delete
* If you need to use an initialization vector in this manner, feel free to prepend it to the key, yourself, before
[218] Fix | Delete
* calling setKey().
[219] Fix | Delete
*
[220] Fix | Delete
* [1] WEP's initialization vectors (IV's) are used in a somewhat insecure way. Since, in that protocol,
[221] Fix | Delete
* the IV's are relatively easy to predict, an attack described by
[222] Fix | Delete
* {@link http://www.drizzle.com/~aboba/IEEE/rc4_ksaproc.pdf Scott Fluhrer, Itsik Mantin, and Adi Shamir}
[223] Fix | Delete
* can be used to quickly guess at the rest of the key. The following links elaborate:
[224] Fix | Delete
*
[225] Fix | Delete
* {@link http://www.rsa.com/rsalabs/node.asp?id=2009 http://www.rsa.com/rsalabs/node.asp?id=2009}
[226] Fix | Delete
* {@link http://en.wikipedia.org/wiki/Related_key_attack http://en.wikipedia.org/wiki/Related_key_attack}
[227] Fix | Delete
*
[228] Fix | Delete
* @param string $iv
[229] Fix | Delete
* @see self::setKey()
[230] Fix | Delete
* @access public
[231] Fix | Delete
*/
[232] Fix | Delete
function setIV($iv)
[233] Fix | Delete
{
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
/**
[237] Fix | Delete
* Sets the key length
[238] Fix | Delete
*
[239] Fix | Delete
* Keys can be between 1 and 256 bytes long.
[240] Fix | Delete
*
[241] Fix | Delete
* @access public
[242] Fix | Delete
* @param int $length
[243] Fix | Delete
*/
[244] Fix | Delete
function setKeyLength($length)
[245] Fix | Delete
{
[246] Fix | Delete
if ($length < 8) {
[247] Fix | Delete
$this->key_length = 1;
[248] Fix | Delete
} elseif ($length > 2048) {
[249] Fix | Delete
$this->key_length = 256;
[250] Fix | Delete
} else {
[251] Fix | Delete
$this->key_length = $length >> 3;
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
parent::setKeyLength($length);
[255] Fix | Delete
}
[256] Fix | Delete
[257] Fix | Delete
/**
[258] Fix | Delete
* Encrypts a message.
[259] Fix | Delete
*
[260] Fix | Delete
* @see Crypt_Base::decrypt()
[261] Fix | Delete
* @see self::_crypt()
[262] Fix | Delete
* @access public
[263] Fix | Delete
* @param string $plaintext
[264] Fix | Delete
* @return string $ciphertext
[265] Fix | Delete
*/
[266] Fix | Delete
function encrypt($plaintext)
[267] Fix | Delete
{
[268] Fix | Delete
if ($this->engine != CRYPT_ENGINE_INTERNAL) {
[269] Fix | Delete
return parent::encrypt($plaintext);
[270] Fix | Delete
}
[271] Fix | Delete
return $this->_crypt($plaintext, CRYPT_RC4_ENCRYPT);
[272] Fix | Delete
}
[273] Fix | Delete
[274] Fix | Delete
/**
[275] Fix | Delete
* Decrypts a message.
[276] Fix | Delete
*
[277] Fix | Delete
* $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)).
[278] Fix | Delete
* At least if the continuous buffer is disabled.
[279] Fix | Delete
*
[280] Fix | Delete
* @see Crypt_Base::encrypt()
[281] Fix | Delete
* @see self::_crypt()
[282] Fix | Delete
* @access public
[283] Fix | Delete
* @param string $ciphertext
[284] Fix | Delete
* @return string $plaintext
[285] Fix | Delete
*/
[286] Fix | Delete
function decrypt($ciphertext)
[287] Fix | Delete
{
[288] Fix | Delete
if ($this->engine != CRYPT_ENGINE_INTERNAL) {
[289] Fix | Delete
return parent::decrypt($ciphertext);
[290] Fix | Delete
}
[291] Fix | Delete
return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT);
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
[295] Fix | Delete
/**
[296] Fix | Delete
* Setup the key (expansion)
[297] Fix | Delete
*
[298] Fix | Delete
* @see Crypt_Base::_setupKey()
[299] Fix | Delete
* @access private
[300] Fix | Delete
*/
[301] Fix | Delete
function _setupKey()
[302] Fix | Delete
{
[303] Fix | Delete
$key = $this->key;
[304] Fix | Delete
$keyLength = strlen($key);
[305] Fix | Delete
$keyStream = range(0, 255);
[306] Fix | Delete
$j = 0;
[307] Fix | Delete
for ($i = 0; $i < 256; $i++) {
[308] Fix | Delete
$j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
[309] Fix | Delete
$temp = $keyStream[$i];
[310] Fix | Delete
$keyStream[$i] = $keyStream[$j];
[311] Fix | Delete
$keyStream[$j] = $temp;
[312] Fix | Delete
}
[313] Fix | Delete
[314] Fix | Delete
$this->stream = array();
[315] Fix | Delete
$this->stream[CRYPT_RC4_DECRYPT] = $this->stream[CRYPT_RC4_ENCRYPT] = array(
[316] Fix | Delete
0, // index $i
[317] Fix | Delete
0, // index $j
[318] Fix | Delete
$keyStream
[319] Fix | Delete
);
[320] Fix | Delete
}
[321] Fix | Delete
[322] Fix | Delete
/**
[323] Fix | Delete
* Encrypts or decrypts a message.
[324] Fix | Delete
*
[325] Fix | Delete
* @see self::encrypt()
[326] Fix | Delete
* @see self::decrypt()
[327] Fix | Delete
* @access private
[328] Fix | Delete
* @param string $text
[329] Fix | Delete
* @param int $mode
[330] Fix | Delete
* @return string $text
[331] Fix | Delete
*/
[332] Fix | Delete
function _crypt($text, $mode)
[333] Fix | Delete
{
[334] Fix | Delete
if ($this->changed) {
[335] Fix | Delete
$this->_setup();
[336] Fix | Delete
$this->changed = false;
[337] Fix | Delete
}
[338] Fix | Delete
[339] Fix | Delete
$stream = &$this->stream[$mode];
[340] Fix | Delete
if ($this->continuousBuffer) {
[341] Fix | Delete
$i = &$stream[0];
[342] Fix | Delete
$j = &$stream[1];
[343] Fix | Delete
$keyStream = &$stream[2];
[344] Fix | Delete
} else {
[345] Fix | Delete
$i = $stream[0];
[346] Fix | Delete
$j = $stream[1];
[347] Fix | Delete
$keyStream = $stream[2];
[348] Fix | Delete
}
[349] Fix | Delete
[350] Fix | Delete
$len = strlen($text);
[351] Fix | Delete
for ($k = 0; $k < $len; ++$k) {
[352] Fix | Delete
$i = ($i + 1) & 255;
[353] Fix | Delete
$ksi = $keyStream[$i];
[354] Fix | Delete
$j = ($j + $ksi) & 255;
[355] Fix | Delete
$ksj = $keyStream[$j];
[356] Fix | Delete
[357] Fix | Delete
$keyStream[$i] = $ksj;
[358] Fix | Delete
$keyStream[$j] = $ksi;
[359] Fix | Delete
$text[$k] = $text[$k] ^ chr($keyStream[($ksj + $ksi) & 255]);
[360] Fix | Delete
}
[361] Fix | Delete
[362] Fix | Delete
return $text;
[363] Fix | Delete
}
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function