- 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
This commit is contained in:
Jim Wigginton 2009-05-27 16:15:23 +00:00
parent d365b7a587
commit 9194759d58
7 changed files with 38 additions and 38 deletions

View File

@ -56,7 +56,7 @@
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVIII Jim Wigginton * @copyright MMVIII Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt * @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 * @link http://phpseclib.sourceforge.net
*/ */
@ -67,8 +67,8 @@ require_once 'Rijndael.php';
/**#@+ /**#@+
* @access public * @access public
* @see Crypt_Rijndael::encrypt() * @see Crypt_AES::encrypt()
* @see Crypt_Rijndael::decrypt() * @see Crypt_AES::decrypt()
*/ */
/** /**
* Encrypt / decrypt using the Electronic Code Book mode. * 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 ) { if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
$this->_mcryptSetup(); $this->_mcryptSetup();
$this->_pad($plaintext); $plaintext = $this->_pad($plaintext);
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, $this->mcrypt[0], $this->mode, $this->mcrypt[1]); $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, $this->mcrypt[0], $this->mode, $this->mcrypt[1]);
mcrypt_generic_init($td, $this->key, $this->encryptIV); mcrypt_generic_init($td, $this->key, $this->encryptIV);

View File

@ -53,7 +53,7 @@
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVII Jim Wigginton * @copyright MMVII Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt * @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 * @link http://phpseclib.sourceforge.net
*/ */
@ -121,7 +121,7 @@ class Crypt_DES {
* @var Array * @var Array
* @access private * @access private
*/ */
var $keys = false; var $keys = "\0\0\0\0\0\0\0\0";
/** /**
* The Encryption Mode * The Encryption Mode
@ -302,7 +302,7 @@ class Crypt_DES {
*/ */
function encrypt($plaintext) function encrypt($plaintext)
{ {
$this->_pad($plaintext); $plaintext = $this->_pad($plaintext);
if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) { if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
$td = mcrypt_module_open(MCRYPT_DES, $this->mcrypt[0], $this->mode, $this->mcrypt[1]); $td = mcrypt_module_open(MCRYPT_DES, $this->mcrypt[0], $this->mode, $this->mcrypt[1]);
@ -320,7 +320,7 @@ class Crypt_DES {
return $ciphertext; return $ciphertext;
} }
if ($this->keys === false) { if (!is_array($this->keys)) {
$this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0"); $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0");
} }
@ -378,7 +378,7 @@ class Crypt_DES {
return $this->_unpad($plaintext); 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"); $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0");
} }

View File

