码迷,mamicode.com
首页 > Web开发 > 详细

各种加密解密函数(URL加密解密、sha1加密解密、des加密解密)

时间:2014-12-28 14:06:52      阅读:738      评论:0      收藏:0      [点我收藏+]

标签:

普通hash函数如md5、sha1、base64等都是不可逆函数。虽然我们利用php可以利用这些函数写出可逆函数来。但是跨语言时这类可逆函数非常难搞定。所以这时尽量使用AES DES RC4 Rabbit TripleDes这些方法。


包含超时的加密解密函数

 1 /** 
 2     * 加密 
 3     * @param string $string     要加密或解密的字符串 
 4     * @param string $operation 加密 ‘‘  解密 DECODE 
 5     * @param string $key        密钥,加密解密时保持一致 
 6     * @param int    $expiry 有效时长,单位:秒 
 7     * @return string 
 8     */  
 9     function encrypt_code($string, $expiry = 0, $key = ‘abc12345‘) {  
10     $ckey_length = 7;  
11     $key = md5($key ? $key : UC_KEY); //加密解密时这个是不变的  
12     $keya = md5(substr($key, 0, 16)); //加密解密时这个是不变的  
13     $keyb = md5(substr($key, 16, 16)); //加密解密时这个是不变的  
14     $keyc = $ckey_length ?  substr(md5(microtime()), -$ckey_length) : ‘‘;  
15     $cryptkey = $keya . md5($keya . $keyc); //64  
16     $key_length = strlen($cryptkey); //64  
17   
18     $string =sprintf(‘%010d‘, $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;  
19     $string_length = strlen($string);  
20   
21     $result = ‘‘;  
22     $box = range(0, 255);  
23   
24     $rndkey = array();  
25     for ($i = 0; $i <= 255; $i++) { //字母表 64位后重复 数列 范围为48~122  
26         $rndkey[$i] = ord($cryptkey[$i % $key_length]);  
27     }  
28   
29     for ($j = $i = 0; $i < 256; $i++) { //这里是一个打乱算法  
30         $j = ($j + $box[$i] + $rndkey[$i]) % 256;  
31         $tmp = $box[$i];  
32         $box[$i] = $box[$j];  
33         $box[$j] = $tmp;  
34     }  
35     for ($a = $j = $i = 0; $i < $string_length; $i++) {  
36         $result .= chr(ord($string[$i]) ^ ($box[$i]));  
37       
38     }  
39      $str =  $keyc . str_replace(‘=‘, ‘‘, urlsafe_b64encode($result));    
40                 //  $str =htmlentities($str, ENT_QUOTES, "UTF-8"); // curl 访问出错  
41                   return $str ;  
42 }  
43          
44          
45    /** 
46     * 解密 
47     * @param string $string     要加密或解密的字符串 
48     * @param string $operation 加密 ‘‘  解密 DECODE 
49     * @param string $key        密钥,加密解密时保持一致 
50     * @param int    $expiry 有效时长,单位:秒 
51     * @return string 
52     */  
53      function encrypt_decode($string, $expiry = 0,$key = ‘abc12345‘) {    
54                             //  $string = html_entity_decode($string, ENT_QUOTES, "UTF-8") ; //curl 访问出错  
55     $ckey_length = 7;  
56     $key = md5($key ? $key : UC_KEY); //加密解密时这个是不变的  
57     $keya = md5(substr($key, 0, 16)); //加密解密时这个是不变的  
58     $keyb = md5(substr($key, 16, 16)); //加密解密时这个是不变的  
59                
60     $keyc = $ckey_length ?  substr($string, 0, $ckey_length)   : ‘‘;  
61   
62     $cryptkey = $keya . md5($keya . $keyc); //64  
63     $key_length = strlen($cryptkey); //64  
64     $string = urlsafe_b64decode(substr($string, $ckey_length)) ;  
65                $string_length = strlen($string);  
66     $result = ‘‘;  
67     $box = range(0, 255);  
68   
69     $rndkey = array();  
70     for ($i = 0; $i <= 255; $i++) { //字母表 64位后重复 数列 范围为48~122  
71         $rndkey[$i] = ord($cryptkey[$i % $key_length]);  
72     }  
73     for ($j = $i = 0; $i < 256; $i++) { //这里是一个打乱算法  
74         $j = ($j + $box[$i] + $rndkey[$i]) % 256;  
75   
76         $tmp = $box[$i];  
77         $box[$i] = $box[$j];  
78         $box[$j] = $tmp;  
79     }  
80     for ($a = $j = $i = 0; $i < $string_length; $i++) {  
81         $result .= chr(ord($string[$i]) ^ ($box[$i]));  
82     }  
83           if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {  
84                        return substr($result, 26);  
85                } else {  
86                        return false;  
87                }  
88       
89 }  

最简单的往往是最好用的。

 

URL加密解密函数

 1     //加密函数  
 2     function lock_url($txt,$key=‘www.zhuoyuexiazai.com‘){  
 3         $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";  
 4         $nh = rand(0,64);  
 5         $ch = $chars[$nh];  
 6         $mdKey = md5($key.$ch);  
 7         $mdKey = substr($mdKey,$nh%8, $nh%8+7);  
 8         $txt = base64_encode($txt);  
 9         $tmp = ‘‘;  
10         $i=0;$j=0;$k = 0;  
11         for ($i=0; $i<strlen($txt); $i++) {  
12             $k = $k == strlen($mdKey) ? 0 : $k;  
13             $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;  
14             $tmp .= $chars[$j];  
15         }  
16         return urlencode($ch.$tmp);  
17     }  
18     //解密函数  
19     function unlock_url($txt,$key=‘www.zhuoyuexiazai.com‘){  
20         $txt = urldecode($txt);  
21         $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";  
22         $ch = $txt[0];  
23         $nh = strpos($chars,$ch);  
24         $mdKey = md5($key.$ch);  
25         $mdKey = substr($mdKey,$nh%8, $nh%8+7);  
26         $txt = substr($txt,1);  
27         $tmp = ‘‘;  
28         $i=0;$j=0; $k = 0;  
29         for ($i=0; $i<strlen($txt); $i++) {  
30             $k = $k == strlen($mdKey) ? 0 : $k;  
31             $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);  
32             while ($j<0) $j+=64;  
33             $tmp .= $chars[$j];  
34         }  
35         return base64_decode($tmp);  
36     }  

