PHP加密解密数字

2021-07-17 17:05

阅读:4860

标签:数据   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.‘
‘;
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)."
"; 77 echo $obj->encode(2)."
"; 78 echo $obj->encode(3)."
"; 79 echo $obj->encode(4)."
"; 80 echo $obj->encode(5)."
"; 81 echo $obj->encode(6)."
"; 82 echo $obj->encode(7)."
"; 83 echo $obj->encode(8)."
"; 84 echo $obj->encode(9)."
"; 85 echo $obj->encode(10)."
"; 86 echo $obj->encode(45762454)."
"; 87 echo $obj->encode(1023213123)."
"; 88 echo $obj->encode(134320)."
"; 89 echo
; 90 91 echo $obj->decode(‘FzxxmyQzxV‘)."
"; 92 echo $obj->decode(‘FSSyJzxSSj‘)."
"; 93 echo $obj->decode(‘FmNyixHxNq‘)."
"; 94 echo $obj->decode(‘FQJJzHSSJg‘)."
"; 95 echo $obj->decode(‘FzyQiNziye‘)."
"; 96 echo $obj->decode(‘FizmQyNyzW‘)."
"; 97 echo $obj->decode(‘FNSNSxxJyU‘)."
"; 98 echo $obj->decode(‘FQNHyNJJHP‘)."
"; 99 echo $obj->decode(‘FyiQiimiiX‘)."
"; 100 echo $obj->decode(‘lxxmyQzxVk‘)."
"; 101 echo $obj->decode(‘CSgeUWjgeg‘)."
"; 102 echo $obj->decode(‘aQSSmNHxxzzJJNVkjqjVqVjq‘)."
"; 103 echo $obj->decode(‘7ziiVqgqjk‘)."
"; 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加密解密数字

标签:数据   split   fss   ssm   字段   bst   function   replace   class   

原文地址:http://www.cnblogs.com/719907411hl/p/7066780.html


评论


亲,登录后才可以留言!