Crypt/Base: add initial exception support

To accomodate this decrypt() can no longer return false. If padding is enabled and the padding length is invalid (the only condition under which false was returned) an InvalidInputException will be thrown.

Also, previously, when encrypting, if the padding was disabled and the plaintext wasn't a multiple of the block size a E_USER_NOTICE would be issued via user_error() and then padding would be enabled. Now it's not enabled - an exception is thrown suggesting that you might want to enable padding and that's that.
This commit is contained in:
terrafrost 2015-07-15 12:50:57 -05:00
parent 489a723d33
commit 3cb0248f99
3 changed files with 43 additions and 5 deletions

View File

@ -37,6 +37,8 @@
namespace phpseclib\Crypt;
use phpseclib\Crypt\Hash;
use phpseclib\Exception\InvalidInputException;
use phpseclib\Exception\KeyGenerationException;
/**
* Base Class for all \phpseclib\Crypt\* cipher classes
@ -579,8 +581,7 @@ abstract class Base
$hashObj = new Hash();
$hashObj->setHash($hash);
if ($dkLen > $hashObj->getLength()) {
user_error('Derived key too long');
return false;
throw new KeyGenerationException('Derived key too long');
}
$t = $password . $salt;
for ($i = 0; $i < $count; ++$i) {
@ -1779,8 +1780,7 @@ abstract class Base
if ($length % $this->block_size == 0) {
return $text;
} else {
user_error("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size})");
$this->padding = true;
throw new InvalidInputException("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size}). Try enabling padding.");
}
}
@ -1809,7 +1809,7 @@ abstract class Base
$length = ord($text[strlen($text) - 1]);
if (!$length || $length > $this->block_size) {
return false;
throw new InvalidInputException("The ciphertext has an invalid padding length ($length) compared to the block size ({$this->block_size})");
}
return substr($text, 0, -$length);

View File

@ -0,0 +1,19 @@
<?php
/**
* InvalidInputException
*
* PHP version 5
*
* @category Exception
* @package InvalidInputException
* @copyright 2015 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
namespace phpseclib\Exception;
class InvalidInputException extends \Exception
{
}

View File

@ -0,0 +1,19 @@
<?php
/**
* KeyGenerationException
*
* PHP version 5
*
* @category Exception
* @package KeyGenerationException
* @copyright 2015 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
namespace phpseclib\Exception;
class KeyGenerationException extends \Exception
{
}