Skip to content

Instantly share code, notes, and snippets.

@resting
Forked from RiANOl/gist:1077723
Created August 22, 2012 02:41
Show Gist options
  • Save resting/3421760 to your computer and use it in GitHub Desktop.
Save resting/3421760 to your computer and use it in GitHub Desktop.
AES128 encrypt/decrypt in PHP with base64
<?
function aes128Encrypt($key, $data) {
if(16 !== strlen($key)) $key = hash('MD5', $key, true);
$padding = 16 - (strlen($data) % 16);
$data .= str_repeat(chr($padding), $padding);
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, str_repeat("\0", 16)));
}
function aes128Decrypt($key, $data) {
$data = base64_decode($data);
if(16 !== strlen($key)) $key = hash('MD5', $key, true);
$data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, str_repeat("\0", 16));
$padding = ord($data[strlen($data) - 1]);
return substr($data, 0, -$padding);
}
@Tilldeathdouspart54
Copy link

works awesome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment