From 9194759d58a75169ae9001872e61e37184466722 Mon Sep 17 00:00:00 2001 From: Jim Wigginton Date: Wed, 27 May 2009 16:15:23 +0000 Subject: [PATCH] - cosmetic changes to the code - encryption should work even when setKey() isn't called - padding wasn't always done when it should have been git-svn-id: http://phpseclib.svn.sourceforge.net/svnroot/phpseclib/trunk@33 21d32557-59b3-4da0-833f-c5933fad653e --- phpseclib/Crypt/AES.php | 8 ++++---- phpseclib/Crypt/DES.php | 10 +++++----- phpseclib/Crypt/Hash.php | 8 ++++---- phpseclib/Crypt/RC4.php | 17 ++++++++++++----- phpseclib/Crypt/Rijndael.php | 11 ++++++----- phpseclib/Crypt/TripleDES.php | 6 ++---- phpseclib/Math/BigInteger.php | 16 +++++----------- 7 files changed, 38 insertions(+), 38 deletions(-) diff --git a/phpseclib/Crypt/AES.php b/phpseclib/Crypt/AES.php index 72c9b274..867befcb 100644 --- a/phpseclib/Crypt/AES.php +++ b/phpseclib/Crypt/AES.php @@ -56,7 +56,7 @@ * @author Jim Wigginton * @copyright MMVIII Jim Wigginton * @license http://www.gnu.org/licenses/lgpl.txt - * @version $Id: AES.php,v 1.2 2009-02-26 17:25:00 terrafrost Exp $ + * @version $Id: AES.php,v 1.3 2009-05-27 16:15:23 terrafrost Exp $ * @link http://phpseclib.sourceforge.net */ @@ -67,8 +67,8 @@ require_once 'Rijndael.php'; /**#@+ * @access public - * @see Crypt_Rijndael::encrypt() - * @see Crypt_Rijndael::decrypt() + * @see Crypt_AES::encrypt() + * @see Crypt_AES::decrypt() */ /** * Encrypt / decrypt using the Electronic Code Book mode. @@ -203,7 +203,7 @@ class Crypt_AES extends Crypt_Rijndael { { if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) { $this->_mcryptSetup(); - $this->_pad($plaintext); + $plaintext = $this->_pad($plaintext); $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, $this->mcrypt[0], $this->mode, $this->mcrypt[1]); mcrypt_generic_init($td, $this->key, $this->encryptIV); diff --git a/phpseclib/Crypt/DES.php b/phpseclib/Crypt/DES.php index fd5746b4..a8d28f7e 100644 --- a/phpseclib/Crypt/DES.php +++ b/phpseclib/Crypt/DES.php @@ -53,7 +53,7 @@ * @author Jim Wigginton * @copyright MMVII Jim Wigginton * @license http://www.gnu.org/licenses/lgpl.txt - * @version $Id: DES.php,v 1.7 2009-03-09 05:13:24 terrafrost Exp $ + * @version $Id: DES.php,v 1.8 2009-05-27 16:15:23 terrafrost Exp $ * @link http://phpseclib.sourceforge.net */ @@ -121,7 +121,7 @@ class Crypt_DES { * @var Array * @access private */ - var $keys = false; + var $keys = "\0\0\0\0\0\0\0\0"; /** * The Encryption Mode @@ -302,7 +302,7 @@ class Crypt_DES { */ function encrypt($plaintext) { - $this->_pad($plaintext); + $plaintext = $this->_pad($plaintext); if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) { $td = mcrypt_module_open(MCRYPT_DES, $this->mcrypt[0], $this->mode, $this->mcrypt[1]); @@ -320,7 +320,7 @@ class Crypt_DES { return $ciphertext; } - if ($this->keys === false) { + if (!is_array($this->keys)) { $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0"); } @@ -378,7 +378,7 @@ class Crypt_DES { return $this->_unpad($plaintext); } - if ($this->keys === false) { + if (!is_array($this->keys)) { $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0"); } diff --git a/phpseclib/Crypt/Hash.php b/phpseclib/Crypt/Hash.php index e7dc6a7e..c0fa8870 100644 --- a/phpseclib/Crypt/Hash.php +++ b/phpseclib/Crypt/Hash.php @@ -46,7 +46,7 @@ * @author Jim Wigginton * @copyright MMVII Jim Wigginton * @license http://www.gnu.org/licenses/lgpl.txt - * @version $Id: Hash.php,v 1.2 2009-02-26 17:25:00 terrafrost Exp $ + * @version $Id: Hash.php,v 1.3 2009-05-27 16:15:23 terrafrost Exp $ * @link http://phpseclib.sourceforge.net */ @@ -189,7 +189,7 @@ class Crypt_Hash { $this->l = 20; } - switch (CRYPT_HASH_MODE) { + switch ( CRYPT_HASH_MODE ) { case CRYPT_HASH_MODE_MHASH: switch ($hash) { case 'md5': @@ -242,7 +242,7 @@ class Crypt_Hash { function hash($text) { if (!empty($this->key)) { - switch (CRYPT_HASH_MODE) { + switch ( CRYPT_HASH_MODE ) { case CRYPT_HASH_MODE_MHASH: $output = mhash($this->hash, $text, $this->key); break; @@ -266,7 +266,7 @@ class Crypt_Hash { $output = pack('H*', $hash($output)); // step 7 } } else { - switch (CRYPT_HASH_MODE) { + switch ( CRYPT_HASH_MODE ) { case CRYPT_HASH_MODE_MHASH: $output = mhash($this->hash, $text); break; diff --git a/phpseclib/Crypt/RC4.php b/phpseclib/Crypt/RC4.php index f413c6e5..029e7d9d 100644 --- a/phpseclib/Crypt/RC4.php +++ b/phpseclib/Crypt/RC4.php @@ -55,7 +55,7 @@ * @author Jim Wigginton * @copyright MMVII Jim Wigginton * @license http://www.gnu.org/licenses/lgpl.txt - * @version $Id: RC4.php,v 1.5 2009-04-28 02:56:34 terrafrost Exp $ + * @version $Id: RC4.php,v 1.6 2009-05-27 16:15:23 terrafrost Exp $ * @link http://phpseclib.sourceforge.net */ @@ -208,7 +208,7 @@ class Crypt_RC4 { { $this->key = $key; - if (CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT) { + if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) { return; } @@ -277,7 +277,8 @@ class Crypt_RC4 { * @access public * @param String $plaintext */ - function encrypt($plaintext) { + function encrypt($plaintext) + { return $this->_crypt($plaintext, CRYPT_RC4_ENCRYPT); } @@ -291,7 +292,8 @@ class Crypt_RC4 { * @access public * @param String $ciphertext */ - function decrypt($ciphertext) { + function decrypt($ciphertext) + { return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT); } @@ -304,7 +306,8 @@ class Crypt_RC4 { * @param String $text * @param Integer $mode */ - function _crypt($text, $mode) { + function _crypt($text, $mode) + { if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) { $keyStream = $mode == CRYPT_RC4_ENCRYPT ? 'encryptStream' : 'decryptStream'; @@ -332,6 +335,10 @@ class Crypt_RC4 { list($i, $j) = $this->decryptIndex; } + if ($keyStream === false) { + $this->setKey($this->key); + } + $newText = ''; for ($k = 0; $k < strlen($text); $k++) { $i = ($i + 1) & 255; diff --git a/phpseclib/Crypt/Rijndael.php b/phpseclib/Crypt/Rijndael.php index fd3b8c33..e1797747 100644 --- a/phpseclib/Crypt/Rijndael.php +++ b/phpseclib/Crypt/Rijndael.php @@ -64,7 +64,7 @@ * @author Jim Wigginton * @copyright MMVIII Jim Wigginton * @license http://www.gnu.org/licenses/lgpl.txt - * @version $Id: Rijndael.php,v 1.2 2009-02-16 22:22:13 terrafrost Exp $ + * @version $Id: Rijndael.php,v 1.3 2009-05-27 16:15:23 terrafrost Exp $ * @link http://phpseclib.sourceforge.net */ @@ -89,7 +89,7 @@ define('CRYPT_RIJNDAEL_MODE_CBC', 2); /**#@+ * @access private - * @see Crypt_AES::Crypt_AES() + * @see Crypt_Rijndael::Crypt_Rijndael() */ /** * Toggles the internal implementation @@ -158,7 +158,7 @@ class Crypt_Rijndael { /** * Continuous Buffer status * - * @see Crypt_DES::enableContinuousBuffer() + * @see Crypt_Rijndael::enableContinuousBuffer() * @var Boolean * @access private */ @@ -167,7 +167,7 @@ class Crypt_Rijndael { /** * Padding status * - * @see Crypt_DES::enablePadding() + * @see Crypt_Rijndael::enablePadding() * @var Boolean * @access private */ @@ -358,7 +358,8 @@ class Crypt_Rijndael { * @return Crypt_Rijndael * @access public */ - function Crypt_Rijndael($mode = CRYPT_MODE_RIJNDAEL_CBC) { + function Crypt_Rijndael($mode = CRYPT_MODE_RIJNDAEL_CBC) + { switch ($mode) { case CRYPT_RIJNDAEL_MODE_ECB: case CRYPT_RIJNDAEL_MODE_CBC: diff --git a/phpseclib/Crypt/TripleDES.php b/phpseclib/Crypt/TripleDES.php index 527f7250..f017095f 100644 --- a/phpseclib/Crypt/TripleDES.php +++ b/phpseclib/Crypt/TripleDES.php @@ -47,7 +47,7 @@ * @author Jim Wigginton * @copyright MMVII Jim Wigginton * @license http://www.gnu.org/licenses/lgpl.txt - * @version $Id: TripleDES.php,v 1.6 2009-03-09 05:13:24 terrafrost Exp $ + * @version $Id: TripleDES.php,v 1.7 2009-05-27 16:15:23 terrafrost Exp $ * @link http://phpseclib.sourceforge.net */ @@ -313,9 +313,7 @@ class Crypt_TripleDES { */ function encrypt($plaintext) { - if ($this->padding) { - $plaintext = $this->_pad($plaintext); - } + $plaintext = $this->_pad($plaintext); // if the key is smaller then 8, do what we'd normally do if ($this->mode == CRYPT_DES_MODE_3CBC && strlen($this->key) > 8) { diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index 1ac66c6a..ea597e99 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -69,7 +69,7 @@ * @author Jim Wigginton * @copyright MMVI Jim Wigginton * @license http://www.gnu.org/licenses/lgpl.txt - * @version $Id: BigInteger.php,v 1.8 2009-05-16 17:09:37 terrafrost Exp $ + * @version $Id: BigInteger.php,v 1.9 2009-05-27 16:15:23 terrafrost Exp $ * @link http://pear.php.net/package/Math_BigInteger */ @@ -358,9 +358,9 @@ class Math_BigInteger { $str = '0x'; while (strlen($x)) { - $part = substr($x, 0, 4); - $str.= dechex(bindec($part)); - $x = substr($x, 4); + $part = substr($x, 0, 4); + $str.= dechex(bindec($part)); + $x = substr($x, 4); } if ($this->is_negative) { @@ -792,7 +792,6 @@ class Math_BigInteger { $product->value[$k] = $carry; - // the above for loop is what the previous comment was talking about. the // following for loop is the "one with nested for loops" @@ -1864,7 +1863,6 @@ class Math_BigInteger { break; case MATH_BIGINTEGER_MODE_BCMATH: - $temp->value = bcdiv($this->value, bcpow('2', $shift)); break; @@ -2180,8 +2178,4 @@ class Math_BigInteger { $temp = unpack('Nint', str_pad($x, 4, chr(0), STR_PAD_LEFT)); return $temp['int']; } -} - -// vim: ts=4:sw=4:et: -// vim6: fdl=1: -?> \ No newline at end of file +} \ No newline at end of file