Skip to content

Instantly share code, notes, and snippets.

@ikarius6
Last active August 29, 2015 14:09
Show Gist options
  • Save ikarius6/2459ec4afcea6bded8d0 to your computer and use it in GitHub Desktop.
Save ikarius6/2459ec4afcea6bded8d0 to your computer and use it in GitHub Desktop.
<?php
/*
AES256 v1.0 MrJack
https://gist.github.com/1077723/0a9a7c5299a7fb4340e4b2064fd383ef236a5aa5
https://gist.github.com/RiANOl/1077723
*/
function aes256Encrypt($key, $data, $mode = MCRYPT_MODE_CBC) {
if(32 !== strlen($key)) $key = hash('SHA256', $key, true);
$padding = 16 - (strlen($data) % 16);
$data .= str_repeat(chr($padding), $padding);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, $mode);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
return $iv.mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, $mode, $iv);
}
function aes256Decrypt($key, $data, $mode = MCRYPT_MODE_CBC) {
if(32 !== strlen($key)) $key = hash('SHA256', $key, true);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, $mode);
$iv_dec = substr($data, 0, $iv_size);
$data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, $mode, $iv_dec);
$padding = ord($data[strlen($data) - 1]);
$data = substr($data, 0, -$padding);
$data = substr($data, $iv_size);
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment