Skip to content

Instantly share code, notes, and snippets.

@LeoDT
Created May 10, 2013 01:49
Show Gist options
  • Save LeoDT/5551897 to your computer and use it in GitHub Desktop.
Save LeoDT/5551897 to your computer and use it in GitHub Desktop.
php encrypt to hex
function hex2bin($data)
{
return pack("H" . strlen($data), $data);
}
function encrypt($key, $data)
{
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_RAND);
return bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB, $iv));
}
function decrypt($key, $data)
{
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_RAND);
return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, hex2bin($data), MCRYPT_MODE_ECB, $iv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment