/**
  * Decrypt using a private key
  *
  * @param string $msg
  * @param string $privateKey
  * @param string $passPhrase
  * @param string $id
  * @return string
  * @throws RuntimeException
  */
 public function decrypt($msg, $privateKey = null, $passPhrase = null, $id = null)
 {
     // get the session key
     list($encKeys, $ciphertext) = explode(';', $msg, 2);
     $keys = explode(':', $encKeys);
     $pos = array_search(base64_encode($id), $keys);
     if (false === $pos) {
         throw new Exception\RuntimeException("This private key cannot be used for decryption");
     }
     if (!$privateKey instanceof PrivateKey && !is_string($privateKey)) {
         throw new Exception\RuntimeException(sprintf("The private key must be a string in PEM format or an instance of %s", PrivateKey::class));
     }
     $privateKey = is_string($privateKey) ? new PrivateKey($privateKey, $passPhrase) : $privateKey;
     // decrypt the session key with privateKey
     $sessionKey = $this->rsa->decrypt(base64_decode($keys[$pos + 1]), $privateKey);
     // decrypt the plaintext with the blockcipher algorithm
     $this->bCipher->setKey($sessionKey);
     return $this->bCipher->decrypt($ciphertext, $sessionKey);
 }
Пример #2
0
 public function testEncryptionUsingPrivateKeyBase64Encryption()
 {
     $rsa = new RSA(array('pemString' => $this->_testPemString));
     $encrypted = $rsa->encrypt('1234567890', $rsa->getPrivateKey(), RSA::BASE64);
     $this->assertEquals('1234567890', $rsa->decrypt($encrypted, $rsa->getPublicKey(), RSA::BASE64));
 }