用户密码可逆加密解密函数:

 1     <?php  
 2     function passport_encrypt($txt, $key = ‘www.zhuoyuexiazai.com‘) {   
 3         srand((double)microtime() * 1000000);   
 4         $encrypt_key = md5(rand(0, 32000));   
 5         $ctr = 0;   
 6         $tmp = ‘‘;   
 7         for($i = 0;$i < strlen($txt); $i++) {   
 8         $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;   
 9         $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);   
10         }   
11         return urlencode(base64_encode(passport_key($tmp, $key)));   
12     }   
13       
14     function passport_decrypt($txt, $key = ‘www.zhuoyuexiazai.com‘) {   
15         $txt = passport_key(base64_decode(urldecode($txt)), $key);   
16         $tmp = ‘‘;   
17         for($i = 0;$i < strlen($txt); $i++) {   
18         $md5 = $txt[$i];   
19         $tmp .= $txt[++$i] ^ $md5;   
20         }   
21         return $tmp;   
22     }   
23       
24     function passport_key($txt, $encrypt_key) {   
25         $encrypt_key = md5($encrypt_key);   
26         $ctr = 0;   
27         $tmp = ‘‘;   
28         for($i = 0; $i < strlen($txt); $i++) {   
29         $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;   
30         $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];   
31         }   
32         return $tmp;   
33     }   
34     ?>  

测试方法:

1     <?php   
2     $txt = "1";   
3     $key = "testkey";   
4     $encrypt = passport_encrypt($txt,$key);   
5     $decrypt = passport_decrypt($encrypt,$key);   
6       
7     echo $encrypt."<br>";   
8     echo $decrypt."<br>";   
9     ?>   

