A PHP encryption and decryption class for digital

Source: Internet
Author: User
Tags foreach numeric

  This article mainly introduces a PHP encryption and decryption class for digital, which only supports encrypted digits. Compare the encryption decryption that applies to the ID field in the database, and the encryption based on the number display URL, the friend you need can refer to the following

The code is as follows: <?php/**  * Encryption decryption class  * The algorithm only supports encrypted digits. Compare the encryption decryption that applies to the ID field in the database and the encryption that displays the URL based on the number.  * @author late autumn bamboo  * @version Alpha  * @ Encryption principle Mark length + complement + digit replacement  * @ Encryption step:  * will be a-z,a-z,0-9 62 characters upset, take before M (the largest number of digits) bit as a token length string, takes the m+1 to the m+10 digit replaces the string, the remaining is the complement string  * 1. Calculates the number of length n, and takes the nth bit of garbled characters as the mark length.  * 2. Calculates the length of the complement, the length of the cipher string N-1-n is the length of the complement. Gets the complement string according to the specified algorithm.  * 3. Replaces a number with a numeric replacement string to obtain a numeric encrypted string.  * tag length character + complement string + numeric encryption string = Cryptographic strings  * Usage:  *   $obj = new Xdeode (9);  *   $e _txt = $obj->encode (123);  *   echo $e _txt. ' <br/> ';  *   echo $key->decode ($e _txt);  */  class xdeode{  Private $strbase = "Flpvf70csakvjqgewupxqxsyjizmnh6b1u3b8caekwtd54nrtzomdhog2ylri ";   Private $key, $length, $codelen, $codenums, $codeext;   function __construct ($length = 9, $key = 2543.5415412812) {    $this->key = $key;     $this-& Gt;length = $length;     $this->codelen = substr ($this->strbase,0, $this->length);     $this->codenums = substr ($this->strbase, $this->length,10);     $this->codeext = substr ($this->strbase, $this->length + 10);  }       function encode ($nums) {    $RTN = "";     $numslen = strlen ($nums);    //redaction The length of the first mark number     $begin = substr ($this->codelen, $numslen -1,1);      //ciphertext extension     $extlen = $this->length-$numslen-1;     $temp = Str_replace ('. ', ', $nums/$this->key);     $temp = substr ($temp,-$extlen);       $arrextTemp = Str_split ($this->codeext);     $arrext = Str_split ($temp);     foreach ($arrext as $v) {      $rtn. = $arrextTemp [$v];    }       $arrnumsTemp = Str_split ($this->codenums);     $arrnums = Str_split ($nums);     foreach ($arrnums as $v) {      $rtn. = $arrnumsTemp [$v]; &nBsp       return $begin. $rtn;  }       function decode ($code) {      $begin = substr ($code, 0,1);     $RTN = '';     $len = Strpos ($this->codelen, $begin);     if ($len!== false) {      $len + +:       $arrnums = Str_split (substr ($code,-$len ));       foreach ($arrnums as $v) {        $RTN. = Strpos ($this->codenums, $v);      }    }       return $RTN;  }  /**** sample  ****/$begin = 9950; $end = $begin + 50; $obj = new Xdeode (9); for ($i = $begin; $i < $end; $i + +) {  $en = $obj->encode ($i);   $de = $obj->decode ($en);   Echoln ("[{$i }]=[{$en}]=[{$de}] "); }   function Echoln ($str) {  echo ' {$str}<br/> ';}?>   Run sample results:   [9950]=[vmizxppga]=[9950] [ 9951]=[VSNSSPPGK]=[9951] [9952]=[vnqnyppgv]=[9952] [9953]=[vyyjjppgj]=[9953] [9954]=[VNZQZPPGQ]=[9954] [9955]=[vynzmppgg]=[9955] [9956]=[vxxsnppge]=[9956] [9957]=[vxjjjppgw]=[9957] [9958]=[vXziQPPgU]=[9958] [ 9959]=[VIXXSPPGP]=[9959] [9960]=[vqxmyppea]=[9960] [9961]=[vijyjppek]=[9961]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.