diff --git a/src/BlockCipher.php b/src/BlockCipher.php index 0b9b0e8..d2c7517 100644 --- a/src/BlockCipher.php +++ b/src/BlockCipher.php @@ -391,9 +391,19 @@ public function getPbkdf2HashAlgorithm() public function encrypt($data) { // 0 (as integer), 0.0 (as float) & '0' (as string) will return false, though these should be allowed - if (!is_string($data) || $data === '') { + // Must be a string, integer, or float in order to encrypt + if ((is_string($data) && $data === '') + || is_array($data) + || is_object($data) + ) { throw new Exception\InvalidArgumentException('The data to encrypt cannot be empty'); } + + // Cast to string prior to encrypting + if (!is_string($data)) { + $data = (string) $data; + } + if (empty($this->cipher)) { throw new Exception\InvalidArgumentException('No symmetric cipher specified'); } diff --git a/src/Symmetric/Mcrypt.php b/src/Symmetric/Mcrypt.php index 7410a8d..2c6c90d 100755 --- a/src/Symmetric/Mcrypt.php +++ b/src/Symmetric/Mcrypt.php @@ -326,7 +326,8 @@ public function getPadding() */ public function encrypt($data) { - if (empty($data)) { + // Cannot encrypt empty string + if (!is_string($data) || $data === '') { throw new Exception\InvalidArgumentException('The data to encrypt cannot be empty'); } if (null === $this->getKey()) { diff --git a/test/BlockCipherTest.php b/test/BlockCipherTest.php index 999817b..14826ff 100644 --- a/test/BlockCipherTest.php +++ b/test/BlockCipherTest.php @@ -156,6 +156,32 @@ public function testEncryptDecryptUsingBinary() } } + public function zeroValuesProvider() + { + return array( + '"0"' => array(0), + '"0.0"' => array(0.0), + '"0"' => array('0'), + ); + } + + /** + * @dataProvider zeroValuesProvider + */ + public function testEncryptDecryptUsingZero($value) + { + $this->blockCipher->setKey('test'); + $this->blockCipher->setKeyIteration(1000); + foreach ($this->blockCipher->getCipherSupportedAlgorithms() as $algo) { + $this->blockCipher->setCipherAlgorithm($algo); + + $encrypted = $this->blockCipher->encrypt($value); + $this->assertTrue(!empty($encrypted)); + $decrypted = $this->blockCipher->decrypt($encrypted); + $this->assertEquals($value, $decrypted); + } + } + public function testDecryptAuthFail() { $this->blockCipher->setKey('test');