標籤:資料 split fss ssm 欄位 bst function replace class
1 <?php 2 /** 3 * 加密解密類,PHP加密解密數字,適用於URL加密 4 * 該演算法僅支援加密數字。比較適用於資料庫中id欄位的加密解密,以及根據數字顯示url的加密。 5 * @version alpha 6 * @加密原則 標記長度 + 補位 + 數字替換 7 * @加密步驟: 8 * 將a-z,A-Z,0-9 62個字元打亂,取前M(數字最大的位元)位作為 標記長度字串,取第M+1 到第M+10位為數字替換字串,剩餘的為補位字串 9 * 1.計算數字長度n,取亂碼的第n位作為標記長度。 10 * 2.計算補位的長度,加密串的長度N -1 - n 為補位的長度。根據指定的演算法得到補位字串。 11 * 3.根據數字替換字串替換數字,得到數字加密字串。 12 * 標記長度字元 + 補位字串 + 數字加密字串 = 加密串 13 * Usage: 14 * $obj = new XDeode(9); 15 * $e_txt = $obj->encode(123); 16 * echo $e_txt.‘<br/>‘; 17 * echo $key->decode($e_txt); 18 */ 19 class XDeode 20 { 21 private $strbase = "Flpvf70CsakVjqgeWUPXQxSyJizmNH6B1u3b8cAEKwTd54nRtZOMDhoG2YLrI"; 22 private $key,$length,$codelen,$codenums,$codeext; 23 function __construct($length = 9,$key = 2543.5415412812){ 24 $this->key = $key; 25 $this->length = $length; 26 $this->codelen = substr($this->strbase,0,$this->length); 27 $this->codenums = substr($this->strbase,$this->length,10); 28 $this->codeext = substr($this->strbase,$this->length + 10); 29 } 30 31 32 function encode($nums){ 33 $rtn = ""; 34 $numslen = strlen($nums); 35 //密文第一位標記數位長度 36 $begin = substr($this->codelen,$numslen - 1,1); 37 38 //密文的擴充位 39 $extlen = $this->length - $numslen - 1; 40 $temp = str_replace(‘.‘, ‘‘, $nums / $this->key); 41 $temp = substr($temp,-$extlen); 42 43 $arrextTemp = str_split($this->codeext); 44 $arrext = str_split($temp); 45 foreach ($arrext as $v) { 46 $rtn .= $arrextTemp[$v]; 47 } 48 49 $arrnumsTemp = str_split($this->codenums); 50 $arrnums = str_split($nums); 51 foreach ($arrnums as $v) { 52 $rtn .= $arrnumsTemp[$v]; 53 } 54 return $begin.$rtn; 55 } 56 57 58 function decode($code){ 59 60 $begin = substr($code,0,1); 61 $rtn = ‘‘; 62 $len = strpos($this->codelen,$begin); 63 if($len!== false){ 64 $len++; 65 $arrnums = str_split(substr($code,-$len)); 66 foreach ($arrnums as $v) { 67 $rtn .= strpos($this->codenums,$v); 68 } 69 } 70 71 return $rtn; 72 } 73 } 74 75 $obj = new XDeode(10); 76 echo $obj->encode(1)."<br/>"; 77 echo $obj->encode(2)."<br/>"; 78 echo $obj->encode(3)."<br/>"; 79 echo $obj->encode(4)."<br/>"; 80 echo $obj->encode(5)."<br/>"; 81 echo $obj->encode(6)."<br/>"; 82 echo $obj->encode(7)."<br/>"; 83 echo $obj->encode(8)."<br/>"; 84 echo $obj->encode(9)."<br/>"; 85 echo $obj->encode(10)."<br/>"; 86 echo $obj->encode(45762454)."<br/>"; 87 echo $obj->encode(1023213123)."<br/>"; 88 echo $obj->encode(134320)."<br/>"; 89 echo ‘<hr/>‘; 90 91 echo $obj->decode(‘FzxxmyQzxV‘)."<br/>"; 92 echo $obj->decode(‘FSSyJzxSSj‘)."<br/>"; 93 echo $obj->decode(‘FmNyixHxNq‘)."<br/>"; 94 echo $obj->decode(‘FQJJzHSSJg‘)."<br/>"; 95 echo $obj->decode(‘FzyQiNziye‘)."<br/>"; 96 echo $obj->decode(‘FizmQyNyzW‘)."<br/>"; 97 echo $obj->decode(‘FNSNSxxJyU‘)."<br/>"; 98 echo $obj->decode(‘FQNHyNJJHP‘)."<br/>"; 99 echo $obj->decode(‘FyiQiimiiX‘)."<br/>";100 echo $obj->decode(‘lxxmyQzxVk‘)."<br/>";101 echo $obj->decode(‘CSgeUWjgeg‘)."<br/>";102 echo $obj->decode(‘aQSSmNHxxzzJJNVkjqjVqVjq‘)."<br/>";103 echo $obj->decode(‘7ziiVqgqjk‘)."<br/>";104 105 //FzxxmyQzxV106 //FSSyJzxSSj107 //FmNyixHxNq108 //FQJJzHSSJg109 //FzyQiNziye110 //FizmQyNyzW111 //FNSNSxxJyU112 //FQNHyNJJHP113 //FyiQiimiiX114 //lxxmyQzxVk115 //CSgeUWjgeg116 //aQSSmNHxxzzJJNVkjqjVqVjq117 //7ziiVqgqjk118 //1119 //2120 //3121 //4122 //5123 //6124 //7125 //8126 //9127 //10128 //45762454129 //1023213123130 //134320
PHP加密解密數字