1. 程式人生 > >PHP加密解密數字

PHP加密解密數字

sub 得到 ssm clas fyi 3.5 url bstr reac

  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 //FzxxmyQzxV 106 //FSSyJzxSSj 107 //FmNyixHxNq 108 //FQJJzHSSJg 109 //FzyQiNziye 110 //FizmQyNyzW 111 //FNSNSxxJyU 112 //FQNHyNJJHP 113 //FyiQiimiiX 114 //lxxmyQzxVk 115 //CSgeUWjgeg 116 //aQSSmNHxxzzJJNVkjqjVqVjq 117 //7ziiVqgqjk 118 //1 119 //2 120 //3 121 //4 122 //5 123 //6 124 //7 125 //8 126 //9 127 //10 128 //45762454 129 //1023213123 130 //134320

PHP加密解密數字