mirror of
https://github.com/phpseclib/phpseclib.git
synced 2025-02-11 08:08:52 +00:00
AES, DES, RC4, Rijndael, TripleDES: multiple changes
- apply petrich's TripleDES / CFB decrypt to AES and DES - make DES use the updated _generate_xor - correct a comment typo
This commit is contained in:
parent
f7dc23a18e
commit
d94f1b252d
@ -415,14 +415,9 @@ class Crypt_AES extends Crypt_Rijndael {
|
|||||||
$this->debuffer['demcrypt_init'] = true;
|
$this->debuffer['demcrypt_init'] = true;
|
||||||
}
|
}
|
||||||
if ($len >= 16) {
|
if ($len >= 16) {
|
||||||
if ($this->debuffer['demcrypt_init'] === true) {
|
|
||||||
mcrypt_generic_init($this->demcrypt, $this->key, $iv);
|
|
||||||
$this->debuffer['demcrypt_init'] = false;
|
|
||||||
}
|
|
||||||
$cb = substr($ciphertext, $i, $len - $len % 16);
|
$cb = substr($ciphertext, $i, $len - $len % 16);
|
||||||
$plaintext.= mdecrypt_generic($this->demcrypt, $cb);
|
$plaintext.= mcrypt_generic($this->ecb, $iv . $cb) ^ $cb;
|
||||||
$iv = substr($cb, -16);
|
$iv = substr($cb, -16);
|
||||||
$i = strlen($plaintext);
|
|
||||||
$len%= 16;
|
$len%= 16;
|
||||||
}
|
}
|
||||||
if ($len) {
|
if ($len) {
|
||||||
|
@ -391,7 +391,7 @@ class Crypt_DES {
|
|||||||
if (!isset($hash)) {
|
if (!isset($hash)) {
|
||||||
$hash = 'sha1';
|
$hash = 'sha1';
|
||||||
}
|
}
|
||||||
// WPA and WPA use the SSID as the salt
|
// WPA and WPA2 use the SSID as the salt
|
||||||
if (!isset($salt)) {
|
if (!isset($salt)) {
|
||||||
$salt = 'phpseclib/salt';
|
$salt = 'phpseclib/salt';
|
||||||
}
|
}
|
||||||
@ -448,15 +448,11 @@ class Crypt_DES {
|
|||||||
* @see Crypt_DES::decrypt()
|
* @see Crypt_DES::decrypt()
|
||||||
* @see Crypt_DES::encrypt()
|
* @see Crypt_DES::encrypt()
|
||||||
* @access public
|
* @access public
|
||||||
* @param Integer $length
|
|
||||||
* @param String $iv
|
* @param String $iv
|
||||||
*/
|
*/
|
||||||
function _generate_xor($length, &$iv)
|
function _generate_xor(&$iv)
|
||||||
{
|
{
|
||||||
$xor = '';
|
$xor = $iv;
|
||||||
$num_blocks = ($length + 7) >> 3;
|
|
||||||
for ($i = 0; $i < $num_blocks; $i++) {
|
|
||||||
$xor.= $iv;
|
|
||||||
for ($j = 4; $j <= 8; $j+=4) {
|
for ($j = 4; $j <= 8; $j+=4) {
|
||||||
$temp = substr($iv, -$j, 4);
|
$temp = substr($iv, -$j, 4);
|
||||||
switch ($temp) {
|
switch ($temp) {
|
||||||
@ -472,7 +468,6 @@ class Crypt_DES {
|
|||||||
break 2;
|
break 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return $xor;
|
return $xor;
|
||||||
}
|
}
|
||||||
@ -599,14 +594,14 @@ class Crypt_DES {
|
|||||||
if (strlen($buffer['encrypted'])) {
|
if (strlen($buffer['encrypted'])) {
|
||||||
for ($i = 0; $i < strlen($plaintext); $i+=8) {
|
for ($i = 0; $i < strlen($plaintext); $i+=8) {
|
||||||
$block = substr($plaintext, $i, 8);
|
$block = substr($plaintext, $i, 8);
|
||||||
$buffer['encrypted'].= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
|
$buffer['encrypted'].= $this->_processBlock($this->_generate_xor($xor), CRYPT_DES_ENCRYPT);
|
||||||
$key = $this->_string_shift($buffer['encrypted'], 8);
|
$key = $this->_string_shift($buffer['encrypted'], 8);
|
||||||
$ciphertext.= $block ^ $key;
|
$ciphertext.= $block ^ $key;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for ($i = 0; $i < strlen($plaintext); $i+=8) {
|
for ($i = 0; $i < strlen($plaintext); $i+=8) {
|
||||||
$block = substr($plaintext, $i, 8);
|
$block = substr($plaintext, $i, 8);
|
||||||
$key = $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
|
$key = $this->_processBlock($this->_generate_xor($xor), CRYPT_DES_ENCRYPT);
|
||||||
$ciphertext.= $block ^ $key;
|
$ciphertext.= $block ^ $key;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -734,26 +729,10 @@ class Crypt_DES {
|
|||||||
$this->debuffer['demcrypt_init'] = true;
|
$this->debuffer['demcrypt_init'] = true;
|
||||||
}
|
}
|
||||||
if ($len >= 8) {
|
if ($len >= 8) {
|
||||||
if ($this->debuffer['demcrypt_init'] === false || $len > 600) {
|
|
||||||
if ($this->debuffer['demcrypt_init'] === true) {
|
|
||||||
mcrypt_generic_init($this->demcrypt, $this->keys, $iv);
|
|
||||||
$this->debuffer['demcrypt_init'] = false;
|
|
||||||
}
|
|
||||||
$cb = substr($ciphertext, $i, $len - $len % 8);
|
$cb = substr($ciphertext, $i, $len - $len % 8);
|
||||||
$plaintext.= mdecrypt_generic($this->demcrypt, $cb);
|
$plaintext.= mcrypt_generic($this->ecb, $iv . $cb) ^ $cb;
|
||||||
$iv = substr($cb, -8);
|
$iv = substr($cb, -8);
|
||||||
$i = strlen($plaintext);
|
|
||||||
$len%= 8;
|
$len%= 8;
|
||||||
} else {
|
|
||||||
while ($len >= 8) {
|
|
||||||
$iv = mcrypt_generic($this->ecb,$iv);
|
|
||||||
$cb = substr($ciphertext, $i, 8);
|
|
||||||
$plaintext.= $iv ^ $cb;
|
|
||||||
$iv = $cb;
|
|
||||||
$len-= 8;
|
|
||||||
$i+= 8;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if ($len) {
|
if ($len) {
|
||||||
$iv = mcrypt_generic($this->ecb, $iv);
|
$iv = mcrypt_generic($this->ecb, $iv);
|
||||||
@ -800,14 +779,14 @@ class Crypt_DES {
|
|||||||
if (strlen($buffer['ciphertext'])) {
|
if (strlen($buffer['ciphertext'])) {
|
||||||
for ($i = 0; $i < strlen($ciphertext); $i+=8) {
|
for ($i = 0; $i < strlen($ciphertext); $i+=8) {
|
||||||
$block = substr($ciphertext, $i, 8);
|
$block = substr($ciphertext, $i, 8);
|
||||||
$buffer['ciphertext'].= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
|
$buffer['ciphertext'].= $this->_processBlock($this->_generate_xor($xor), CRYPT_DES_ENCRYPT);
|
||||||
$key = $this->_string_shift($buffer['ciphertext'], 8);
|
$key = $this->_string_shift($buffer['ciphertext'], 8);
|
||||||
$plaintext.= $block ^ $key;
|
$plaintext.= $block ^ $key;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for ($i = 0; $i < strlen($ciphertext); $i+=8) {
|
for ($i = 0; $i < strlen($ciphertext); $i+=8) {
|
||||||
$block = substr($ciphertext, $i, 8);
|
$block = substr($ciphertext, $i, 8);
|
||||||
$key = $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
|
$key = $this->_processBlock($this->_generate_xor($xor), CRYPT_DES_ENCRYPT);
|
||||||
$plaintext.= $block ^ $key;
|
$plaintext.= $block ^ $key;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -250,7 +250,7 @@ class Crypt_RC4 {
|
|||||||
if (!isset($hash)) {
|
if (!isset($hash)) {
|
||||||
$hash = 'sha1';
|
$hash = 'sha1';
|
||||||
}
|
}
|
||||||
// WPA and WPA use the SSID as the salt
|
// WPA and WPA2 use the SSID as the salt
|
||||||
if (!isset($salt)) {
|
if (!isset($salt)) {
|
||||||
$salt = 'phpseclib/salt';
|
$salt = 'phpseclib/salt';
|
||||||
}
|
}
|
||||||
|
@ -599,7 +599,7 @@ class Crypt_Rijndael {
|
|||||||
if (!isset($hash)) {
|
if (!isset($hash)) {
|
||||||
$hash = 'sha1';
|
$hash = 'sha1';
|
||||||
}
|
}
|
||||||
// WPA and WPA use the SSID as the salt
|
// WPA and WPA2 use the SSID as the salt
|
||||||
if (!isset($salt)) {
|
if (!isset($salt)) {
|
||||||
$salt = 'phpseclib';
|
$salt = 'phpseclib';
|
||||||
}
|
}
|
||||||
|
@ -387,7 +387,7 @@ class Crypt_TripleDES {
|
|||||||
if (!isset($hash)) {
|
if (!isset($hash)) {
|
||||||
$hash = 'sha1';
|
$hash = 'sha1';
|
||||||
}
|
}
|
||||||
// WPA and WPA use the SSID as the salt
|
// WPA and WPA2 use the SSID as the salt
|
||||||
if (!isset($salt)) {
|
if (!isset($salt)) {
|
||||||
$salt = 'phpseclib';
|
$salt = 'phpseclib';
|
||||||
}
|
}
|
||||||
@ -745,35 +745,12 @@ class Crypt_TripleDES {
|
|||||||
}
|
}
|
||||||
$plaintext = substr($iv, $orig_pos) ^ $ciphertext;
|
$plaintext = substr($iv, $orig_pos) ^ $ciphertext;
|
||||||
$iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
|
$iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
|
||||||
// $this->debuffer['demcrypt_init'] = true;
|
|
||||||
}
|
}
|
||||||
if ($len >= 8) {
|
if ($len >= 8) {
|
||||||
// In decrypt() possible. Will work with better performance as the commented code below
|
|
||||||
$cb = substr($ciphertext, $i, $len - $len % 8);
|
$cb = substr($ciphertext, $i, $len - $len % 8);
|
||||||
$plaintext.= mcrypt_generic($this->ecb, $iv.$cb) ^ $cb;
|
$plaintext.= mcrypt_generic($this->ecb, $iv . $cb) ^ $cb;
|
||||||
$iv = substr($cb, -8);
|
$iv = substr($cb, -8);
|
||||||
$len%= 8;
|
$len%= 8;
|
||||||
/*
|
|
||||||
if ($this->debuffer['demcrypt_init'] === false || $len > 950) {
|
|
||||||
if ($this->debuffer['demcrypt_init'] === true) {
|
|
||||||
mcrypt_generic_init($this->demcrypt, $this->key, $iv);
|
|
||||||
$this->debuffer['demcrypt_init'] = false;
|
|
||||||
}
|
|
||||||
$cb = substr($ciphertext, $i, $len - $len % 8);
|
|
||||||
$plaintext.= mdecrypt_generic($this->demcrypt, $cb);
|
|
||||||
$iv = substr($cb, -8);
|
|
||||||
$len%= 8;
|
|
||||||
} else {
|
|
||||||
while ($len >= 8) {
|
|
||||||
$iv = mcrypt_generic($this->ecb,$iv);
|
|
||||||
$cb = substr($ciphertext, $i, 8);
|
|
||||||
$plaintext.= $iv ^ $cb;
|
|
||||||
$iv = $cb;
|
|
||||||
$i+= 8;
|
|
||||||
$len-= 8;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
if ($len) {
|
if ($len) {
|
||||||
$iv = mcrypt_generic($this->ecb, $iv);
|
$iv = mcrypt_generic($this->ecb, $iv);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user