You are here

function aes_decrypt in AES encryption 5

Same name and namespace in other branches
  1. 6 aes.module \aes_decrypt()
  2. 7 aes.module \aes_decrypt()

Decrypts a string of encrypted data.

Parameters

string $string The string to decrypt.:

bool $base64encoded Whether this encrypted string is base64 encoded or not.:

string $custom_key Use this as the key rather than the stored one for this operation.:

string $custom_cipher Use this cipher rather than the default one.:

string $custom_iv Use this initialization vector instead of the default one.:

Return value

string The decrypted string.

2 calls to aes_decrypt()
aes_config_submit in ./aes.module
aes_get_password in ./aes.module

File

./aes.module, line 516

Code

function aes_decrypt($string, $base64encoded = true, $custom_key = null, $custom_cipher = null, $custom_iv = null) {
  if ($base64encoded) {
    $string = base64_decode($string);
  }
  if ($custom_cipher != null) {
    $cipher = $custom_cipher;
  }
  else {
    $cipher = variable_get("aes_cipher", "rijndael-128");
  }
  $td = mcrypt_module_open($cipher, "", MCRYPT_MODE_CBC, "");
  $ks = mcrypt_enc_get_key_size($td);
  if ($custom_iv == null) {
    $iv = base64_decode(variable_get("aes_encryption_iv", ""));
  }
  else {
    $iv = base64_decode($custom_iv);
  }
  if (empty($iv)) {
    watchdog("aes", "No initialization vector found while trying to decrypt. Aborting!", array(), WATCHDOG_ERROR);
  }
  if (!empty($custom_key)) {
    $key = $custom_key;
  }
  else {
    $key = aes_get_key();
  }
  $key = substr(sha1($key), 0, $ks);
  mcrypt_generic_init($td, $key, $iv);
  $decrypted = mdecrypt_generic($td, $string);
  mcrypt_generic_deinit($td);
  mcrypt_module_close($td);
  return $decrypted;
}