可逆加密函数改dz的支持数组。
<?php
/**
* $data 原文或者密文支持数组
* $key 密钥
* $operation 操作(0 | 1), 默认为0解密
* $expiry密文有效期, 加密时候有效, 单位 秒,0 为永久有效
*/
function AuthCode($data, $key = 'www.phpin.net', $operation = 0, $expiry = 0) {
$ckey_length = 6;
$keya = sha1($key);
$keyb = substr(md5($key), $ckey_length);
$data = $operation == 0 ? $data : serialize($data);
$keyc = $ckey_length ? ($operation == 0 ? substr($data, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
$cryptkey = $keya . md5($keya . $keyc);
$key_length = strlen($cryptkey);
$data = $operation == 0 ? base64_decode(substr($data, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($data . $keyb), 0, 16) . $data;
$data_length = strlen($data);
$result = '';
$box = range(0, 255);
$rndkey = array();
for ($i = 0;$i <= 255;$i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for ($j = $i = 0;$i < 256;$i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for ($a = $j = $i = 0;$i < $data_length;$i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result.= chr(ord($data[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if ($operation == 0) {
if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
return unserialize(substr($result, 26));
} else {
return '';
}
} else {
return $keyc . str_replace('=', '', base64_encode($result));
}
}
echo$a = AuthCode(array(1,'fsdg',3),'www.yonglan.net',1);
echo '<br />';
$b = AuthCode($a,'www.yonglan.net');//支持数组
print_r($b);
echo '<br />';
echo $a = AuthCode('13838389438', 'key', 1,3600);
echo '<br />';
echo$b = AuthCode($a, 'key'); // 在一个小时内,$b(abc),否则 $b 为空
echo '<br />';
页:
[1]