很贊的PHP字符串加密函數


最近, 從discuz里面發現了一個很牛的加密解密函數。

此函數的厲害之處在於可以在指定時間內加密還原字符串,超時無法還

這樣我們就可以拿此函數來做很多用途了,比如:單點登錄的token加密傳輸啦,臨時密碼啦等等

 1 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 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     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM