123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- <?php
- namespace phpseclib\Crypt;
- class TripleDES extends DES
- {
-
- const MODE_3CBC = -2;
-
- const MODE_CBC3 = Base::MODE_CBC;
-
- var $key_length = 24;
-
- var $password_default_salt = 'phpseclib';
-
- var $cipher_name_mcrypt = 'tripledes';
-
- var $cfb_init_len = 750;
-
- var $key_length_max = 24;
-
- var $mode_3cbc;
-
- var $des;
-
- function __construct($mode)
- {
- switch ($mode) {
-
-
- case self::MODE_3CBC:
- parent::__construct(Base::MODE_CBC);
- $this->mode_3cbc = true;
-
- $this->des = array(
- new DES(Base::MODE_CBC),
- new DES(Base::MODE_CBC),
- new DES(Base::MODE_CBC),
- );
-
- $this->des[0]->disablePadding();
- $this->des[1]->disablePadding();
- $this->des[2]->disablePadding();
- break;
-
- default:
- parent::__construct($mode);
- }
- }
-
- function isValidEngine($engine)
- {
- if ($engine == self::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);
- }
-
- 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);
- }
- }
-
- function setKeyLength($length)
- {
- switch ($length) {
- case 128:
- case 192:
- break;
- default:
- throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys of sizes 128 or 192 bits are supported');
- }
- parent::setKeyLength($length);
- }
-
- function setKey($key)
- {
- if ($this->explicit_key_length !== false && strlen($key) != $this->explicit_key_length) {
- throw new \LengthException('Key length has already been set to ' . $this->explicit_key_length . ' bytes and this key is ' . strlen($key) . ' bytes');
- }
- switch (strlen($key)) {
- case 16:
- $key.= substr($key, 0, 8);
- case 24:
- break;
- default:
- throw new \LengthException('Key of size ' . strlen($key) . ' not supported by this algorithm. Only keys of sizes 16 or 24 are supported');
- }
-
- $this->key = $key;
- $this->key_length = strlen($key);
- $this->changed = true;
- $this->_setEngine();
- if ($this->mode_3cbc) {
- $this->des[0]->setKey(substr($key, 0, 8));
- $this->des[1]->setKey(substr($key, 8, 8));
- $this->des[2]->setKey(substr($key, 16, 8));
- }
- }
-
- function encrypt($plaintext)
- {
-
-
-
- if ($this->mode_3cbc && strlen($this->key) > 8) {
- return $this->des[2]->encrypt(
- $this->des[1]->decrypt(
- $this->des[0]->encrypt(
- $this->_pad($plaintext)
- )
- )
- );
- }
- return parent::encrypt($plaintext);
- }
-
- function decrypt($ciphertext)
- {
- if ($this->mode_3cbc && strlen($this->key) > 8) {
- return $this->_unpad(
- $this->des[0]->decrypt(
- $this->des[1]->encrypt(
- $this->des[2]->decrypt(
- str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, "\0")
- )
- )
- )
- );
- }
- return parent::decrypt($ciphertext);
- }
-
- function enableContinuousBuffer()
- {
- parent::enableContinuousBuffer();
- if ($this->mode_3cbc) {
- $this->des[0]->enableContinuousBuffer();
- $this->des[1]->enableContinuousBuffer();
- $this->des[2]->enableContinuousBuffer();
- }
- }
-
- function disableContinuousBuffer()
- {
- parent::disableContinuousBuffer();
- if ($this->mode_3cbc) {
- $this->des[0]->disableContinuousBuffer();
- $this->des[1]->disableContinuousBuffer();
- $this->des[2]->disableContinuousBuffer();
- }
- }
-
- function _setupKey()
- {
- switch (true) {
-
-
- case strlen($this->key) <= 8:
- $this->des_rounds = 1;
- break;
-
- default:
- $this->des_rounds = 3;
-
- if ($this->mode_3cbc) {
- $this->des[0]->_setupKey();
- $this->des[1]->_setupKey();
- $this->des[2]->_setupKey();
-
-
- return;
- }
- }
-
- parent::_setupKey();
- }
-
- 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);
- }
- }
|