mirror of
https://github.com/phpseclib/phpseclib.git
synced 2025-01-26 08:38:29 +00:00
AES: rm redundant setKey() method and fix setKeyLength()
This commit is contained in:
parent
2cc785fc54
commit
b85ce73d17
@ -155,43 +155,13 @@ class Crypt_AES extends Crypt_Rijndael
|
||||
*/
|
||||
function setKeyLength($length)
|
||||
{
|
||||
switch ($length) {
|
||||
case 160:
|
||||
$length = 192;
|
||||
break;
|
||||
case 224:
|
||||
$length = 256;
|
||||
}
|
||||
parent::setKeyLength($length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the key.
|
||||
*
|
||||
* Rijndael supports five different key lengths, AES only supports three.
|
||||
*
|
||||
* @see Crypt_Rijndael:setKey()
|
||||
* @see setKeyLength()
|
||||
* @access public
|
||||
* @param string $key
|
||||
*/
|
||||
function setKey($key)
|
||||
{
|
||||
parent::setKey($key);
|
||||
|
||||
if (!$this->explicit_key_length) {
|
||||
$length = strlen($key);
|
||||
switch (true) {
|
||||
case $length <= 16:
|
||||
$this->key_length = 16;
|
||||
break;
|
||||
case $length <= 24:
|
||||
$this->key_length = 24;
|
||||
break;
|
||||
default:
|
||||
$this->key_length = 32;
|
||||
}
|
||||
$this->_setEngine();
|
||||
switch ($this->key_length) {
|
||||
case 20:
|
||||
$this->key_length = 24;
|
||||
break;
|
||||
case 28:
|
||||
$this->key_length = 32;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -370,6 +370,35 @@ abstract class Unit_Crypt_AES_TestCase extends PhpseclibTestCase
|
||||
$this->assertSame($aes->getKeyLength(), 256);
|
||||
}
|
||||
|
||||
public function testInvalidLengthKeyWithAES()
|
||||
{
|
||||
$plaintext = str_repeat('x', 16);
|
||||
|
||||
$aes = new Crypt_Rijndael();
|
||||
$aes->setKey(str_repeat('a', 19));
|
||||
$this->assertSame($aes->getKeyLength(), 160);
|
||||
|
||||
$ref = new Crypt_Rijndael();
|
||||
$ref->setKey(str_repeat('a', 19) . "\0");
|
||||
$this->assertSame(
|
||||
bin2hex($aes->encrypt($plaintext)),
|
||||
bin2hex($ref->encrypt($plaintext)),
|
||||
'actual and expected value do not match for 168 bit Rijndael'
|
||||
);
|
||||
|
||||
$aes = new Crypt_AES();
|
||||
$aes->setKey(str_repeat('a', 19));
|
||||
$this->assertSame($aes->getKeyLength(), 192);
|
||||
|
||||
$ref = new Crypt_AES();
|
||||
$ref->setKey(str_repeat('a', 19) . "\0\0\0\0\0");
|
||||
$this->assertSame(
|
||||
bin2hex($aes->encrypt($plaintext)),
|
||||
bin2hex($ref->encrypt($plaintext)),
|
||||
'actual and expected value do not match for 168 bit AES'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group github938
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user