Back to CryptoCipher class

Method decrypt

public string
decrypt
(mixed $data, \Joomla\Crypt\Key $key)
Method to decrypt a data string.
Parameters
  • string $data The encrypted string to decrypt.
  • \Joomla\Crypt\Key $key The key object to use for decryption.
Returns
  • string The decrypted data string.
Since
  • 3.5
-
  • \RuntimeException
Class: CryptoCipher
Project: Joomla

Method decrypt - Source code

/**
 * 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);
    }
}