【加密解密】PHP中常用的加密解密方法

php加密解密:php加密和解密函数通常可以用来加密一些有用的字符串存放在数据库里或作为各个子系统间同步登陆的令牌,并且通过解密算法解密字符串,该函数使用了base64和MD5加密和解密。

①第一种加密解密算法

[php]  view plain  copy
  1. <?php  
  2. function encryptDecrypt($key$string$decrypt){   
  3.     if($decrypt){   
  4.         $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "12");   
  5.         return $decrypted;   
  6.     }else{   
  7.         $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));   
  8.         return $encrypted;   
  9.     }   
  10. }   
  11.   
  12. //加密:"z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk="  
  13. echo encryptDecrypt('password''Helloweba欢迎您',0);   
  14. //解密:"Helloweba欢迎您"  
  15. echo encryptDecrypt('password''z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=',1);  
  16. ?>  

②第二种加密解密算法:

[php]  view plain  copy
  1. <?php  
  2. //加密函数  
  3. function lock_url($txt,$key='www.zhuoyuexiazai.com'){  
  4.     $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";  
  5.     $nh = rand(0,64);  
  6.     $ch = $chars[$nh];  
  7.     $mdKey = md5($key.$ch);  
  8.     $mdKey = substr($mdKey,$nh%8, $nh%8+7);  
  9.     $txt = base64_encode($txt);  
  10.     $tmp = '';  
  11.     $i=0;$j=0;$k = 0;  
  12.     for ($i=0; $i<strlen($txt); $i++) {  
  13.         $k = $k == strlen($mdKey) ? 0 : $k;  
  14.         $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;  
  15.         $tmp .= $chars[$j];  
  16.     }  
  17.     return urlencode($ch.$tmp);  
  18. }  
  19. //解密函数  
  20. function unlock_url($txt,$key='www.zhuoyuexiazai.com'){  
  21.     $txt = urldecode($txt);  
  22.     $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";  
  23.     $ch = $txt[0];  
  24.     $nh = strpos($chars,$ch);  
  25.     $mdKey = md5($key.$ch);  
  26.     $mdKey = substr($mdKey,$nh%8, $nh%8+7);  
  27.     $txt = substr($txt,1);  
  28.     $tmp = '';  
  29.     $i=0;$j=0; $k = 0;  
  30.     for ($i=0; $i<strlen($txt); $i++) {  
  31.         $k = $k == strlen($mdKey) ? 0 : $k;  
  32.         $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);  
  33.         while ($j<0) $j+=64;  
  34.         $tmp .= $chars[$j];  
  35.     }  
  36.     return base64_decode($tmp);  
  37. }  
  38. ?>  
③第三种加密解密算法:
[php]  view plain  copy
  1. <?php  
  2.   
  3. //改进后的算法  
  4. //加密函数  
  5. function lock_url($txt,$key='zhuoyuexiazai'){  
  6.     $txt = $txt.$key;  
  7.     $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";  
  8.     $nh = rand(0,64);  
  9.     $ch = $chars[$nh];  
  10.     $mdKey = md5($key.$ch);  
  11.     $mdKey = substr($mdKey,$nh%8, $nh%8+7);  
  12.     $txt = base64_encode($txt);  
  13.     $tmp = '';  
  14.     $i=0;$j=0;$k = 0;  
  15.     for ($i=0; $i<strlen($txt); $i++) {  
  16.         $k = $k == strlen($mdKey) ? 0 : $k;  
  17.         $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;  
  18.         $tmp .= $chars[$j];  
  19.     }  
  20.     return urlencode(base64_encode($ch.$tmp));  
  21. }  
  22. //解密函数  
  23. function unlock_url($txt,$key='zhuoyuexiazai'){  
  24.     $txt = base64_decode(urldecode($txt));  
  25.     $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";  
  26.     $ch = $txt[0];  
  27.     $nh = strpos($chars,$ch);  
  28.     $mdKey = md5($key.$ch);  
  29.     $mdKey = substr($mdKey,$nh%8, $nh%8+7);  
  30.     $txt = substr($txt,1);  
  31.     $tmp = '';  
  32.     $i=0;$j=0; $k = 0;  
  33.     for ($i=0; $i<strlen($txt); $i++) {  
  34.         $k = $k == strlen($mdKey) ? 0 : $k;  
  35.         $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);  
  36.         while ($j<0) $j+=64;  
  37.         $tmp .= $chars[$j];  
  38.     }  
  39.     return trim(base64_decode($tmp),$key);  
  40. }  
  41.   
  42. ?>  
