三个功能强大的php加密解密函数
1 //加密函数 2 function lock_url($txt,$key='www.fyunw.com') 3 { 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.fyunw.com') 21 { 22 $txt = urldecode($txt); 23 $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+"; 24 $ch = $txt[0]; 25 $nh = strpos($chars,$ch); 26 $mdKey = md5($key.$ch); 27 $mdKey = substr($mdKey,$nh%8, $nh%8+7); 28 $txt = substr($txt,1); 29 $tmp = ''; 30 $i=0;$j=0; $k = 0; 31 for ($i=0; $i<strlen($txt); $i++) { 32 $k = $k == strlen($mdKey) ? 0 : $k; 33 $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]); 34 while ($j<0) $j+=64; 35 $tmp .= $chars[$j]; 36 } 37 return base64_decode($tmp); 38 } 39 40 41 42 <?php 43 function passport_encrypt($txt, $key = 'www.fyunw.com') 44 { 45 srand((double)microtime() * 1000000); 46 $encrypt_key = md5(rand(0, 32000)); 47 $ctr = 0; 48 $tmp = ''; 49 for($i = 0;$i < strlen($txt); $i++) { 50 $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr; 51 $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]); 52 } 53 return urlencode(base64_encode(passport_key($tmp, $key))); 54 } 55 56 function passport_decrypt($txt, $key = 'www.fyunw.com') 57 { 58 $txt = passport_key(base64_decode(urldecode($txt)), $key); 59 $tmp = ''; 60 for($i = 0;$i < strlen($txt); $i++) { 61 $md5 = $txt[$i]; 62 $tmp .= $txt[++$i] ^ $md5; 63 } 64 return $tmp; 65 } 66 67 function passport_key($txt, $encrypt_key) 68 { 69 $encrypt_key = md5($encrypt_key); 70 $ctr = 0; 71 $tmp = ''; 72 for($i = 0; $i < strlen($txt); $i++) { 73 $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr; 74 $tmp .= $txt[$i] ^ $encrypt_key[$ctr++]; 75 } 76 return $tmp; 77 } 78 ?> 79 80 <?php 81 82 $txt = "1"; 83 $key = "testkey"; 84 $encrypt = passport_encrypt($txt,$key); 85 $decrypt = passport_decrypt($encrypt,$key); 86 87 echo $encrypt."<br>"; 88 echo $decrypt."<br>"; 89 ?> 90 91 //改进第一改加密之后的算法 92 //加密函数 93 function lock_url($txt,$key='www.fyunw.com') 94 { 95 $txt = $txt.$key; 96 $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+"; 97 $nh = rand(0,64); 98 $ch = $chars[$nh]; 99 $mdKey = md5($key.$ch); 100 $mdKey = substr($mdKey,$nh%8, $nh%8+7); 101 $txt = base64_encode($txt); 102 $tmp = ''; 103 $i=0;$j=0;$k = 0; 104 for ($i=0; $i<strlen($txt); $i++) { 105 $k = $k == strlen($mdKey) ? 0 : $k; 106 $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64; 107 $tmp .= $chars[$j]; 108 } 109 return urlencode(base64_encode($ch.$tmp)); 110 } 111 //解密函数 112 function unlock_url($txt,$key='www.fyunw.com') 113 { 114 $txt = base64_decode(urldecode($txt)); 115 $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+"; 116 $ch = $txt[0]; 117 $nh = strpos($chars,$ch); 118 $mdKey = md5($key.$ch); 119 $mdKey = substr($mdKey,$nh%8, $nh%8+7); 120 $txt = substr($txt,1); 121 $tmp = ''; 122 $i=0;$j=0; $k = 0; 123 for ($i=0; $i<strlen($txt); $i++) { 124 $k = $k == strlen($mdKey) ? 0 : $k; 125 $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]); 126 while ($j<0) $j+=64; 127 $tmp .= $chars[$j]; 128 } 129 return trim(base64_decode($tmp),$key); 130 }
作者:zqifa