php可逆的加密解密算法实现

字体大小: 中小 标准 ->行高大小: 标准
  1. /********************************************************************* 
  2.     函数名称:encrypt 
  3.     函数作用:加密解密字符串 
  4.     使用方法: 
  5.     加密     :encrypt('str','E','nowamagic'); 
  6.     解密     :encrypt('被加密过的字符串','D','nowamagic'); 
  7.     参数说明: 
  8.     $string   :需要加密解密的字符串 
  9.     $operation:判断是加密还是解密:E:加密   D:解密 
  10.     $key      :加密的钥匙(密匙); 
  11.     *********************************************************************/  
  12.     function encrypt($string,$operation,$key='')  
  13.     {  
  14.         $key=md5($key);  
  15.         $key_length=strlen($key);  
  16.         $string=$operation=='D'?base64_decode($string):substr(md5($string.$key),0,8).$string;  
  17.         $string_length=strlen($string);  
  18.         $rndkey=$box=array();  
  19.         $result='';  
  20.         for($i=0;$i<=255;$i++)  
  21.         {  
  22.             $rndkey[$i]=ord($key[$i%$key_length]);  
  23.             $box[$i]=$i;  
  24.         }  
  25.         for($j=$i=0;$i<256;$i++)  
  26.         {  
  27.             $j=($j+$box[$i]+$rndkey[$i])%256;  
  28.             $tmp=$box[$i];  
  29.             $box[$i]=$box[$j];  
  30.             $box[$j]=$tmp;  
  31.         }  
  32.         for($a=$j=$i=0;$i<$string_length;$i++)  
  33.         {  
  34.             $a=($a+1)%256;  
  35.             $j=($j+$box[$a])%256;  
  36.             $tmp=$box[$a];  
  37.             $box[$a]=$box[$j];  
  38.             $box[$j]=$tmp;  
  39.             $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256]));  
  40.         }  
  41.         if($operation=='D')  
  42.         {  
  43.             if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8))  
  44.             {  
  45.                 return substr($result,8);  
  46.             }  
  47.             else  
  48.             {  
  49.                 return'';  
  50.             }  
  51.         }  
  52.         else  
  53.         {  
  54.             $vals = str_replace('=','',base64_encode($result));  
  55.             $vals = urlencode($vals); // 对url参数进行编码  
  56.             return $vals;  
  57.         }  
  58.     }  

使用方法如下:

 

  1. $id = 132;  
  2.        
  3. $token = encrypt($id'E''anv12');  
  4.        
  5. echo '加密:'.encrypt($id'E''anv12');  
  6. echo '<br />';  
  7.        
  8. echo '解密:'.encrypt($token'D''anv12');  

还有一种实现方法,如下:

 

 

  1. <?  
  2.    
  3. class encryptCalss  
  4. {  
  5. var $key=12;  
  6. function encode($txt){  
  7. for($i=0;$i<strlen($txt);$i++){  
  8. $txt[$i]=chr(ord($txt[$i])+$this->key);  
  9. }  
  10. return $txt=urlencode(base64_encode(urlencode($txt)));  
  11. }  
  12. function decode($txt){  
  13. $txt=urldecode(base64_decode($txt));  
  14. for($i=0;$i<strlen($txt);$i++){  
  15. $txt[$i]=chr(ord($txt[$i])-$this->key);  
  16. }  
  17. return $txt;  
  18. }  
  19. }  
  20.    
  21. ?>  

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/69171.html