diff --git a/lib/openpgp.php b/lib/openpgp.php index 565f09b..531dedf 100644 --- a/lib/openpgp.php +++ b/lib/openpgp.php @@ -150,10 +150,12 @@ class OpenPGP_S2K { $bytes .= chr($this->hash_algorithm); break; case 1: + if(strlen($this->salt) != 8) throw new Exception("Invalid salt length"); $bytes .= chr($this->hash_algorithm); $bytes .= $this->salt; break; case 3: + if(strlen($this->salt) != 8) throw new Exception("Invalid salt length"); $bytes .= chr($this->hash_algorithm); $bytes .= $this->salt; $bytes .= chr(OpenPGP::encode_s2k_count($this->count)); diff --git a/lib/openpgp_crypt_symmetric.php b/lib/openpgp_crypt_symmetric.php index 3f608cb..8c811a5 100644 --- a/lib/openpgp_crypt_symmetric.php +++ b/lib/openpgp_crypt_symmetric.php @@ -40,7 +40,7 @@ class OpenPGP_Crypt_Symmetric { $esk = pack('n', OpenPGP::bitlength($esk)) . $esk; array_unshift($encrypted, new OpenPGP_AsymmetricSessionKeyPacket($pass->algorithm, $pass->fingerprint(), $esk)); } else if(is_string($pass)) { - $s2k = new OpenPGP_S2K(Random::string(10)); + $s2k = new OpenPGP_S2K(Random::string(8)); $cipher->setKey($s2k->make_key($pass, $key_bytes)); $esk = $cipher->encrypt(chr($symmetric_algorithm) . $key); array_unshift($encrypted, new OpenPGP_SymmetricSessionKeyPacket($s2k, $esk, $symmetric_algorithm)); diff --git a/tests/phpseclib_suite.php b/tests/phpseclib_suite.php index 70b93aa..1e5cf03 100644 --- a/tests/phpseclib_suite.php +++ b/tests/phpseclib_suite.php @@ -155,6 +155,7 @@ class Encryption extends PHPUnit_Framework_TestCase { public function oneSymmetric($algorithm) { $data = new OpenPGP_LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt')); $encrypted = OpenPGP_Crypt_Symmetric::encrypt('secret', new OpenPGP_Message(array($data)), $algorithm); + $encrypted = OpenPGP_Message::parse($encrypted->to_bytes()); $decrypted = OpenPGP_Crypt_Symmetric::decryptSymmetric('secret', $encrypted); $this->assertEquals($decrypted[0]->data, 'This is text.'); } @@ -193,6 +194,7 @@ class Encryption extends PHPUnit_Framework_TestCase { $key = OpenPGP_Message::parse(file_get_contents(dirname(__FILE__) . '/data/helloKey.gpg')); $data = new OpenPGP_LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt')); $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data))); + $encrypted = OpenPGP_Message::parse($encrypted->to_bytes()); $decryptor = new OpenPGP_Crypt_RSA($key); $decrypted = $decryptor->decrypt($encrypted); $this->assertEquals($decrypted[0]->data, 'This is text.');