2010-05-14 14:06:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP implementation of Triple DES.
|
|
|
|
*
|
|
|
|
* Uses mcrypt, if available, and an internal implementation, otherwise. Operates in the EDE3 mode (encrypt-decrypt-encrypt).
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* Here's a short example of how to use this library:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
2015-07-06 19:44:12 +00:00
|
|
|
* include 'Crypt/TripleDES.php';
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
|
|
|
* $des = new Crypt_TripleDES();
|
|
|
|
*
|
|
|
|
* $des->setKey('abcdefghijklmnopqrstuvwx');
|
|
|
|
*
|
|
|
|
* $size = 10 * 1024;
|
|
|
|
* $plaintext = '';
|
|
|
|
* for ($i = 0; $i < $size; $i++) {
|
|
|
|
* $plaintext.= 'a';
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* echo $des->decrypt($des->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_TripleDES
|
|
|
|
* @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
|
2010-05-14 14:06:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include Crypt_DES
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
if (!class_exists('Crypt_DES')) {
|
|
|
|
include_once 'DES.php';
|
|
|
|
}
|
2010-05-14 14:06:17 +00:00
|
|
|
|
2015-10-04 13:24:43 +00:00
|
|
|
/**#@+
|
|
|
|
* @access public
|
2016-01-19 18:02:38 +00:00
|
|
|
* @see self::Crypt_TripleDES()
|
2015-10-04 13:24:43 +00:00
|
|
|
*/
|
2010-05-14 14:06:17 +00:00
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using inner chaining
|
|
|
|
*
|
|
|
|
* Inner chaining is used by SSH-1 and is generally considered to be less secure then outer chaining (CRYPT_DES_MODE_CBC3).
|
|
|
|
*/
|
2015-10-04 13:24:43 +00:00
|
|
|
define('CRYPT_MODE_3CBC', -2);
|
|
|
|
/**
|
|
|
|
* BC version of the above.
|
|
|
|
*/
|
2011-05-19 18:02:11 +00:00
|
|
|
define('CRYPT_DES_MODE_3CBC', -2);
|
2010-05-14 14:06:17 +00:00
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using outer chaining
|
|
|
|
*
|
|
|
|
* Outer chaining is used by SSH-2 and when the mode is set to CRYPT_DES_MODE_CBC.
|
|
|
|
*/
|
2015-10-04 13:24:43 +00:00
|
|
|
define('CRYPT_MODE_CBC3', CRYPT_MODE_CBC);
|
|
|
|
/**
|
|
|
|
* BC version of the above.
|
|
|
|
*/
|
|
|
|
define('CRYPT_DES_MODE_CBC3', CRYPT_MODE_CBC3);
|
|
|
|
/**#@-*/
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP implementation of Triple DES.
|
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @package Crypt_TripleDES
|
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_TripleDES extends Crypt_DES
|
|
|
|
{
|
2010-05-14 14:06:17 +00:00
|
|
|
/**
|
2016-01-19 18:02:38 +00:00
|
|
|
* Key Length (in bytes)
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2016-01-19 18:02:38 +00:00
|
|
|
* @see Crypt_TripleDES::setKeyLength()
|
|
|
|
* @var int
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-01-19 18:02:38 +00:00
|
|
|
var $key_length = 24;
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* The default salt used by setPassword()
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::password_default_salt
|
|
|
|
* @see Crypt_Base::setPassword()
|
2016-01-19 18:02:38 +00:00
|
|
|
* @var string
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
var $password_default_salt = 'phpseclib';
|
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_DES::const_namespace
|
|
|
|
* @see Crypt_Base::const_namespace
|
2016-01-19 18:02:38 +00:00
|
|
|
* @var string
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
var $const_namespace = 'DES';
|
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_DES::cipher_name_mcrypt
|
|
|
|
* @see Crypt_Base::cipher_name_mcrypt
|
2016-01-19 18:02:38 +00:00
|
|
|
* @var string
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
var $cipher_name_mcrypt = 'tripledes';
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* Optimizing value while CFB-encrypting
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::cfb_init_len
|
2016-01-19 18:02:38 +00:00
|
|
|
* @var int
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
var $cfb_init_len = 750;
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* max possible size of $key
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2016-01-19 18:02:38 +00:00
|
|
|
* @see self::setKey()
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_DES::setKey()
|
2016-01-19 18:02:38 +00:00
|
|
|
* @var string
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-01-19 18:02:38 +00:00
|
|
|
var $key_length_max = 24;
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* Internal flag whether using CRYPT_DES_MODE_3CBC or not
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2016-01-19 18:02:38 +00:00
|
|
|
* @var bool
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
var $mode_3cbc;
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* The Crypt_DES objects
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* Used only if $mode_3cbc === true
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2016-01-19 18:02:38 +00:00
|
|
|
* @var array
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
var $des;
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* Default Constructor.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* Determines whether or not the mcrypt extension should be used.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* $mode could be:
|
2011-05-19 18:02:11 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* - CRYPT_DES_MODE_ECB
|
2011-05-19 18:02:11 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* - CRYPT_DES_MODE_CBC
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* - CRYPT_DES_MODE_CTR
|
2011-05-19 18:02:11 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* - CRYPT_DES_MODE_CFB
|
2011-05-19 18:02:11 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* - CRYPT_DES_MODE_OFB
|
2011-05-19 18:02:11 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* - CRYPT_DES_MODE_3CBC
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2015-07-06 19:44:12 +00:00
|
|
|
* If not explicitly set, CRYPT_DES_MODE_CBC will be used.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_DES::Crypt_DES()
|
|
|
|
* @see Crypt_Base::Crypt_Base()
|
2016-01-19 18:02:38 +00:00
|
|
|
* @param int $mode
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2015-10-04 13:24:43 +00:00
|
|
|
function Crypt_TripleDES($mode = CRYPT_MODE_CBC)
|
2010-05-14 14:06:17 +00:00
|
|
|
{
|
2014-03-30 06:55:33 +00:00
|
|
|
switch ($mode) {
|
|
|
|
// In case of CRYPT_DES_MODE_3CBC, we init as CRYPT_DES_MODE_CBC
|
|
|
|
// and additional flag us internally as 3CBC
|
|
|
|
case CRYPT_DES_MODE_3CBC:
|
2015-10-04 13:24:43 +00:00
|
|
|
parent::Crypt_Base(CRYPT_MODE_CBC);
|
2014-03-30 06:55:33 +00:00
|
|
|
$this->mode_3cbc = true;
|
|
|
|
|
|
|
|
// This three $des'es will do the 3CBC work (if $key > 64bits)
|
2010-05-14 14:06:17 +00:00
|
|
|
$this->des = array(
|
2015-10-04 13:24:43 +00:00
|
|
|
new Crypt_DES(CRYPT_MODE_CBC),
|
|
|
|
new Crypt_DES(CRYPT_MODE_CBC),
|
|
|
|
new Crypt_DES(CRYPT_MODE_CBC),
|
2010-05-14 14:06:17 +00:00
|
|
|
);
|
2014-03-30 06:55:33 +00:00
|
|
|
|
2010-05-14 14:06:17 +00:00
|
|
|
// we're going to be doing the padding, ourselves, so disable it in the Crypt_DES objects
|
|
|
|
$this->des[0]->disablePadding();
|
|
|
|
$this->des[1]->disablePadding();
|
|
|
|
$this->des[2]->disablePadding();
|
2014-03-30 06:55:33 +00:00
|
|
|
break;
|
|
|
|
// If not 3CBC, we init as usual
|
|
|
|
default:
|
2015-07-06 19:44:12 +00:00
|
|
|
parent::Crypt_Base($mode);
|
2014-03-30 06:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
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()
|
2016-01-19 18:02:38 +00:00
|
|
|
* @param int $engine
|
2015-10-04 13:24:43 +00:00
|
|
|
* @access public
|
2016-01-19 18:02:38 +00:00
|
|
|
* @return bool
|
2015-10-04 13:24:43 +00:00
|
|
|
*/
|
|
|
|
function isValidEngine($engine)
|
|
|
|
{
|
|
|
|
if ($engine == CRYPT_ENGINE_OPENSSL) {
|
|
|
|
$this->cipher_name_openssl_ecb = 'des-ede3';
|
|
|
|
$mode = $this->_openssl_translate_mode();
|
|
|
|
$this->cipher_name_openssl = $mode == 'ecb' ? 'des-ede3' : 'des-ede3-' . $mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::isValidEngine($engine);
|
|
|
|
}
|
|
|
|
|
2014-03-30 06:55:33 +00:00
|
|
|
/**
|
|
|
|
* Sets the initialization vector. (optional)
|
|
|
|
*
|
2015-07-06 19:44:12 +00:00
|
|
|
* SetIV is not required when CRYPT_DES_MODE_ECB is being used. If not explicitly set, it'll be assumed
|
2014-03-30 06:55:33 +00:00
|
|
|
* to be all zero's.
|
|
|
|
*
|
|
|
|
* @see Crypt_Base::setIV()
|
|
|
|
* @access public
|
2016-01-19 18:02:38 +00:00
|
|
|
* @param string $iv
|
2014-03-30 06:55:33 +00:00
|
|
|
*/
|
|
|
|
function setIV($iv)
|
|
|
|
{
|
|
|
|
parent::setIV($iv);
|
|
|
|
if ($this->mode_3cbc) {
|
|
|
|
$this->des[0]->setIV($iv);
|
|
|
|
$this->des[1]->setIV($iv);
|
|
|
|
$this->des[2]->setIV($iv);
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 18:02:38 +00:00
|
|
|
/**
|
|
|
|
* Sets the key length.
|
|
|
|
*
|
|
|
|
* Valid key lengths are 64, 128 and 192
|
|
|
|
*
|
|
|
|
* @see Crypt_Base:setKeyLength()
|
|
|
|
* @access public
|
|
|
|
* @param int $length
|
|
|
|
*/
|
|
|
|
function setKeyLength($length)
|
|
|
|
{
|
|
|
|
$length >>= 3;
|
|
|
|
switch (true) {
|
|
|
|
case $length <= 8:
|
|
|
|
$this->key_length = 8;
|
|
|
|
break;
|
|
|
|
case $length <= 16:
|
|
|
|
$this->key_length = 16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->key_length = 24;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::setKeyLength($length);
|
|
|
|
}
|
|
|
|
|
2010-05-14 14:06:17 +00:00
|
|
|
/**
|
|
|
|
* Sets the key.
|
|
|
|
*
|
|
|
|
* Keys can be of any length. Triple DES, itself, can use 128-bit (eg. strlen($key) == 16) or
|
|
|
|
* 192-bit (eg. strlen($key) == 24) keys. This function pads and truncates $key as appropriate.
|
|
|
|
*
|
|
|
|
* DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
|
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* If the key is not explicitly set, it'll be assumed to be all null bytes.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
|
|
|
* @access public
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_DES::setKey()
|
|
|
|
* @see Crypt_Base::setKey()
|
2016-01-19 18:02:38 +00:00
|
|
|
* @param string $key
|
2010-05-14 14:06:17 +00:00
|
|
|
*/
|
|
|
|
function setKey($key)
|
|
|
|
{
|
2016-01-19 18:02:38 +00:00
|
|
|
$length = $this->explicit_key_length ? $this->key_length : strlen($key);
|
2010-05-14 14:06:17 +00:00
|
|
|
if ($length > 8) {
|
2014-03-30 06:55:33 +00:00
|
|
|
$key = str_pad(substr($key, 0, 24), 24, chr(0));
|
2010-05-14 14:06:17 +00:00
|
|
|
// if $key is between 64 and 128-bits, use the first 64-bits as the last, per this:
|
|
|
|
// http://php.net/function.mcrypt-encrypt#47973
|
2015-10-04 13:24:43 +00:00
|
|
|
$key = $length <= 16 ? substr_replace($key, substr($key, 0, 8), 16) : substr($key, 0, 24);
|
2011-05-19 18:02:11 +00:00
|
|
|
} else {
|
|
|
|
$key = str_pad($key, 8, chr(0));
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
2014-03-30 06:55:33 +00:00
|
|
|
parent::setKey($key);
|
|
|
|
|
|
|
|
// And in case of CRYPT_DES_MODE_3CBC:
|
|
|
|
// if key <= 64bits we not need the 3 $des to work,
|
|
|
|
// because we will then act as regular DES-CBC with just a <= 64bit key.
|
|
|
|
// So only if the key > 64bits (> 8 bytes) we will call setKey() for the 3 $des.
|
|
|
|
if ($this->mode_3cbc && $length > 8) {
|
|
|
|
$this->des[0]->setKey(substr($key, 0, 8));
|
|
|
|
$this->des[1]->setKey(substr($key, 8, 8));
|
|
|
|
$this->des[2]->setKey(substr($key, 16, 8));
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encrypts a message.
|
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::encrypt()
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access public
|
2016-01-19 18:02:38 +00:00
|
|
|
* @param string $plaintext
|
|
|
|
* @return string $cipertext
|
2010-05-14 14:06:17 +00:00
|
|
|
*/
|
|
|
|
function encrypt($plaintext)
|
|
|
|
{
|
2014-03-30 06:55:33 +00:00
|
|
|
// parent::en/decrypt() is able to do all the work for all modes and keylengths,
|
2015-10-04 13:24:43 +00:00
|
|
|
// except for: CRYPT_MODE_3CBC (inner chaining CBC) with a key > 64bits
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
// if the key is smaller then 8, do what we'd normally do
|
2014-03-30 06:55:33 +00:00
|
|
|
if ($this->mode_3cbc && strlen($this->key) > 8) {
|
|
|
|
return $this->des[2]->encrypt(
|
2015-07-06 19:44:12 +00:00
|
|
|
$this->des[1]->decrypt(
|
|
|
|
$this->des[0]->encrypt(
|
|
|
|
$this->_pad($plaintext)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 06:55:33 +00:00
|
|
|
return parent::encrypt($plaintext);
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrypts a message.
|
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::decrypt()
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access public
|
2016-01-19 18:02:38 +00:00
|
|
|
* @param string $ciphertext
|
|
|
|
* @return string $plaintext
|
2010-05-14 14:06:17 +00:00
|
|
|
*/
|
|
|
|
function decrypt($ciphertext)
|
|
|
|
{
|
2014-03-30 06:55:33 +00:00
|
|
|
if ($this->mode_3cbc && strlen($this->key) > 8) {
|
2015-07-06 19:44:12 +00:00
|
|
|
return $this->_unpad(
|
|
|
|
$this->des[0]->decrypt(
|
|
|
|
$this->des[1]->encrypt(
|
|
|
|
$this->des[2]->decrypt(
|
|
|
|
str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, "\0")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2011-05-19 18:02:11 +00:00
|
|
|
}
|
2010-05-14 14:06:17 +00:00
|
|
|
|
2014-03-30 06:55:33 +00:00
|
|
|
return parent::decrypt($ciphertext);
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Treat consecutive "packets" as if they are a continuous buffer.
|
|
|
|
*
|
|
|
|
* Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
|
|
|
|
* will yield different outputs:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* echo $des->encrypt(substr($plaintext, 0, 8));
|
|
|
|
* echo $des->encrypt(substr($plaintext, 8, 8));
|
|
|
|
* </code>
|
|
|
|
* <code>
|
|
|
|
* echo $des->encrypt($plaintext);
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
|
|
|
|
* another, as demonstrated with the following:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $des->encrypt(substr($plaintext, 0, 8));
|
|
|
|
* echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
|
|
|
|
* </code>
|
|
|
|
* <code>
|
|
|
|
* echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
|
|
|
|
* outputs. The reason is due to the fact that the initialization vector's change after every encryption /
|
|
|
|
* decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
|
|
|
|
*
|
|
|
|
* Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
|
|
|
|
* encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
|
|
|
|
* continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
|
|
|
|
* however, they are also less intuitive and more likely to cause you problems.
|
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::enableContinuousBuffer()
|
2016-01-19 18:02:38 +00:00
|
|
|
* @see self::disableContinuousBuffer()
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function enableContinuousBuffer()
|
|
|
|
{
|
2014-03-30 06:55:33 +00:00
|
|
|
parent::enableContinuousBuffer();
|
|
|
|
if ($this->mode_3cbc) {
|
2010-05-14 14:06:17 +00:00
|
|
|
$this->des[0]->enableContinuousBuffer();
|
|
|
|
$this->des[1]->enableContinuousBuffer();
|
|
|
|
$this->des[2]->enableContinuousBuffer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Treat consecutive packets as if they are a discontinuous buffer.
|
|
|
|
*
|
|
|
|
* The default behavior.
|
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::disableContinuousBuffer()
|
2016-01-19 18:02:38 +00:00
|
|
|
* @see self::enableContinuousBuffer()
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function disableContinuousBuffer()
|
|
|
|
{
|
2014-03-30 06:55:33 +00:00
|
|
|
parent::disableContinuousBuffer();
|
|
|
|
if ($this->mode_3cbc) {
|
2010-05-14 14:06:17 +00:00
|
|
|
$this->des[0]->disableContinuousBuffer();
|
|
|
|
$this->des[1]->disableContinuousBuffer();
|
|
|
|
$this->des[2]->disableContinuousBuffer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* Creates the key schedule
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_DES::_setupKey()
|
|
|
|
* @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
|
|
|
switch (true) {
|
|
|
|
// if $key <= 64bits we configure our internal pure-php cipher engine
|
|
|
|
// to act as regular [1]DES, not as 3DES. mcrypt.so::tripledes does the same.
|
|
|
|
case strlen($this->key) <= 8:
|
|
|
|
$this->des_rounds = 1;
|
|
|
|
break;
|
2010-05-14 14:06:17 +00:00
|
|
|
|
2014-03-30 06:55:33 +00:00
|
|
|
// otherwise, if $key > 64bits, we configure our engine to work as 3DES.
|
|
|
|
default:
|
|
|
|
$this->des_rounds = 3;
|
2010-05-14 14:06:17 +00:00
|
|
|
|
2014-03-30 06:55:33 +00:00
|
|
|
// (only) if 3CBC is used we have, of course, to setup the $des[0-2] keys also separately.
|
|
|
|
if ($this->mode_3cbc) {
|
|
|
|
$this->des[0]->_setupKey();
|
|
|
|
$this->des[1]->_setupKey();
|
|
|
|
$this->des[2]->_setupKey();
|
2010-05-14 14:06:17 +00:00
|
|
|
|
2014-03-30 06:55:33 +00:00
|
|
|
// because $des[0-2] will, now, do all the work we can return here
|
|
|
|
// not need unnecessary stress parent::_setupKey() with our, now unused, $key.
|
|
|
|
return;
|
|
|
|
}
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
2014-03-30 06:55:33 +00:00
|
|
|
// setup our key
|
|
|
|
parent::_setupKey();
|
2011-05-19 18:02:11 +00:00
|
|
|
}
|
2015-10-04 13:24:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the internal crypt engine
|
|
|
|
*
|
|
|
|
* @see Crypt_Base::Crypt_Base()
|
|
|
|
* @see Crypt_Base::setPreferredEngine()
|
2016-01-19 18:02:38 +00:00
|
|
|
* @param int $engine
|
2015-10-04 13:24:43 +00:00
|
|
|
* @access public
|
2016-01-19 18:02:38 +00:00
|
|
|
* @return int
|
2015-10-04 13:24:43 +00:00
|
|
|
*/
|
|
|
|
function setPreferredEngine($engine)
|
|
|
|
{
|
|
|
|
if ($this->mode_3cbc) {
|
|
|
|
$this->des[0]->setPreferredEngine($engine);
|
|
|
|
$this->des[1]->setPreferredEngine($engine);
|
|
|
|
$this->des[2]->setPreferredEngine($engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::setPreferredEngine($engine);
|
|
|
|
}
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|