/** * Decrypt the given message using the given private key * * @param KeyPair $key * @param string $encryptedText * @throws DecryptionException * @return string */ public function decrypt(KeyPair $key, $encryptedText) { $encryptedText = base64_decode($encryptedText); $success = openssl_private_decrypt($encryptedText, $result, $key->getPrivateKey()); if ($success !== TRUE) { throw new DecryptionException('Decryption failed'); } return $result; }
/** * Setup test environment * */ public function setUp() { if (!extension_loaded('openssl')) { throw new PrerequisitesNotMetError(PREREQUISITE_LIBRARYMISSING, $cause = NULL, array('openssl')); } if ($this->cert && $this->publickey && $this->privatekey) { return; } // Generate private & public key, using a self-signed certificate $keypair = KeyPair::generate(); $privatekey = $keypair->getPrivateKey(); $csr = new CSR(new Principal(array('C' => 'DE', 'ST' => 'Baden-Württemberg', 'L' => 'Karlsruhe', 'O' => 'XP', 'OU' => 'XP Team', 'CN' => 'XP Unittest', 'EMAIL' => '*****@*****.**')), $keypair); $cert = $csr->sign($keypair); $publickey = $cert->getPublicKey(); $this->cert = $cert; $this->publickey = $publickey; $this->privatekey = $privatekey; }
/** * 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); }
public function setPrivateKey(KeyPair $pk) { \debug("OpenSSL CSR: Assigning private key"); $this->pkey = $pk->getKey(); $this->pkeypass = $pk->getPassphrase(); }
/** * Decrypt the given message using the given key pair * * @param KeyPair $key * @param string $encryptedText * @return string */ public function decrypt(KeyPair $key, $encryptedText) { $rsa = new \Crypt_RSA(); $rsa->loadKey($key->getPrivateKey()); return $rsa->decrypt($encryptedText); }
/** * Constructor. * * @since 1.0.0 * @access protected */ protected function __construct() { parent::__construct('account'); }
/** * Constructor. * * @since 1.0.0 * @access protected * * @param string $domain The domain of this key pair. */ protected function __construct($domain) { parent::__construct($domain); }
/** * Decrypt the given message using the given key pair * * @abstract * @param KeyPair $key * @param string $encryptedText * @return string */ public function decrypt(KeyPair $key, $encryptedText) { return $this->rsaUtil($key->getPrivateKey(), $encryptedText, 'decrypt'); }