Back to OpenSSL class

Method decrypt

public string
decrypt
(mixed $cipherText, mixed $key)
Decrypts a string. Returns the raw binary plaintext.
Parameters
  • string $cipherText The ciphertext to encrypt
  • string $key The raw binary key (will be zero-padded or chopped if its size is different than the block size)
Returns
  • string The raw unencrypted binary string.
Class: OpenSSL
Project: Joomla

Method decrypt - Source code

/**
 * Decrypts a string. Returns the raw binary plaintext.
 *
 * $ciphertext MUST start with the IV followed by the ciphertext, even for EBC data (the first block of data is
 * dropped in EBC mode since there is no concept of IV in EBC).
 *
 * WARNING: The returned plaintext is zero-padded to the algorithm's block size during encryption. You are advised
 * to trim the string to the original plaintext's length upon decryption. While rtrim($decrypted, "\0") sounds
 * appealing it's NOT the correct approach for binary data (zero bytes may actually be part of your plaintext, not
 * just padding!).
 *
 * @param   string  $cipherText  The ciphertext to encrypt
 * @param   string  $key         The raw binary key (will be zero-padded or chopped if its size is different than the block size)
 *
 * @return  string  The raw unencrypted binary string.
 */
public function decrypt($cipherText, $key)
{
    $iv_size = $this->getBlockSize();
    $key = $this->resizeKey($key, $iv_size);
    $iv = substr($cipherText, 0, $iv_size);
    $cipherText = substr($cipherText, $iv_size);
    $plainText = openssl_decrypt($cipherText, $this->method, $key, $this->openSSLOptions, $iv);
    // Remove the zero padding
    return rtrim($plainText, "\x00");
}