@ -46,7 +46,7 @@
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVII Jim Wigginton * @copyright MMVII Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt * @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 * @link http://phpseclib.sourceforge.net
*/ */
@ -189,7 +189,7 @@ class Crypt_Hash {
$this->l = 20; $this->l = 20;
} }
switch (CRYPT_HASH_MODE) { switch ( CRYPT_HASH_MODE ) {
case CRYPT_HASH_MODE_MHASH: case CRYPT_HASH_MODE_MHASH:
switch ($hash) { switch ($hash) {
case 'md5': case 'md5':
@ -242,7 +242,7 @@ class Crypt_Hash {
function hash($text) function hash($text)
{ {
if (!empty($this->key)) { if (!empty($this->key)) {
switch (CRYPT_HASH_MODE) { switch ( CRYPT_HASH_MODE ) {
case CRYPT_HASH_MODE_MHASH: case CRYPT_HASH_MODE_MHASH:
$output = mhash($this->hash, $text, $this->key); $output = mhash($this->hash, $text, $this->key);
break; break;
@ -266,7 +266,7 @@ class Crypt_Hash {
$output = pack('H*', $hash($output)); // step 7 $output = pack('H*', $hash($output)); // step 7
} }
} else { } else {
switch (CRYPT_HASH_MODE) { switch ( CRYPT_HASH_MODE ) {
case CRYPT_HASH_MODE_MHASH: case CRYPT_HASH_MODE_MHASH:
$output = mhash($this->hash, $text); $output = mhash($this->hash, $text);
break; break;

View File

@ -55,7 +55,7 @@
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVII Jim Wigginton * @copyright MMVII Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt * @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 * @link http://phpseclib.sourceforge.net
*/ */
@ -208,7 +208,7 @@ class Crypt_RC4 {
{ {
$this->key = $key; $this->key = $key;
if (CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT) { if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
return; return;
} }
@ -277,7 +277,8 @@ class Crypt_RC4 {
* @access public * @access public
* @param String $plaintext * @param String $plaintext
*/ */
function encrypt($plaintext) { function encrypt($plaintext)
{
return $this->_crypt($plaintext, CRYPT_RC4_ENCRYPT); return $this->_crypt($plaintext, CRYPT_RC4_ENCRYPT);
} }
@ -291,7 +292,8 @@ class Crypt_RC4 {
* @access public * @access public
* @param String $ciphertext * @param String $ciphertext
*/ */
function decrypt($ciphertext) { function decrypt($ciphertext)
{
return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT); return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT);
} }
@ -304,7 +306,8 @@ class Crypt_RC4 {
* @param String $text * @param String $text
* @param Integer $mode * @param Integer $mode
*/ */
function _crypt($text, $mode) { function _crypt($text, $mode)
{
if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) { if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
$keyStream = $mode == CRYPT_RC4_ENCRYPT ? 'encryptStream' : 'decryptStream'; $keyStream = $mode == CRYPT_RC4_ENCRYPT ? 'encryptStream' : 'decryptStream';
@ -332,6 +335,10 @@ class Crypt_RC4 {
list($i, $j) = $this->decryptIndex; list($i, $j) = $this->decryptIndex;
} }
if ($keyStream === false) {
$this->setKey($this->key);
}
$newText = ''; $newText = '';
for ($k = 0; $k < strlen($text); $k++) { for ($k = 0; $k < strlen($text); $k++) {
$i = ($i + 1) & 255; $i = ($i + 1) & 255;

View File

@ -64,7 +64,7 @@
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVIII Jim Wigginton * @copyright MMVIII Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt * @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 * @link http://phpseclib.sourceforge.net
*/ */
@ -89,7 +89,7 @@ define('CRYPT_RIJNDAEL_MODE_CBC', 2);
/**#@+ /**#@+
* @access private * @access private
* @see Crypt_AES::Crypt_AES() * @see Crypt_Rijndael::Crypt_Rijndael()
*/ */
/** /**
* Toggles the internal implementation * Toggles the internal implementation
@ -158,7 +158,7 @@ class Crypt_Rijndael {
/** /**
* Continuous Buffer status * Continuous Buffer status
* *
* @see Crypt_DES::enableContinuousBuffer() * @see Crypt_Rijndael::enableContinuousBuffer()
* @var Boolean * @var Boolean
* @access private * @access private
*/ */
@ -167,7 +167,7 @@ class Crypt_Rijndael {
/** /**
* Padding status * Padding status
* *
* @see Crypt_DES::enablePadding() * @see Crypt_Rijndael::enablePadding()
* @var Boolean * @var Boolean
* @access private * @access private
*/ */
@ -358,7 +358,8 @@ class Crypt_Rijndael {
* @return Crypt_Rijndael * @return Crypt_Rijndael
* @access public * @access public
*/ */
function Crypt_Rijndael($mode = CRYPT_MODE_RIJNDAEL_CBC) { function Crypt_Rijndael($mode = CRYPT_MODE_RIJNDAEL_CBC)
{
switch ($mode) { switch ($mode) {
case CRYPT_RIJNDAEL_MODE_ECB: case CRYPT_RIJNDAEL_MODE_ECB:
case CRYPT_RIJNDAEL_MODE_CBC: case CRYPT_RIJNDAEL_MODE_CBC:

View File

@ -47,7 +47,7 @@
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVII Jim Wigginton * @copyright MMVII Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt * @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 * @link http://phpseclib.sourceforge.net
*/ */
@ -313,9 +313,7 @@ class Crypt_TripleDES {
*/ */
function encrypt($plaintext) 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 the key is smaller then 8, do what we'd normally do
if ($this->mode == CRYPT_DES_MODE_3CBC && strlen($this->key) > 8) { if ($this->mode == CRYPT_DES_MODE_3CBC && strlen($this->key) > 8) {

View File

@ -69,7 +69,7 @@
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVI Jim Wigginton * @copyright MMVI Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt * @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 * @link http://pear.php.net/package/Math_BigInteger
*/ */
@ -358,9 +358,9 @@ class Math_BigInteger {
$str = '0x'; $str = '0x';
while (strlen($x)) { while (strlen($x)) {
$part = substr($x, 0, 4); $part = substr($x, 0, 4);
$str.= dechex(bindec($part)); $str.= dechex(bindec($part));
$x = substr($x, 4); $x = substr($x, 4);
} }
if ($this->is_negative) { if ($this->is_negative) {
@ -792,7 +792,6 @@ class Math_BigInteger {
$product->value[$k] = $carry; $product->value[$k] = $carry;
// the above for loop is what the previous comment was talking about. the // the above for loop is what the previous comment was talking about. the
// following for loop is the "one with nested for loops" // following for loop is the "one with nested for loops"
@ -1864,7 +1863,6 @@ class Math_BigInteger {
break; break;
case MATH_BIGINTEGER_MODE_BCMATH: case MATH_BIGINTEGER_MODE_BCMATH:
$temp->value = bcdiv($this->value, bcpow('2', $shift)); $temp->value = bcdiv($this->value, bcpow('2', $shift));
break; break;
@ -2180,8 +2178,4 @@ class Math_BigInteger {
$temp = unpack('Nint', str_pad($x, 4, chr(0), STR_PAD_LEFT)); $temp = unpack('Nint', str_pad($x, 4, chr(0), STR_PAD_LEFT));
return $temp['int']; return $temp['int'];
} }
} }
// vim: ts=4:sw=4:et:
// vim6: fdl=1:
?>