PHP可逆加密/解密函数分享

前端之家收集整理的这篇文章主要介绍了PHP可逆加密/解密函数分享前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

函数源码
<div class="codetitle"><a style="CURSOR: pointer" data="40057" class="copybut" id="copybut40057" onclick="doCopy('code40057')"> 代码如下:

<div class="codebody" id="code40057">
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,$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,strlen($str) - $pad);
}
return unserialize($str);
}

调用函数
<div class="codetitle"><a style="CURSOR: pointer" data="31771" class="copybut" id="copybut31771" onclick="doCopy('code31771')"> 代码如下:
<div class="codebody" id="code31771">
$key = 'okyo.cn';
$data = array('id' => 100,'username' => 'customer','password' => 'e10adc3949ba59abbe56e057f20f883e');
$snarr = serialize($data);
$en = encrypt($data,$key);
$de = decrypt($en,$key);
echo "加密原型:";
print_r($data);
echo "
密钥:$key 加密结果:$en 解密结果:";
print_r($de);

原文链接:https://www.f2er.com/php/26966.html

猜你在找的PHP相关文章