pemString = $pemString; $this->opensslKeyResource = $result; $this->details = openssl_pkey_get_details($this->opensslKeyResource); } /** * Get the public key * * @return PublicKey */ public function getPublicKey() { if ($this->publicKey === null) { $this->publicKey = new PublicKey($this->details['key']); } return $this->publicKey; } /** * Encrypt using this key * * @param string $data * @return string * @throws Exception\RuntimeException * @throws Exception\InvalidArgumentException */ public function encrypt($data) { if (empty($data)) { throw new Exception\InvalidArgumentException('The data to encrypt cannot be empty'); } $encrypted = ''; $result = openssl_private_encrypt($data, $encrypted, $this->getOpensslKeyResource()); if (false === $result) { throw new Exception\RuntimeException( 'Can not encrypt; openssl ' . openssl_error_string() ); } return $encrypted; } /** * Decrypt using this key * * @param string $data * @return string * @throws Exception\RuntimeException * @throws Exception\InvalidArgumentException */ public function decrypt($data) { if (!is_string($data)) { throw new Exception\InvalidArgumentException('The data to decrypt must be a string'); } if ('' === $data) { throw new Exception\InvalidArgumentException('The data to decrypt cannot be empty'); } $decrypted = ''; $result = openssl_private_decrypt($data, $decrypted, $this->getOpensslKeyResource()); if (false === $result) { throw new Exception\RuntimeException( 'Can not decrypt; openssl ' . openssl_error_string() ); } return $decrypted; } /** * @return string */ public function toString() { return $this->pemString; } }