3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Crypt\PublicKey\Rsa
;
15 class PrivateKey
extends AbstractKey
22 protected $publicKey = null;
25 * Create private key instance from PEM formatted key file
27 * @param string $pemFile
28 * @param string|null $passPhrase
30 * @throws Exception\InvalidArgumentException
32 public static function fromFile($pemFile, $passPhrase = null)
34 if (!is_readable($pemFile)) {
35 throw new Exception\
InvalidArgumentException(
36 "PEM file '{$pemFile}' is not readable"
40 return new static(file_get_contents($pemFile), $passPhrase);
46 * @param string $pemString
47 * @param string $passPhrase
48 * @throws Exception\RuntimeException
50 public function __construct($pemString, $passPhrase = null)
52 $result = openssl_pkey_get_private($pemString, $passPhrase);
53 if (false === $result) {
54 throw new Exception\
RuntimeException(
55 'Unable to load private key; openssl ' . openssl_error_string()
59 $this->pemString
= $pemString;
60 $this->opensslKeyResource
= $result;
61 $this->details
= openssl_pkey_get_details($this->opensslKeyResource
);
69 public function getPublicKey()
71 if ($this->publicKey
=== null) {
72 $this->publicKey
= new PublicKey($this->details
['key']);
75 return $this->publicKey
;
79 * Encrypt using this key
82 * @param integer $padding
84 * @throws Exception\RuntimeException
85 * @throws Exception\InvalidArgumentException
87 public function encrypt($data, $padding = OPENSSL_PKCS1_PADDING
)
90 throw new Exception\
InvalidArgumentException('The data to encrypt cannot be empty');
94 $result = openssl_private_encrypt($data, $encrypted, $this->getOpensslKeyResource(), $padding);
95 if (false === $result) {
96 throw new Exception\
RuntimeException(
97 'Can not encrypt; openssl ' . openssl_error_string()
105 * Decrypt using this key
106 * Starting in 2.4.9/2.5.2, we changed the default padding to
107 * OPENSSL_PKCS1_OAEP_PADDING to prevent Bleichenbacher's chosen-ciphertext
110 * @see http://archiv.infsec.ethz.ch/education/fs08/secsem/bleichenbacher98.pdf
111 * @param string $data
112 * @param integer $padding
114 * @throws Exception\RuntimeException
115 * @throws Exception\InvalidArgumentException
117 public function decrypt($data, $padding = OPENSSL_PKCS1_OAEP_PADDING
)
119 if (!is_string($data)) {
120 throw new Exception\
InvalidArgumentException('The data to decrypt must be a string');
123 throw new Exception\
InvalidArgumentException('The data to decrypt cannot be empty');
127 $result = openssl_private_decrypt($data, $decrypted, $this->getOpensslKeyResource(), $padding);
128 if (false === $result) {
129 throw new Exception\
RuntimeException(
130 'Can not decrypt; openssl ' . openssl_error_string()
140 public function toString()
142 return $this->pemString
;