四种好用的PHP自定义加密函数(可逆/不可逆)
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">项目中有时<span style="color: black;">咱们</span><span style="color: black;">必须</span><span style="color: black;">运用</span>PHP将特定的信息进行加密,<span style="color: black;">亦</span><span style="color: black;">便是</span><span style="color: black;">经过</span>加密算法生成一个加密字符串,这些加密后的字符串<span style="color: black;">能够</span><span style="color: black;">经过</span>解密算法进行解密,便于程序对解密后的信息进行处理。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最<span style="color: black;">平常</span>的应用在用户登录以及<span style="color: black;">有些</span>API数据交换的场景。最<span style="color: black;">平常</span>的应用在用户登录以及<span style="color: black;">有些</span>API数据交换的场景。加密解密原理<span style="color: black;">通常</span>都是<span style="color: black;">经过</span><span style="color: black;">必定</span>的加密解密算法,将密钥加入到算法中,<span style="color: black;">最后</span>得到加密解密结果。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">废话不多说,直接上代码。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、</span><span style="color: black;">第1</span>种针<span style="color: black;">针对</span>ID的可逆加密函数,<span style="color: black;">亦</span><span style="color: black;">能够</span>用作于邀请码之类的,解密后的数据比较简单</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例:lockcode(28)=》000X unlockcode(000X)=》28</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">//加密函数
function lockcode($code) {
static $source_string = E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ;
$num = $code;
$code = ;
while ( $num > 0) {
$mod = $num % 35;
$num = ($num - $mod) / 35;
$code = $source_string[$mod].$code;
}
if(empty($code))
$code = str_pad($code,4,0,STR_PAD_LEFT);
return $code;
}
//解密函数
function unlockcode($code) {
static $source_string = E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ;
if (strrpos($code, 0) !== false)
$code = substr($code, strrpos($code, 0)+1);
$len = strlen($code);
$code = strrev($code);
$num = 0;
for ($i=0; $i < $len; $i++) {
$num += strpos($source_string, $code[$i]) * pow(35, $i);
}
return $num;
}</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、</span>第二种是加密函数是我在网上搜索来的,很好用,可逆加密,支持盐值参数</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">encrypt(abcd,1234)=》nkiV93IfJ decrypt(nkiV93IfJ,1234)=》abcd</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">//加密函数
function encrypt($data,$key=CHENI){
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$nh = rand(0,64);
$ch = $chars[$nh];
$mdKey = md5($key.$ch);
$mdKey = substr($mdKey,$nh%8, $nh%8+7);
$data= base64_encode($data);
$tmp = ;
$i=0;$j=0;$k = 0;
for ($i=0; $i<strlen($data); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = ($nh+strpos($chars,$data[$i])+ord($mdKey[$k++]))%64;
$tmp .= $chars[$j];
}
return urlencode($ch.$tmp);
}
//解密函数</div>
这篇文章真的让我受益匪浅,外链发布感谢分享! 感谢楼主的分享!我学到了很多。 感谢你的精彩评论,为我的思绪打开了新的窗口。 感谢您的精彩评论,为我带来了新的思考角度。
页:
[1]