Home  >  Article  >  php教程  >  很赞的PHP字符串加密函数

很赞的PHP字符串加密函数

WBOY
WBOYOriginal
2016-07-11 20:00:391061browse

最近, 从discuz里面发现了一个很牛的加密解密函数。

此函数的厉害之处在于可以在指定时间内加密还原字符串,超时无法还

这样我们就可以拿此函数来做很多用途了,比如:单点登录的token加密传输啦,临时密码啦等等

 1 /**
 2   * @param string $string 原文或者密文
 3   * @param string $operation 操作(ENCODE | DECODE), 默认为 DECODE
 4   * @param string $key 密钥
 5    * @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效
 6    * @return string 处理后的 原文或者 经过 base64_encode 处理后的密文
 7    *
 8      * @example
 9      *
10      *  $a = authcode('abc', 'ENCODE', 'key');
11      *  $b = authcode($a, 'DECODE', 'key');  // $b(abc)
12      *
13      *  $a = authcode('abc', 'ENCODE', 'key', 3600);
14      *  $b = authcode('abc', 'DECODE', 'key'); // 在一个小时内,$b(abc),否则 $b 为空
15      */
16 function authcode($string, $operation = 'DECODE', $key = '', $expiry = 3600) {
17 
18         $ckey_length = 4;   
19         // 随机密钥长度 取值 0-32;
20         // 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
21         // 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
22         // 当此值为 0 时,则不产生随机密钥
23 
24         $key = md5($key ? $key : 'default_key'); //这里可以填写默认key值
25         $keya = md5(substr($key, 0, 16));
26         $keyb = md5(substr($key, 16, 16));
27         $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
28 
29         $cryptkey = $keya.md5($keya.$keyc);
30         $key_length = strlen($cryptkey);
31 
32         $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
33         $string_length = strlen($string);
34 
35         $result = '';
36         $box = range(0, 255);
37 
38         $rndkey = array();
39         for($i = 0$i  255$i++) {
40             $rndkey[$i= ord($cryptkey[$i % $key_length]);
41         }
42 
43         for($j = $i = 0$i  256$i++) {
44             $j = ($j + $box[$i+ $rndkey[$i]) % 256;
45             $tmp = $box[$i];
46             $box[$i= $box[$j];
47             $box[$j= $tmp;
48         }
49 
50         for($a = $j = $i = 0$i  $string_length$i++) {
51             $a = ($a + 1% 256;
52             $j = ($j + $box[$a]) % 256;
53             $tmp = $box[$a];
54             $box[$a= $box[$j];
55             $box[$j= $tmp;
56             $result .= chr(ord($string[$i]) ^ ($box[($box[$a+ $box[$j]) % 256]));
57         }
58 
59         if($operation == 'DECODE') {
60             if((substr($result, 0, 10== 0 || substr($result, 0, 10- time() > 0&& substr($result, 10, 16== substr(md5(substr($result, 26).$keyb), 0, 16)) {
61                 return substr($result, 26);
62             } else {
63                 return '';
64             }
65         } else {
66             return $keyc.str_replace('=', '', base64_encode($result));
67         }
68 
69     }


2010/6/4 修正了24行错误,特别感谢大唐秀提出此错误

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn