From 413741fa84e6874aa88a1ae6029adf4060e89374 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 25 Jul 2018 09:56:57 -0500 Subject: [PATCH] Throw more helpful exception when already decrypted --- lib/openpgp_crypt_symmetric.php | 3 +++ tests/phpseclib_suite.php | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/lib/openpgp_crypt_symmetric.php b/lib/openpgp_crypt_symmetric.php index 844c53b..c853379 100644 --- a/lib/openpgp_crypt_symmetric.php +++ b/lib/openpgp_crypt_symmetric.php @@ -146,6 +146,9 @@ class OpenPGP_Crypt_Symmetric { public static function getCipher($algo) { $cipher = NULL; switch($algo) { + case NULL: + case 0: + throw new Exception("Data is already unencrypted"); case 2: $cipher = new Crypt_TripleDES(CRYPT_DES_MODE_CFB); $key_bytes = 24; diff --git a/tests/phpseclib_suite.php b/tests/phpseclib_suite.php index 0f6ae30..6e7f2e5 100644 --- a/tests/phpseclib_suite.php +++ b/tests/phpseclib_suite.php @@ -114,6 +114,13 @@ class Decryption extends PHPUnit_Framework_TestCase { $skey = OpenPGP_Crypt_Symmetric::decryptSecretKey("hello", $key[0]); $this->assertSame(!!$skey, true); } + + public function testAlreadyDecryptedSecretKey() { + $this->expectException(Exception::class); + $this->expectExceptionMessage("Data is already unencrypted"); + $key = OpenPGP_Message::parse(file_get_contents(dirname(__FILE__) . '/data/helloKey.gpg')); + OpenPGP_Crypt_Symmetric::decryptSecretKey("hello", $key[0]); + } } class Encryption extends PHPUnit_Framework_TestCase {