[PHP]加密解密代码
发布于 2021-05-12 23:39 ,所属分类:区块连和PHP开发学习资料
<?php
$Cache = new ESApi();
$json = $Cache->encoded_url($_REQUEST['txt'],$_REQUEST['key']);
echo json_encode($json,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
class ESApi{
//编码
public function encoded_url($txt, $key = ''){
$txt = $txt . $key;
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
$nh = rand(0, 64);
$ch = $chars[$nh];
$mdKey = md5($key . $ch);
$mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
$txt = base64_encode($txt);
$tmp = '';
$i = 0;
$j = 0;
$k = 0;
for ($i = 0; $i < strlen($txt); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = ($nh + strpos($chars, $txt[$i]) + ord($mdKey[$k++])) % 64;
$tmp .= $chars[$j];
}
return urlencode(base64_encode($ch . $tmp));
}
//解码
public function decode_url($txt, $key = ''){
$txt = base64_decode(urldecode($txt));
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
$ch = $txt[0];
if(!empty($txt)){
$nh = strpos($chars, $ch);
}else{
$nh = $ch;
}
$mdKey = md5($key . $ch);
$mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
$txt = substr($txt, 1);
$tmp = '';
$i = 0;
$j = 0;
$k = 0;
for ($i = 0; $i < strlen($txt); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = strpos($chars, $txt[$i]) - $nh - ord($mdKey[$k++]);
while ($j < 0) {
$j += 64;
}
$tmp .= $chars[$j];
}
return trim(base64_decode($tmp), $key);
}
}
点分享
点收藏
点点赞
点在看
相关资源