/** * Set PEM string * * @param string $value * @return RsaOptions */ public function setPemString($value) { $this->pemString = $value; try { $this->privateKey = new Rsa\PrivateKey($this->pemString, $this->passPhrase); $this->publicKey = $this->privateKey->getPublicKey(); } catch (Rsa\Exception\RuntimeException $e) { $this->privateKey = null; $this->publicKey = new Rsa\PublicKey($this->pemString); } return $this; }
/** * RSA instance factory * * @param array|Traversable $options * @return Rsa * @throws Rsa\Exception\RuntimeException * @throws Rsa\Exception\InvalidArgumentException */ public static function factory($options) { if (!extension_loaded('openssl')) { throw new Exception\RuntimeException('Can not create Zend\\Crypt\\PublicKey\\Rsa; openssl extension to be loaded'); } if ($options instanceof Traversable) { $options = ArrayUtils::iteratorToArray($options); } elseif (!is_array($options)) { throw new Exception\InvalidArgumentException('The options parameter must be an array or a Traversable'); } $privateKey = null; $passPhrase = isset($options['pass_phrase']) ? $options['pass_phrase'] : null; if (isset($options['private_key'])) { if (is_file($options['private_key'])) { $privateKey = Rsa\PrivateKey::fromFile($options['private_key'], $passPhrase); } elseif (is_string($options['private_key'])) { $privateKey = new Rsa\PrivateKey($options['private_key'], $passPhrase); } else { throw new Exception\InvalidArgumentException('Parameter "private_key" must be PEM formatted string or path to key file'); } unset($options['private_key']); } $publicKey = null; if (isset($options['public_key'])) { if (is_file($options['public_key'])) { $publicKey = Rsa\PublicKey::fromFile($options['public_key']); } elseif (is_string($options['public_key'])) { $publicKey = new Rsa\PublicKey($options['public_key']); } else { throw new Exception\InvalidArgumentException('Parameter "public_key" must be PEM/certificate string or path to key/certificate file'); } unset($options['public_key']); } $options = new RsaOptions($options); if ($privateKey instanceof Rsa\PrivateKey) { $options->setPrivateKey($privateKey); } if ($publicKey instanceof Rsa\PublicKey) { $options->setPublicKey($publicKey); } return new Rsa($options); }
/** * Set private key * * @param Rsa\PrivateKey $key * @return RsaOptions */ public function setPrivateKey(Rsa\PrivateKey $key) { $this->privateKey = $key; $this->publicKey = $this->privateKey->getPublicKey(); return $this; }
public function testEncryptionWithPrivateKey() { $publicKey = new Rsa\PublicKey($this->_testCertificateString); $privateKey = new Rsa\PrivateKey($this->_testPemString); $encrypted = $privateKey->encrypt('1234567890'); $this->assertEquals('1234567890', $publicKey->decrypt($encrypted)); }