예제 #1
0
 /**
  * Decrypt data with a public key.
  *
  * @param string $encryptedData
  *
  * @throws \LogicException
  *
  * @return string
  */
 protected function decrypt($encryptedData)
 {
     $publicKey = openssl_pkey_get_public($this->publicKey->getKeyPath());
     $publicKeyDetails = @openssl_pkey_get_details($publicKey);
     if ($publicKeyDetails === null) {
         throw new \LogicException(sprintf('Could not get details of public key: %s', $this->publicKey->getKeyPath()));
     }
     $chunkSize = ceil($publicKeyDetails['bits'] / 8);
     $output = '';
     $encryptedData = base64_decode($encryptedData);
     while ($encryptedData) {
         $chunk = substr($encryptedData, 0, $chunkSize);
         $encryptedData = substr($encryptedData, $chunkSize);
         if (openssl_public_decrypt($chunk, $decrypted, $publicKey) === false) {
             // @codeCoverageIgnoreStart
             throw new \LogicException('Failed to decrypt data');
             // @codeCoverageIgnoreEnd
         }
         $output .= $decrypted;
     }
     openssl_pkey_free($publicKey);
     return $output;
 }