可以加入一个数字进行运算,加密解密
//ascii 不对称加密
function encode_ascii($str){
$c = $str;
$offset = 2762;//需要计算的数,可自定义
$len = strlen($c);//字符串长度
//echo $len;
//die();
$a = 0;
$scill = '';
while ($a < $len) {
$ud = ord($c{$a}) + $offset;
$dec = $ud;
$utf = '';
if ($dec < 128) {
$utf .= chr($dec);
} else if ($dec < 2048) {
$utf .= chr(192 + (($dec - ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
} else {
$utf .= chr(224 + (($dec - ($dec % 4096)) / 4096));
$utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
}
// echo $utf."<br>";
$scill .= $utf;
$a += 1;
}
// echo $scill;
$scill = base64_encode($scill);// 加密
// echo $scill;
// echo "<br>";
$scill2 = strrev($scill);//反转字符串
return $scill2;
}
//ascii 不对称加密的解密
function decode_ascii($str){
$offset = 2762;
$scill = strrev($str);//反转
$c = base64_decode($scill);//解密
$len = strlen($c);
// echo 'len: '.$len."<br>";
$a = 0;
$scill = '';
while ($a < $len) {
$ud = 0;
if (ord($c{$a}) >= 0 && ord($c{$a}) <= 127) {
$ud = ord($c{$a}) - $offset;
$a += 1;
} else if (ord($c{$a}) >= 192 && ord($c{$a}) <= 223) {
$ud = (ord($c{$a}) - 192) * 64 + (ord($c{$a + 1}) - 128) - $offset;
$a += 2;
} else if (ord($c{$a}) >= 224 && ord($c{$a}) <= 239) {
$ud = (ord($c{$a}) - 224) * 4096 + (ord($c{$a + 1}) - 128) * 64 + (ord($c{$a + 2}) - 128) - $offset;
$a += 3;
} else if (ord($c{$a}) >= 240 && ord($c{$a}) <= 247) {
$ud = (ord($c{$a}) - 240) * 262144 + (ord($c{$a + 1}) - 128) * 4096 + (ord($c{$a + 2}) - 128) * 64 + (ord($c{$a + 3}) - 128) - $offset;
$a += 4;
} else if (ord($c{$a}) >= 248 && ord($c{$a}) <= 251) {
$ud = (ord($c{$a}) - 248) * 16777216 + (ord($c{$a + 1}) - 128) * 262144 + (ord($c{$a + 2}) - 128) * 4096 + (ord($c{$a + 3}) - 128) * 64 + (ord($c{$a + 4}) - 128) - $offset;
$a += 5;
} else if (ord($c{$a}) >= 252 && ord($c{$a}) <= 253) {
$ud = (ord($c{$a}) - 252) * 1073741824 + (ord($c{$a + 1}) - 128) * 16777216 + (ord($c{$a + 2}) - 128) * 262144 + (ord($c{$a + 3}) - 128) * 4096 + (ord($c{$a + 4}) - 128) * 64 + (ord($c{$a + 5}) - 128) - $offset;
$a += 6;
} else if (ord($c{$a}) >= 254 && ord($c{$a}) <= 255) { //error
$ud = false;
}
// echo $ud .' : '.chr($ud)."<br>";
// $scill .= $ud ;//十进制
$scill .= chr($ud) ;
}
return $scill;
}
$offset 是用来计算的数字
例子
$uid = "190457545";
$sci = "/uK4+uK4/uK4ByK4/uK4+uK46uK4DyK47uK4";
echo $sci." 解密后 ". decode_ascii($sci);?
echo $uid.' 加密后 '.encode_ascii($uid);
|