Exemplo n.º 1
0
 /**
  * Re-export the private key to change or disable the passphrase
  *
  * @param KeyPair $keyPair
  * @param string $passphrase Passphrase for opening the key pair
  * @param string $exportPassphrase Passphrase for the exported key pair (NULL for unencrypted private key)
  * @return KeyPair
  * @throws \InvalidArgumentException
  * @throws InvalidPassphraseException
  */
 protected function exportKeyPair($keyPair, $passphrase, $exportPassphrase = null)
 {
     $privateKey = null;
     $encrypted = $exportPassphrase !== null;
     $key = openssl_pkey_get_private($keyPair->getPrivateKey(), $passphrase);
     if ($key === false) {
         throw new InvalidPassphraseException('Invalid passphrase, could not open key', 1300101137);
     }
     openssl_pkey_export($key, $privateKey, $exportPassphrase);
     openssl_free_key($key);
     return new KeyPair($privateKey, $keyPair->getPublicKey(), $encrypted);
 }
Exemplo n.º 2
0
 /**
  * Decrypt the given message using the given private key
  *
  * @param KeyPair $key
  * @param string $encryptedText
  * @param bool $public decryp with public key
  * @throws DecryptionException
  * @return string
  */
 public function decrypt(KeyPair $key, $encryptedText, $public = false)
 {
     $encryptedText = base64_decode($encryptedText);
     if ($public) {
         $success = openssl_public_decrypt($encryptedText, $result, $key->getPublicKey(true));
     } else {
         $success = openssl_private_decrypt($encryptedText, $result, $key->getPrivateKey());
     }
     if ($success !== TRUE) {
         throw new DecryptionException('Decryption failed');
     }
     return $result;
 }