通常我们都会选择MD5的方法加密,但这种加密是不可逆的。往往我们开发程序时希望加密后可以再解密,但解密的方法是唯一的。于是我们预设一个唯一的字符映射表,通过这个映射把所有的字符替换调,以达到加密的效果。
代码段如下:
//获得随机唯一数组,页面打印后,替换md_str里的$hx_arr数组 $hx_arr = array( 'a'=>'s', 'b'=>'s', 'c'=>'s', 'd'=>'s', 'e'=>'s', 'f'=>'s', 'g'=>'s', 'h'=>'s', 'i'=>'s', 'j'=>'s', 'k'=>'s', 'l'=>'s', 'm'=>'s', 'n'=>'s', 'o'=>'s', 'p'=>'s', 'q'=>'s', 'r'=>'s', 's'=>'s', 't'=>'s', 'u'=>'s', 'v'=>'s', 'w'=>'s', 'x'=>'s', 'y'=>'s', 'z'=>'s', 'A'=>'s', 'B'=>'s', 'C'=>'s', 'D'=>'s', 'E'=>'s', 'F'=>'s', 'G'=>'s', 'H'=>'s', 'I'=>'s', 'J'=>'s', 'K'=>'s', 'L'=>'s', 'M'=>'s', 'N'=>'s', 'O'=>'s', 'P'=>'s', 'Q'=>'s', 'R'=>'s', 'S'=>'s', 'T'=>'s', 'U'=>'s', 'V'=>'s', 'W'=>'s', 'X'=>'s', 'Y'=>'s', 'Z'=>'s', '0'=>'s', '1'=>'s', '2'=>'s', '3'=>'s', '4'=>'s', '5'=>'s', '6'=>'s', '7'=>'s', '8'=>'s', '9'=>'s' ); $t = array_keys($hx_arr); $tt = $t; foreach($tt as $k=>$v){ $r = array_rand($t); $hx_arr[$v] = $t[$r]; unset($t[$r]); } print_r($hx_arr); function md_str($temp_cookie,$md=0){ $hx_arr=array( 'a' => 'R', 'b' => '5', 'c' => 'r', 'd' => 'P', 'e' => 'g', 'f' => '6', 'g' => '8', 'h' => 'Z', 'i' => 'b', 'j' => 'l', 'k' => 'z', 'l' => 'x', 'm' => 'D', 'n' => '0', 'o' => '2', 'p' => '3', 'q' => 'j', 'r' => 'C', 's' => 'i', 't' => '9', 'u' => 'A', 'v' => 'N', 'w' => 'Y', 'x' => 'I', 'y' => 'S', 'z' => 'f', 'A' => 't', 'B' => 'J', 'C' => 'E', 'D' => 'e', 'E' => 'n', 'F' => 'y', 'G' => 'q', 'H' => 'B', 'I' => 'u', 'J' => 'K', 'K' => 'H', 'L' => 'o', 'M' => 'U', 'N' => 'L', 'O' => 'c', 'P' => '4', 'Q' => 'W', 'R' => 'k', 'S' => '7', 'T' => 's', 'U' => 'Q', 'V' => 'm', 'W' => 'p', 'X' => 'd', 'Y' => 'w', 'Z' => 'X', '0' => 'F', '1' => 'h', '2' => 'v', '3' => 'T', '4' => 'G', '5' => 'O', '6' => '1', '7' => 'V', '8' => 'a', '9' => 'M', '='=>'@' ); $t_arr_v=''; if($md== 0){ $t_cookie=base64_encode($temp_cookie); $t_cookie_len=strlen($t_cookie); for($i=0;$i<$t_cookie_len;$i++){ $t_arr_k=substr($t_cookie,$i,1); $hx_arr[$t_arr_k] == '' && $hx_arr[$t_arr_k] = $t_arr_k; $t_arr_v.=$hx_arr[$t_arr_k]; } }elseif($md== 1){ $t_cookie_len=strlen($temp_cookie); for($i=0;$i<$t_cookie_len;$i++){ $t_arr_k=substr($temp_cookie,$i,1); $s_k = array_search($t_arr_k,$hx_arr); $s_k = (string)$s_k; $s_k == '' && $s_k = $t_arr_k; $t_arr_v .= $s_k; } $t_arr_v=base64_decode($t_arr_v); }else{ exit('error md cookie'); } return$t_arr_v; } //测试部分 $a = '测试验证数据ABC789'; echo $a; $b = md_str($a); echo $b; $c = md_str($b,1); echo $c;