④第四种加密解密算法:
[php]  view plain  copy
  1. <?php  
  2.   
  3. function passport_encrypt($txt$key = 'www.zhuoyuexiazai.com') {   
  4.     srand((double)microtime() * 1000000);   
  5.     $encrypt_key = md5(rand(0, 32000));   
  6.     $ctr = 0;   
  7.     $tmp = '';   
  8.     for($i = 0;$i < strlen($txt); $i++) {   
  9.     $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;   
  10.     $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);   
  11.     }   
  12.     return urlencode(base64_encode(passport_key($tmp$key)));   
  13. }   
  14.   
  15. function passport_decrypt($txt$key = 'www.zhuoyuexiazai.com') {   
  16.     $txt = passport_key(base64_decode(urldecode($txt)), $key);   
  17.     $tmp = '';   
  18.     for($i = 0;$i < strlen($txt); $i++) {   
  19.     $md5 = $txt[$i];   
  20.     $tmp .= $txt[++$i] ^ $md5;   
  21.     }   
  22.     return $tmp;   
  23. }   
  24.   
  25. function passport_key($txt$encrypt_key) {   
  26.     $encrypt_key = md5($encrypt_key);   
  27.     $ctr = 0;   
  28.     $tmp = '';   
  29.     for($i = 0; $i < strlen($txt); $i++) {   
  30.     $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;   
  31.     $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];   
  32.     }   
  33.     return $tmp;   
  34. }   
  35.    
  36.   
  37. $txt = "1";   
  38. $key = "testkey";   
  39. $encrypt = passport_encrypt($txt,$key);   
  40. $decrypt = passport_decrypt($encrypt,$key);   
  41.   
  42. echo $encrypt."<br>";   
  43. echo $decrypt."<br>";   
  44.   
  45. ?>   
⑤第五种加密解密算法:discuz中使用的加密解密算法

项目中有时我们需要使用PHP将特定的信息进行加密,也就是通过加密算法生成一个加密字符串,这个加密后的字符串可以通过解密算法进行解密,便于程序对解密后的信息进行处理。最常见的应用在用户登录以及一些API数据交换的场景。最常见的应用在用户登录以及一些API数据交换的场景。加密解密原理一般都是通过一定的加密解密算法,将密钥加入到算法中,最终得到加密解密结果。

