Skip to content

Commit a45e528

Browse files
committed
Merge pull request #5 from phpseclib/master
Sync
2 parents eff0bb2 + 5f64ab6 commit a45e528

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

phpseclib/Crypt/DES.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,13 @@ function encrypt($plaintext)
656656
$xor = $this->encryptIV;
657657
if (strlen($buffer['xor'])) {
658658
for ($i = 0; $i < strlen($plaintext); $i+=8) {
659-
$xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
660-
$buffer['xor'].= $xor;
659+
$block = substr($plaintext, $i, 8);
660+
if (strlen($block) > strlen($buffer['xor'])) {
661+
$xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
662+
$buffer['xor'].= $xor;
663+
}
661664
$key = $this->_string_shift($buffer['xor'], 8);
662-
$ciphertext.= substr($plaintext, $i, 8) ^ $key;
665+
$ciphertext.= $block ^ $key;
663666
}
664667
} else {
665668
for ($i = 0; $i < strlen($plaintext); $i+=8) {
@@ -843,10 +846,13 @@ function decrypt($ciphertext)
843846
$xor = $this->decryptIV;
844847
if (strlen($buffer['xor'])) {
845848
for ($i = 0; $i < strlen($ciphertext); $i+=8) {
846-
$xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
847-
$buffer['xor'].= $xor;
849+
$block = substr($plaintext, $i, 8);
850+
if (strlen($block) > strlen($buffer['xor'])) {
851+
$xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
852+
$buffer['xor'].= $xor;
853+
}
848854
$key = $this->_string_shift($buffer['xor'], 8);
849-
$plaintext.= substr($ciphertext, $i, 8) ^ $key;
855+
$plaintext.= $block ^ $key;
850856
}
851857
} else {
852858
for ($i = 0; $i < strlen($ciphertext); $i+=8) {

phpseclib/Crypt/Rijndael.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,10 @@ function encrypt($plaintext)
900900
$xor = $this->encryptIV;
901901
if (strlen($buffer['xor'])) {
902902
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
903-
$xor = $this->_encryptBlock($xor);
904-
$buffer['xor'].= $xor;
903+
if (strlen($block) > strlen($buffer['xor'])) {
904+
$xor = $this->_encryptBlock($xor);
905+
$buffer['xor'].= $xor;
906+
}
905907
$key = $this->_string_shift($buffer['xor'], $block_size);
906908
$ciphertext.= substr($plaintext, $i, $block_size) ^ $key;
907909
}
@@ -1038,8 +1040,10 @@ function decrypt($ciphertext)
10381040
$xor = $this->decryptIV;
10391041
if (strlen($buffer['xor'])) {
10401042
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
1041-
$xor = $this->_encryptBlock($xor);
1042-
$buffer['xor'].= $xor;
1043+
if (strlen($block) > strlen($buffer['xor'])) {
1044+
$xor = $this->_encryptBlock($xor);
1045+
$buffer['xor'].= $xor;
1046+
}
10431047
$key = $this->_string_shift($buffer['xor'], $block_size);
10441048
$plaintext.= substr($ciphertext, $i, $block_size) ^ $key;
10451049
}
@@ -1139,7 +1143,7 @@ function _encryptBlock($in)
11391143
$l = ($l + 1) % $Nb;
11401144
}
11411145

1142-
// 100% ugly switch/case code... but ~5% faster (meaning: ~half second faster de/encrypting 1MB text, tested with php5.4.9 on linux/32bit with an AMD Athlon II P360 CPU) then the commented smart code below. Don't know it's worth or not
1146+
// 100% ugly switch/case code... but ~5% faster ("smart code" below commented out)
11431147
switch ($Nb) {
11441148
case 8:
11451149
return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6], $temp[7]);
@@ -1969,10 +1973,12 @@ function inline_crypt_setup()
19691973
19701974
if (strlen($buffer["xor"])) {
19711975
for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') {
1972-
$in = $xor;
1973-
'.$_encryptBlock.'
1974-
$xor = $in;
1975-
$buffer["xor"].= $xor;
1976+
if (strlen($block) > strlen($buffer["xor"])) {
1977+
$in = $xor;
1978+
'.$_encryptBlock.'
1979+
$xor = $in;
1980+
$buffer["xor"].= $xor;
1981+
}
19761982
$key = $self->_string_shift($buffer["xor"], '.$block_size.');
19771983
$ciphertext.= substr($text, $i, '.$block_size.') ^ $key;
19781984
}
@@ -2002,10 +2008,12 @@ function inline_crypt_setup()
20022008
20032009
if (strlen($buffer["xor"])) {
20042010
for ($i = 0; $i < $ciphertext_len; $i+= '.$block_size.') {
2005-
$in = $xor;
2006-
'.$_encryptBlock.'
2007-
$xor = $in;
2008-
$buffer["xor"].= $xor;
2011+
if (strlen($block) > strlen($buffer["xor"])) {
2012+
$in = $xor;
2013+
'.$_encryptBlock.'
2014+
$xor = $in;
2015+
$buffer["xor"].= $xor;
2016+
}
20092017
$key = $self->_string_shift($buffer["xor"], '.$block_size.');
20102018
$plaintext.= substr($text, $i, '.$block_size.') ^ $key;
20112019
}

phpseclib/Crypt/TripleDES.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -669,10 +669,12 @@ function encrypt($plaintext)
669669
$xor = $this->encryptIV;
670670
if (strlen($buffer['xor'])) {
671671
for ($i = 0; $i < strlen($plaintext); $i+=8) {
672-
$xor = $des[0]->_processBlock($xor, CRYPT_DES_ENCRYPT);
673-
$xor = $des[1]->_processBlock($xor, CRYPT_DES_DECRYPT);
674-
$xor = $des[2]->_processBlock($xor, CRYPT_DES_ENCRYPT);
675-
$buffer['xor'].= $xor;
672+
if (strlen($block) > strlen($buffer['xor'])) {
673+
$xor = $des[0]->_processBlock($xor, CRYPT_DES_ENCRYPT);
674+
$xor = $des[1]->_processBlock($xor, CRYPT_DES_DECRYPT);
675+
$xor = $des[2]->_processBlock($xor, CRYPT_DES_ENCRYPT);
676+
$buffer['xor'].= $xor;
677+
}
676678
$key = $this->_string_shift($buffer['xor'], 8);
677679
$ciphertext.= substr($plaintext, $i, 8) ^ $key;
678680
}
@@ -880,10 +882,12 @@ function decrypt($ciphertext)
880882
$xor = $this->decryptIV;
881883
if (strlen($buffer['xor'])) {
882884
for ($i = 0; $i < strlen($ciphertext); $i+=8) {
883-
$xor = $des[0]->_processBlock($xor, CRYPT_DES_ENCRYPT);
884-
$xor = $des[1]->_processBlock($xor, CRYPT_DES_DECRYPT);
885-
$xor = $des[2]->_processBlock($xor, CRYPT_DES_ENCRYPT);
886-
$buffer['xor'].= $xor;
885+
if (strlen($block) > strlen($buffer['xor'])) {
886+
$xor = $des[0]->_processBlock($xor, CRYPT_DES_ENCRYPT);
887+
$xor = $des[1]->_processBlock($xor, CRYPT_DES_DECRYPT);
888+
$xor = $des[2]->_processBlock($xor, CRYPT_DES_ENCRYPT);
889+
$buffer['xor'].= $xor;
890+
}
887891
$key = $this->_string_shift($buffer['xor'], 8);
888892
$plaintext.= substr($ciphertext, $i, 8) ^ $key;
889893
}

0 commit comments

Comments
 (0)