1、设定密钥
2、JS加密
function handle_password(password_text) {
var key = 'yourkey';
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
var nh = Math.ceil(Math.random() * 64) - 1;
var ch = chars.charAt(nh);
var md_key = hex_md5(key + ch);
md_key = md_key.substr(nh % 8, nh % 8 + 7);
password_text = Base64.encode(password_text);
var tmp = '';
var i = 0, j = 0, k = 0;
for (i = 0; i < password_text.length; i++) {
k = k == md_key.length ? 0 : k;
j = (nh + chars.indexOf(password_text[i]) + md_key.charCodeAt(k++)) % 64;
tmp += chars[j];
}
return encodeURI(ch + tmp);
}
3、PHP加解密
public static function lock($txt, $key = 'yourkey')
{
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$nh = rand(0, strlen($chars) - 1);
$ch = $chars[$nh];
$mdKey = md5($key . $ch);
$mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
$txt = base64_encode($txt);
$tmp = '';
$k = 0;
for ($i = 0; $i < strlen($txt); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = ($nh + strpos($chars, $txt[$i]) + ord($mdKey[$k++])) % (strlen($chars) - 1);
$tmp .= $chars[$j];
}
return urlencode($ch . $tmp);
}
public static function unlock($txt, $key = 'yourkey')
{
$txt = urldecode($txt);
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
$ch = $txt[0];
$nh = strpos($chars, $ch);
$mdKey = md5($key . $ch);
$mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
$txt = substr($txt, 1);
$tmp = '';
$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 base64_decode($tmp);
}

发表评论 取消回复