/**
* Method to decrypt a data string.
*
* @param string $data The encrypted string to decrypt.
* @param Key $key The key object to use for decryption.
*
* @return string The decrypted data string.
*
* @since 3.5
* @throws \RuntimeException
*/
public function decrypt($data, Key $key)
{
// Validate key.
if ($key->getType() !== 'crypto') {
throw new \InvalidArgumentException('Invalid key of type: ' . $key->getType() . '. Expected crypto.');
}
// Decrypt the data.
try {
return \Crypto::Decrypt($data, $key->getPublic());
} catch (\InvalidCiphertextException $ex) {
throw new \RuntimeException('DANGER! DANGER! The ciphertext has been tampered with!', $ex->getCode(), $ex);
} catch (\CryptoTestFailedException $ex) {
throw new \RuntimeException('Cannot safely perform decryption', $ex->getCode(), $ex);
} catch (\CannotPerformOperationException $ex) {
throw new \RuntimeException('Cannot safely perform decryption', $ex->getCode(), $ex);
}
}