Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/worker/src/PHPSecLi.../Crypt
File: TripleDES.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Pure-PHP implementation of Triple DES.
[3] Fix | Delete
*
[4] Fix | Delete
* Uses mcrypt, if available, and an internal implementation, otherwise. Operates in the EDE3 mode (encrypt-decrypt-encrypt).
[5] Fix | Delete
*
[6] Fix | Delete
* PHP versions 4 and 5
[7] Fix | Delete
*
[8] Fix | Delete
* Here's a short example of how to use this library:
[9] Fix | Delete
* <code>
[10] Fix | Delete
* <?php
[11] Fix | Delete
* include 'Crypt/TripleDES.php';
[12] Fix | Delete
*
[13] Fix | Delete
* $des = new Crypt_TripleDES();
[14] Fix | Delete
*
[15] Fix | Delete
* $des->setKey('abcdefghijklmnopqrstuvwx');
[16] Fix | Delete
*
[17] Fix | Delete
* $size = 10 * 1024;
[18] Fix | Delete
* $plaintext = '';
[19] Fix | Delete
* for ($i = 0; $i < $size; $i++) {
[20] Fix | Delete
* $plaintext.= 'a';
[21] Fix | Delete
* }
[22] Fix | Delete
*
[23] Fix | Delete
* echo $des->decrypt($des->encrypt($plaintext));
[24] Fix | Delete
* ?>
[25] Fix | Delete
* </code>
[26] Fix | Delete
*
[27] Fix | Delete
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
[28] Fix | Delete
* of this software and associated documentation files (the "Software"), to deal
[29] Fix | Delete
* in the Software without restriction, including without limitation the rights
[30] Fix | Delete
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
[31] Fix | Delete
* copies of the Software, and to permit persons to whom the Software is
[32] Fix | Delete
* furnished to do so, subject to the following conditions:
[33] Fix | Delete
*
[34] Fix | Delete
* The above copyright notice and this permission notice shall be included in
[35] Fix | Delete
* all copies or substantial portions of the Software.
[36] Fix | Delete
*
[37] Fix | Delete
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
[38] Fix | Delete
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
[39] Fix | Delete
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
[40] Fix | Delete
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
[41] Fix | Delete
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
[42] Fix | Delete
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
[43] Fix | Delete
* THE SOFTWARE.
[44] Fix | Delete
*
[45] Fix | Delete
* @category Crypt
[46] Fix | Delete
* @package Crypt_TripleDES
[47] Fix | Delete
* @author Jim Wigginton <terrafrost@php.net>
[48] Fix | Delete
* @copyright MMVII Jim Wigginton
[49] Fix | Delete
* @license http://www.opensource.org/licenses/mit-license.html MIT License
[50] Fix | Delete
* @link http://phpseclib.sourceforge.net
[51] Fix | Delete
*/
[52] Fix | Delete
[53] Fix | Delete
/**
[54] Fix | Delete
* Include Crypt_DES
[55] Fix | Delete
*/
[56] Fix | Delete
if (!class_exists('Crypt_DES')) {
[57] Fix | Delete
require_once dirname(__FILE__).'/DES.php';
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* Encrypt / decrypt using inner chaining
[62] Fix | Delete
*
[63] Fix | Delete
* Inner chaining is used by SSH-1 and is generally considered to be less secure then outer chaining (CRYPT_DES_MODE_CBC3).
[64] Fix | Delete
*/
[65] Fix | Delete
define('CRYPT_DES_MODE_3CBC', -2);
[66] Fix | Delete
[67] Fix | Delete
/**
[68] Fix | Delete
* Encrypt / decrypt using outer chaining
[69] Fix | Delete
*
[70] Fix | Delete
* Outer chaining is used by SSH-2 and when the mode is set to CRYPT_DES_MODE_CBC.
[71] Fix | Delete
*/
[72] Fix | Delete
define('CRYPT_DES_MODE_CBC3', CRYPT_DES_MODE_CBC);
[73] Fix | Delete
[74] Fix | Delete
/**
[75] Fix | Delete
* Pure-PHP implementation of Triple DES.
[76] Fix | Delete
*
[77] Fix | Delete
* @package Crypt_TripleDES
[78] Fix | Delete
* @author Jim Wigginton <terrafrost@php.net>
[79] Fix | Delete
* @access public
[80] Fix | Delete
*/
[81] Fix | Delete
class Crypt_TripleDES extends Crypt_DES
[82] Fix | Delete
{
[83] Fix | Delete
/**
[84] Fix | Delete
* The default password key_size used by setPassword()
[85] Fix | Delete
*
[86] Fix | Delete
* @see Crypt_DES::password_key_size
[87] Fix | Delete
* @see Crypt_Base::password_key_size
[88] Fix | Delete
* @see Crypt_Base::setPassword()
[89] Fix | Delete
* @var Integer
[90] Fix | Delete
* @access private
[91] Fix | Delete
*/
[92] Fix | Delete
public $password_key_size = 24;
[93] Fix | Delete
[94] Fix | Delete
/**
[95] Fix | Delete
* The default salt used by setPassword()
[96] Fix | Delete
*
[97] Fix | Delete
* @see Crypt_Base::password_default_salt
[98] Fix | Delete
* @see Crypt_Base::setPassword()
[99] Fix | Delete
* @var String
[100] Fix | Delete
* @access private
[101] Fix | Delete
*/
[102] Fix | Delete
public $password_default_salt = 'phpseclib';
[103] Fix | Delete
[104] Fix | Delete
/**
[105] Fix | Delete
* The namespace used by the cipher for its constants.
[106] Fix | Delete
*
[107] Fix | Delete
* @see Crypt_DES::const_namespace
[108] Fix | Delete
* @see Crypt_Base::const_namespace
[109] Fix | Delete
* @var String
[110] Fix | Delete
* @access private
[111] Fix | Delete
*/
[112] Fix | Delete
public $const_namespace = 'DES';
[113] Fix | Delete
[114] Fix | Delete
/**
[115] Fix | Delete
* The mcrypt specific name of the cipher
[116] Fix | Delete
*
[117] Fix | Delete
* @see Crypt_DES::cipher_name_mcrypt
[118] Fix | Delete
* @see Crypt_Base::cipher_name_mcrypt
[119] Fix | Delete
* @var String
[120] Fix | Delete
* @access private
[121] Fix | Delete
*/
[122] Fix | Delete
public $cipher_name_mcrypt = 'tripledes';
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* Optimizing value while CFB-encrypting
[126] Fix | Delete
*
[127] Fix | Delete
* @see Crypt_Base::cfb_init_len
[128] Fix | Delete
* @var Integer
[129] Fix | Delete
* @access private
[130] Fix | Delete
*/
[131] Fix | Delete
public $cfb_init_len = 750;
[132] Fix | Delete
[133] Fix | Delete
/**
[134] Fix | Delete
* max possible size of $key
[135] Fix | Delete
*
[136] Fix | Delete
* @see Crypt_TripleDES::setKey()
[137] Fix | Delete
* @see Crypt_DES::setKey()
[138] Fix | Delete
* @var String
[139] Fix | Delete
* @access private
[140] Fix | Delete
*/
[141] Fix | Delete
public $key_size_max = 24;
[142] Fix | Delete
[143] Fix | Delete
/**
[144] Fix | Delete
* Internal flag whether using CRYPT_DES_MODE_3CBC or not
[145] Fix | Delete
*
[146] Fix | Delete
* @var Boolean
[147] Fix | Delete
* @access private
[148] Fix | Delete
*/
[149] Fix | Delete
public $mode_3cbc;
[150] Fix | Delete
[151] Fix | Delete
/**
[152] Fix | Delete
* The Crypt_DES objects
[153] Fix | Delete
*
[154] Fix | Delete
* Used only if $mode_3cbc === true
[155] Fix | Delete
*
[156] Fix | Delete
* @var Array
[157] Fix | Delete
* @access private
[158] Fix | Delete
*/
[159] Fix | Delete
public $des;
[160] Fix | Delete
[161] Fix | Delete
/**
[162] Fix | Delete
* Default Constructor.
[163] Fix | Delete
*
[164] Fix | Delete
* Determines whether or not the mcrypt extension should be used.
[165] Fix | Delete
*
[166] Fix | Delete
* $mode could be:
[167] Fix | Delete
*
[168] Fix | Delete
* - CRYPT_DES_MODE_ECB
[169] Fix | Delete
*
[170] Fix | Delete
* - CRYPT_DES_MODE_CBC
[171] Fix | Delete
*
[172] Fix | Delete
* - CRYPT_DES_MODE_CTR
[173] Fix | Delete
*
[174] Fix | Delete
* - CRYPT_DES_MODE_CFB
[175] Fix | Delete
*
[176] Fix | Delete
* - CRYPT_DES_MODE_OFB
[177] Fix | Delete
*
[178] Fix | Delete
* - CRYPT_DES_MODE_3CBC
[179] Fix | Delete
*
[180] Fix | Delete
* If not explicitly set, CRYPT_DES_MODE_CBC will be used.
[181] Fix | Delete
*
[182] Fix | Delete
* @see Crypt_DES::Crypt_DES()
[183] Fix | Delete
* @see Crypt_Base::Crypt_Base()
[184] Fix | Delete
*
[185] Fix | Delete
* @param optional Integer $mode
[186] Fix | Delete
*
[187] Fix | Delete
* @access public
[188] Fix | Delete
*/
[189] Fix | Delete
public function __construct($mode = CRYPT_DES_MODE_CBC)
[190] Fix | Delete
{
[191] Fix | Delete
switch ($mode) {
[192] Fix | Delete
// In case of CRYPT_DES_MODE_3CBC, we init as CRYPT_DES_MODE_CBC
[193] Fix | Delete
// and additional flag us internally as 3CBC
[194] Fix | Delete
case CRYPT_DES_MODE_3CBC:
[195] Fix | Delete
parent::__construct(CRYPT_DES_MODE_CBC);
[196] Fix | Delete
$this->mode_3cbc = true;
[197] Fix | Delete
[198] Fix | Delete
// This three $des'es will do the 3CBC work (if $key > 64bits)
[199] Fix | Delete
$this->des = array(
[200] Fix | Delete
new Crypt_DES(CRYPT_DES_MODE_CBC),
[201] Fix | Delete
new Crypt_DES(CRYPT_DES_MODE_CBC),
[202] Fix | Delete
new Crypt_DES(CRYPT_DES_MODE_CBC),
[203] Fix | Delete
);
[204] Fix | Delete
[205] Fix | Delete
// we're going to be doing the padding, ourselves, so disable it in the Crypt_DES objects
[206] Fix | Delete
$this->des[0]->disablePadding();
[207] Fix | Delete
$this->des[1]->disablePadding();
[208] Fix | Delete
$this->des[2]->disablePadding();
[209] Fix | Delete
break;
[210] Fix | Delete
// If not 3CBC, we init as usual
[211] Fix | Delete
default:
[212] Fix | Delete
parent::__construct($mode);
[213] Fix | Delete
}
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
/**
[217] Fix | Delete
* Sets the initialization vector. (optional)
[218] Fix | Delete
*
[219] Fix | Delete
* SetIV is not required when CRYPT_DES_MODE_ECB is being used. If not explicitly set, it'll be assumed
[220] Fix | Delete
* to be all zero's.
[221] Fix | Delete
*
[222] Fix | Delete
* @see Crypt_Base::setIV()
[223] Fix | Delete
* @access public
[224] Fix | Delete
*
[225] Fix | Delete
* @param String $iv
[226] Fix | Delete
*/
[227] Fix | Delete
public function setIV($iv)
[228] Fix | Delete
{
[229] Fix | Delete
parent::setIV($iv);
[230] Fix | Delete
if ($this->mode_3cbc) {
[231] Fix | Delete
$this->des[0]->setIV($iv);
[232] Fix | Delete
$this->des[1]->setIV($iv);
[233] Fix | Delete
$this->des[2]->setIV($iv);
[234] Fix | Delete
}
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
/**
[238] Fix | Delete
* Sets the key.
[239] Fix | Delete
*
[240] Fix | Delete
* Keys can be of any length. Triple DES, itself, can use 128-bit (eg. strlen($key) == 16) or
[241] Fix | Delete
* 192-bit (eg. strlen($key) == 24) keys. This function pads and truncates $key as appropriate.
[242] Fix | Delete
*
[243] Fix | Delete
* DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
[244] Fix | Delete
*
[245] Fix | Delete
* If the key is not explicitly set, it'll be assumed to be all null bytes.
[246] Fix | Delete
*
[247] Fix | Delete
* @access public
[248] Fix | Delete
* @see Crypt_DES::setKey()
[249] Fix | Delete
* @see Crypt_Base::setKey()
[250] Fix | Delete
*
[251] Fix | Delete
* @param String $key
[252] Fix | Delete
*/
[253] Fix | Delete
public function setKey($key)
[254] Fix | Delete
{
[255] Fix | Delete
$length = strlen($key);
[256] Fix | Delete
if ($length > 8) {
[257] Fix | Delete
$key = str_pad(substr($key, 0, 24), 24, chr(0));
[258] Fix | Delete
// if $key is between 64 and 128-bits, use the first 64-bits as the last, per this:
[259] Fix | Delete
// http://php.net/function.mcrypt-encrypt#47973
[260] Fix | Delete
//$key = $length <= 16 ? substr_replace($key, substr($key, 0, 8), 16) : substr($key, 0, 24);
[261] Fix | Delete
} else {
[262] Fix | Delete
$key = str_pad($key, 8, chr(0));
[263] Fix | Delete
}
[264] Fix | Delete
parent::setKey($key);
[265] Fix | Delete
[266] Fix | Delete
// And in case of CRYPT_DES_MODE_3CBC:
[267] Fix | Delete
// if key <= 64bits we not need the 3 $des to work,
[268] Fix | Delete
// because we will then act as regular DES-CBC with just a <= 64bit key.
[269] Fix | Delete
// So only if the key > 64bits (> 8 bytes) we will call setKey() for the 3 $des.
[270] Fix | Delete
if ($this->mode_3cbc && $length > 8) {
[271] Fix | Delete
$this->des[0]->setKey(substr($key, 0, 8));
[272] Fix | Delete
$this->des[1]->setKey(substr($key, 8, 8));
[273] Fix | Delete
$this->des[2]->setKey(substr($key, 16, 8));
[274] Fix | Delete
}
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
/**
[278] Fix | Delete
* Encrypts a message.
[279] Fix | Delete
*
[280] Fix | Delete
* @see Crypt_Base::encrypt()
[281] Fix | Delete
* @access public
[282] Fix | Delete
*
[283] Fix | Delete
* @param String $plaintext
[284] Fix | Delete
*
[285] Fix | Delete
* @return String $cipertext
[286] Fix | Delete
*/
[287] Fix | Delete
public function encrypt($plaintext)
[288] Fix | Delete
{
[289] Fix | Delete
// parent::en/decrypt() is able to do all the work for all modes and keylengths,
[290] Fix | Delete
// except for: CRYPT_DES_MODE_3CBC (inner chaining CBC) with a key > 64bits
[291] Fix | Delete
[292] Fix | Delete
// if the key is smaller then 8, do what we'd normally do
[293] Fix | Delete
if ($this->mode_3cbc && strlen($this->key) > 8) {
[294] Fix | Delete
return $this->des[2]->encrypt(
[295] Fix | Delete
$this->des[1]->decrypt(
[296] Fix | Delete
$this->des[0]->encrypt(
[297] Fix | Delete
$this->_pad($plaintext)
[298] Fix | Delete
)
[299] Fix | Delete
)
[300] Fix | Delete
);
[301] Fix | Delete
}
[302] Fix | Delete
[303] Fix | Delete
return parent::encrypt($plaintext);
[304] Fix | Delete
}
[305] Fix | Delete
[306] Fix | Delete
/**
[307] Fix | Delete
* Decrypts a message.
[308] Fix | Delete
*
[309] Fix | Delete
* @see Crypt_Base::decrypt()
[310] Fix | Delete
* @access public
[311] Fix | Delete
*
[312] Fix | Delete
* @param String $ciphertext
[313] Fix | Delete
*
[314] Fix | Delete
* @return String $plaintext
[315] Fix | Delete
*/
[316] Fix | Delete
public function decrypt($ciphertext)
[317] Fix | Delete
{
[318] Fix | Delete
if ($this->mode_3cbc && strlen($this->key) > 8) {
[319] Fix | Delete
return $this->_unpad(
[320] Fix | Delete
$this->des[0]->decrypt(
[321] Fix | Delete
$this->des[1]->encrypt(
[322] Fix | Delete
$this->des[2]->decrypt(
[323] Fix | Delete
str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, "\0")
[324] Fix | Delete
)
[325] Fix | Delete
)
[326] Fix | Delete
)
[327] Fix | Delete
);
[328] Fix | Delete
}
[329] Fix | Delete
[330] Fix | Delete
return parent::decrypt($ciphertext);
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
/**
[334] Fix | Delete
* Treat consecutive "packets" as if they are a continuous buffer.
[335] Fix | Delete
*
[336] Fix | Delete
* Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
[337] Fix | Delete
* will yield different outputs:
[338] Fix | Delete
*
[339] Fix | Delete
* <code>
[340] Fix | Delete
* echo $des->encrypt(substr($plaintext, 0, 8));
[341] Fix | Delete
* echo $des->encrypt(substr($plaintext, 8, 8));
[342] Fix | Delete
* </code>
[343] Fix | Delete
* <code>
[344] Fix | Delete
* echo $des->encrypt($plaintext);
[345] Fix | Delete
* </code>
[346] Fix | Delete
*
[347] Fix | Delete
* The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
[348] Fix | Delete
* another, as demonstrated with the following:
[349] Fix | Delete
*
[350] Fix | Delete
* <code>
[351] Fix | Delete
* $des->encrypt(substr($plaintext, 0, 8));
[352] Fix | Delete
* echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
[353] Fix | Delete
* </code>
[354] Fix | Delete
* <code>
[355] Fix | Delete
* echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
[356] Fix | Delete
* </code>
[357] Fix | Delete
*
[358] Fix | Delete
* With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
[359] Fix | Delete
* outputs. The reason is due to the fact that the initialization vector's change after every encryption /
[360] Fix | Delete
* decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
[361] Fix | Delete
*
[362] Fix | Delete
* Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
[363] Fix | Delete
* encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
[364] Fix | Delete
* continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
[365] Fix | Delete
* however, they are also less intuitive and more likely to cause you problems.
[366] Fix | Delete
*
[367] Fix | Delete
* @see Crypt_Base::enableContinuousBuffer()
[368] Fix | Delete
* @see Crypt_TripleDES::disableContinuousBuffer()
[369] Fix | Delete
* @access public
[370] Fix | Delete
*/
[371] Fix | Delete
public function enableContinuousBuffer()
[372] Fix | Delete
{
[373] Fix | Delete
parent::enableContinuousBuffer();
[374] Fix | Delete
if ($this->mode_3cbc) {
[375] Fix | Delete
$this->des[0]->enableContinuousBuffer();
[376] Fix | Delete
$this->des[1]->enableContinuousBuffer();
[377] Fix | Delete
$this->des[2]->enableContinuousBuffer();
[378] Fix | Delete
}
[379] Fix | Delete
}
[380] Fix | Delete
[381] Fix | Delete
/**
[382] Fix | Delete
* Treat consecutive packets as if they are a discontinuous buffer.
[383] Fix | Delete
*
[384] Fix | Delete
* The default behavior.
[385] Fix | Delete
*
[386] Fix | Delete
* @see Crypt_Base::disableContinuousBuffer()
[387] Fix | Delete
* @see Crypt_TripleDES::enableContinuousBuffer()
[388] Fix | Delete
* @access public
[389] Fix | Delete
*/
[390] Fix | Delete
public function disableContinuousBuffer()
[391] Fix | Delete
{
[392] Fix | Delete
parent::disableContinuousBuffer();
[393] Fix | Delete
if ($this->mode_3cbc) {
[394] Fix | Delete
$this->des[0]->disableContinuousBuffer();
[395] Fix | Delete
$this->des[1]->disableContinuousBuffer();
[396] Fix | Delete
$this->des[2]->disableContinuousBuffer();
[397] Fix | Delete
}
[398] Fix | Delete
}
[399] Fix | Delete
[400] Fix | Delete
/**
[401] Fix | Delete
* Creates the key schedule
[402] Fix | Delete
*
[403] Fix | Delete
* @see Crypt_DES::_setupKey()
[404] Fix | Delete
* @see Crypt_Base::_setupKey()
[405] Fix | Delete
* @access private
[406] Fix | Delete
*/
[407] Fix | Delete
public function _setupKey()
[408] Fix | Delete
{
[409] Fix | Delete
switch (true) {
[410] Fix | Delete
// if $key <= 64bits we configure our internal pure-php cipher engine
[411] Fix | Delete
// to act as regular [1]DES, not as 3DES. mcrypt.so::tripledes does the same.
[412] Fix | Delete
case strlen($this->key) <= 8:
[413] Fix | Delete
$this->des_rounds = 1;
[414] Fix | Delete
break;
[415] Fix | Delete
[416] Fix | Delete
// otherwise, if $key > 64bits, we configure our engine to work as 3DES.
[417] Fix | Delete
default:
[418] Fix | Delete
$this->des_rounds = 3;
[419] Fix | Delete
[420] Fix | Delete
// (only) if 3CBC is used we have, of course, to setup the $des[0-2] keys also separately.
[421] Fix | Delete
if ($this->mode_3cbc) {
[422] Fix | Delete
$this->des[0]->_setupKey();
[423] Fix | Delete
$this->des[1]->_setupKey();
[424] Fix | Delete
$this->des[2]->_setupKey();
[425] Fix | Delete
[426] Fix | Delete
// because $des[0-2] will, now, do all the work we can return here
[427] Fix | Delete
// not need unnecessary stress parent::_setupKey() with our, now unused, $key.
[428] Fix | Delete
return;
[429] Fix | Delete
}
[430] Fix | Delete
}
[431] Fix | Delete
// setup our key
[432] Fix | Delete
parent::_setupKey();
[433] Fix | Delete
}
[434] Fix | Delete
}
[435] Fix | Delete
[436] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function