[php]  view plain  copy
  1. <?php  
  2. //非常给力的authcode加密函数,Discuz!经典代码(带详解)  
  3. //函数authcode($string, $operation, $key, $expiry)中的$string:字符串,明文或密文;$operation:DECODE表示解密,其它表示加密;$key:密匙;$expiry:密文有效期。  
  4. function authcode($string$operation = 'DECODE'$key = ''$expiry = 0) {     
  5.     // 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙     
  6.     $ckey_length = 4;     
  7.          
  8.     // 密匙     
  9.     $key = md5($key ? $key : $GLOBALS['discuz_auth_key']);     
  10.          
  11.     // 密匙a会参与加解密     
  12.     $keya = md5(substr($key, 0, 16));     
  13.     // 密匙b会用来做数据完整性验证     
  14.     $keyb = md5(substr($key, 16, 16));     
  15.     // 密匙c用于变化生成的密文     
  16.     $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';     
  17.     // 参与运算的密匙     
  18.     $cryptkey = $keya.md5($keya.$keyc);     
  19.     $key_length = strlen($cryptkey);     
  20.     // 明文,前10位用来保存时间戳,解密时验证数据有效性,10到26位用来保存$keyb(密匙b),   
  21.     //解密时会通过这个密匙验证数据完整性     
  22.     // 如果是解码的话,会从第$ckey_length位开始,因为密文前$ckey_length位保存 动态密匙,以保证解密正确     
  23.     $string = $operation == 'DECODE' ? base64_decode(substr($string$ckey_length)) :  sprintf('%010d'$expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;     
  24.     $string_length = strlen($string);     
  25.     $result = '';     
  26.     $box = range(0, 255);     
  27.     $rndkey = array();     
  28.     // 产生密匙簿     
  29.     for($i = 0; $i <= 255; $i++) {     
  30.         $rndkey[$i] = ord($cryptkey[$i % $key_length]);     
  31.     }     
  32.     // 用固定的算法,打乱密匙簿,增加随机性,好像很复杂,实际上对并不会增加密文的强度     
  33.     for($j = $i = 0; $i < 256; $i++) {     
  34.         $j = ($j + $box[$i] + $rndkey[$i]) % 256;     
  35.         $tmp = $box[$i];     
  36.         $box[$i] = $box[$j];     
  37.         $box[$j] = $tmp;     
  38.     }     
  39.     // 核心加解密部分     
  40.     for($a = $j = $i = 0; $i < $string_length$i++) {     
  41.         $a = ($a + 1) % 256;     
  42.         $j = ($j + $box[$a]) % 256;     
  43.         $tmp = $box[$a];     
  44.         $box[$a] = $box[$j];     
  45.         $box[$j] = $tmp;     
  46.         // 从密匙簿得出密匙进行异或,再转成字符     
  47.         $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));     
  48.     }     
  49.     if($operation == 'DECODE') {    
  50.         // 验证数据有效性,请看未加密明文的格式     
  51.         if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) &&  substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {     
  52.             return substr($result, 26);     
  53.         } else {     
  54.             return '';     
  55.         }     
  56.     } else {     
  57.         // 把动态密匙保存在密文里,这也是为什么同样的明文,生产不同密文后能解密的原因     
  58.         // 因为加密后的密文可能是一些特殊字符,复制过程可能会丢失,所以用base64编码     
  59.         return $keyc.str_replace('='''base64_encode($result));     
  60.     }     
  61. }   
  62.   
  63. $str = 'abcdef';   
  64. $key = 'www.helloweba.com';   
  65. echo authcode($str,'ENCODE',$key,0); //加密   
  66. $str = '56f4yER1DI2WTzWMqsfPpS9hwyoJnFP2MpC8SOhRrxO7BOk';   
  67. echo authcode($str,'DECODE',$key,0); //解密   
  68.   
  69. ?>  
⑥第六种加密解密算法:
[php]  view plain  copy
  1. <?php  
  2. //函数encrypt($string,$operation,$key)中$string:需要加密解密的字符串;$operation:判断是加密还是解密,E表示加密,D表示解密;$key:密匙。  
  3. function encrypt($string,$operation,$key=''){   
  4.     $key=md5($key);   
  5.     $key_length=strlen($key);   
  6.       $string=$operation=='D'?base64_decode($string):substr(md5($string.$key),0,8).$string;   
  7.     $string_length=strlen($string);   
  8.     $rndkey=$box=array();   
  9.     $result='';   
  10.     for($i=0;$i<=255;$i++){   
  11.            $rndkey[$i]=ord($key[$i%$key_length]);   
  12.         $box[$i]=$i;   
  13.     }   
  14.     for($j=$i=0;$i<256;$i++){   
  15.         $j=($j+$box[$i]+$rndkey[$i])%256;   
  16.         $tmp=$box[$i];   
  17.         $box[$i]=$box[$j];   
  18.         $box[$j]=$tmp;   
  19.     }   
  20.     for($a=$j=$i=0;$i<$string_length;$i++){   
  21.         $a=($a+1)%256;   
  22.         $j=($j+$box[$a])%256;   
  23.         $tmp=$box[$a];   
  24.         $box[$a]=$box[$j];   
  25.         $box[$j]=$tmp;   
  26.         $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256]));   
  27.     }   
  28.     if($operation=='D'){   
  29.         if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8)){   
  30.             return substr($result,8);   
  31.         }else{   
  32.             return'';   
  33.         }   
  34.     }else{   
  35.         return str_replace('=','',base64_encode($result));   
  36.     }   
  37. }   
  38.   
  39. $str = 'abc';   
  40. $key = 'www.helloweba.com';   
  41. $token = encrypt($str'E'$key);   
  42. echo '加密:'.encrypt($str'E'$key);   
  43. echo '解密:'.encrypt($str'D'$key);  
  44.   
  45. ?>  

猜你喜欢

转载自blog.csdn.net/maguangwithc/article/details/80087943