Back to SodiumCipher 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.8.0
-
  • \RuntimeException
Class: SodiumCipher
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.8.0
 * @throws  \RuntimeException
 */
public function decrypt($data, Key $key)
{
    // Validate key.
    if ($key->getType() !== 'sodium') {
        throw new \InvalidArgumentException('Invalid key of type: ' . $key->getType() . '.  Expected sodium.');
    }
    if (!$this->nonce) {
        throw new \RuntimeException('Missing nonce to decrypt data');
    }
    $decrypted = Compat::crypto_box_open($data, $this->nonce, Compat::crypto_box_keypair_from_secretkey_and_publickey($key->getPrivate(), $key->getPublic()));
    if ($decrypted === false) {
        throw new \RuntimeException('Malformed message or invalid MAC');
    }
    return $decrypted;
}