编程语言
首页 > 编程语言> > php加密解密方法

php加密解密方法

作者:互联网

function encrypt($data, $key) { 
$prep_code = serialize($data); 
$block = mcrypt_get_block_size('des', 'ecb'); 
if (($pad = $block - (strlen($prep_code) % $block)) < $block) { 
$prep_code .= str_repeat(chr($pad), $pad); 

$encrypt = mcrypt_encrypt(MCRYPT_DES, $key, $prep_code, MCRYPT_MODE_ECB); 
return base64_encode($encrypt); 


function decrypt($str, $key) { 
$str = base64_decode($str); 
$str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB); 
$block = mcrypt_get_block_size('des', 'ecb'); 
$pad = ord($str[($len = strlen($str)) - 1]); 
if ($pad && $pad < $block && preg_match('/' . chr($pad) . '{' . $pad . '}$/', $str)) { 
$str = substr($str, 0, strlen($str) - $pad); 

return unserialize($str); 

标签:code,加密,key,解密,pad,str,php,prep,block
来源: https://www.cnblogs.com/findher/p/10610566.html