SHA1的可逆加密解密函数:

 1     <?php  
 2     $string = "Helloworld";  
 3     echo $str1 = dencrypt($string, true, "www.miaohr.com");  
 4     echo $str2 = dencrypt($str1, false, "www.miaohr.com");  
 5       
 6     function dencrypt($string, $isEncrypt = true, $key = KEY_SPACE) {  
 7     if (!isset($string{0}) || !isset($key{0})) {  
 8     return false;  
 9     }  
10       
11     $dynKey = $isEncrypt ? hash(‘sha1‘, microtime(true)) : substr($string, 0, 40);  
12     $fixedKey = hash(‘sha1‘, $key);  
13       
14     $dynKeyPart1 = substr($dynKey, 0, 20);  
15     $dynKeyPart2 = substr($dynKey, 20);  
16     $fixedKeyPart1 = substr($fixedKey, 0, 20);  
17     $fixedKeyPart2 = substr($fixedKey, 20);  
18     $key = hash(‘sha1‘, $dynKeyPart1 . $fixedKeyPart1 . $dynKeyPart2 . $fixedKeyPart2);  
19       
20     $string = $isEncrypt ? $fixedKeyPart1 . $string . $dynKeyPart2 : (isset($string{339}) ? gzuncompress(base64_decode(substr($string, 40))) : base64_decode(substr($string, 40)));  
21       
22     $n = 0;  
23     $result = ‘‘;  
24     $len = strlen($string);  
25       
26     for ($n = 0; $n < $len; $n++) {  
27     $result .= chr(ord($string{$n}) ^ ord($key{$n % 40}));  
28     }  
29     return $isEncrypt ? $dynKey . str_replace(‘=‘, ‘‘, base64_encode($n > 299 ? gzcompress($result) : $result)) : substr($result, 20, -20);  
30     }  
31       
32     ?>  

DES的加密解密函数:

 1     <?php  
 2       
 3     $input =‘http://mlaan2.home.xs4all.nl/ispack/isetup-5.5.3.exe‘;  
 4      /** 
 5     *加密函数 
 6     *$input 要被加密的字符串 
 7     *$key 密钥 
 8     */  
 9       
10     $key = randomkeys(8);//生成随机密匙  
11     function do_mencrypt($input, $key)  
12     {  
13         $input = base64_encode(trim($input));  
14         //$key = substr(md5($key), 0, 4);  
15         $td = mcrypt_module_open(‘des‘, ‘‘, ‘ecb‘, ‘‘);  
16         $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);  
17         mcrypt_generic_init($td, $key, $iv);  
18         $encrypted_data = mcrypt_generic($td, $input);  
19         mcrypt_generic_deinit($td);  
20         mcrypt_module_close($td);  
21         return trim(base64_encode($encrypted_data));  
22     }  
23     print_r(do_mencrypt($input, $key));  
24     echo "<br/>";  
25     /** 
26     *解密函数 
27     *$input 要被解密的字符串 
28     *$key 密钥 
29     */  
30     $input1 = do_mencrypt($input, $key);  
31     function do_mdecrypt($input1, $key)  
32     {  
33         $input1 = base64_decode(trim($input1));  
34         $td = mcrypt_module_open(‘des‘, ‘‘, ‘ecb‘, ‘‘);  
35         //$key = substr(md5($key), 0, 4);  
36         $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);  
37         mcrypt_generic_init($td, $key, $iv);  
38         $decrypted_data = mdecrypt_generic($td, $input1);  
39         mcrypt_generic_deinit($td);  
40         mcrypt_module_close($td);  
41         return trim(base64_decode($decrypted_data));  
42     }  
43     print_r(do_mdecrypt($input1, $key));  
44            
45      #2.rand key: "CWSTOAYD":生成随机密匙,统一用字母或者数字,长度为8.      
46     function randomkeys($length)  
47     {  
48        $pattern = ‘1234567890‘;  
49         for($i=0;$i<$length;$i++)  
50         {  
51             @$key .= $pattern{rand(0,9)};    //生成php随机数  
52         }  
53         return $key;  
54     }  
55     ?>  

如果你感觉这些内容对你有帮助,那就收藏他吧。

各种加密解密函数(URL加密解密、sha1加密解密、des加密解密)

标签:

原文地址:http://www.cnblogs.com/easirm/p/4189901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!