Skip to content

Instantly share code, notes, and snippets.

@sohalloran
Last active December 28, 2015 07:39
Show Gist options
  • Save sohalloran/7465679 to your computer and use it in GitHub Desktop.
Save sohalloran/7465679 to your computer and use it in GitHub Desktop.
PHP AES decryption example Can be tested here: http://phpfiddle.org/main/code/7465540
<?php
function decrypt_data($data, $iv, $key) {
$cypher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
if(is_null($iv)) {
$ivlen = mcrypt_enc_get_iv_size($cypher);
$iv = substr($data, 0, $ivlen);
$data = substr($data, $ivlen);
}
// initialize encryption handle
if (mcrypt_generic_init($cypher, $key, $iv) != -1) {
// decrypt
$decrypted = mdecrypt_generic($cypher, $data);
// clean up
mcrypt_generic_deinit($cypher);
mcrypt_module_close($cypher);
return $decrypted;
}
return false;
}
function decrypt($data) {
$ivLen = 16;
$key = "tQfeGChqyHEApg7dd76wYQ==";
$iv = substr($data,0,$ivLen);
$dec = decrypt_data(base64_decode($data), $iv, base64_decode($key));
return substr($dec,$ivLen);
}
echo decrypt("Fw6/UWuGpjHDhIt2mVfnXfQ1OKlEbnAfgjaDRMy80uXQs0fi0qm4jJQeOwLcXgH9");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment