diff --git a/src/Encryption/AbstractEncryptionAdapter.php b/src/Encryption/AbstractEncryptionAdapter.php index 907df45..0322866 100644 --- a/src/Encryption/AbstractEncryptionAdapter.php +++ b/src/Encryption/AbstractEncryptionAdapter.php @@ -69,7 +69,7 @@ public function generateKey(): string public function parseNonce(string &$message): string { if (mb_strlen($message, static::ENCODING) < $this->getNonceSize()) { - throw new BadNonceException('Unable to parse nounce from message'); + throw new BadNonceException('Unable to parse nounce from message "'.$message.'"'); } $nonce = mb_substr($message, 0, $this->getNonceSize(), static::ENCODING); $message = mb_substr($message, $this->getNonceSize(), null, static::ENCODING); diff --git a/src/Manager/EncryptionManager.php b/src/Manager/EncryptionManager.php index 56aa344..886cf92 100644 --- a/src/Manager/EncryptionManager.php +++ b/src/Manager/EncryptionManager.php @@ -120,11 +120,15 @@ public function encryptString(string $string, string $nonce = null): string */ public function decryptString(string $encryptedString, string $nonce = null): string { - if ($nonce === null) { - $nonce = $this->encryptionAdapter->parseNonce($encryptedString); + // Do not try to decrypt blank string + if ($encryptedString === '') { + return ''; } try { + if ($nonce === null) { + $nonce = $this->encryptionAdapter->parseNonce($encryptedString); + } $decrypted = $this->encryptionAdapter->decrypt( $encryptedString, $nonce, @@ -134,10 +138,10 @@ public function decryptString(string $encryptedString, string $nonce = null): st if ($this->throwExceptions) { throw $exception; } - + return ''; } - + return rtrim($decrypted, "\0"); } diff --git a/tests/PHPUnit/Adapter/Aes256GcmSodiumEncryptionAdapterTest.php b/tests/PHPUnit/Adapter/Aes256GcmSodiumEncryptionAdapterTest.php index 4056308..2d0b883 100644 --- a/tests/PHPUnit/Adapter/Aes256GcmSodiumEncryptionAdapterTest.php +++ b/tests/PHPUnit/Adapter/Aes256GcmSodiumEncryptionAdapterTest.php @@ -37,6 +37,7 @@ public function getDataProvider(): array [bin2hex(random_bytes(400))], [bin2hex(random_bytes(1000))], [bin2hex(random_bytes(10000))], + [''], ]; }