A PHP encryption/Decryption class for numbers _ php instance

Source: Internet
Author: User
This article mainly introduces a PHP encryption and decryption class for numbers, which only supports encrypted numbers. It is applicable to the encryption and decryption of the id field in the database and the encryption of the url according to the number. For more information, see The Code is as follows:

/**
* Encryption and decryption
* This algorithm only supports encrypted numbers. It is applicable to the encryption and decryption of the id field in the database and the encryption of the display url based on numbers.
* @ Author bamboo in late autumn
* @ Version alpha
* @ Encryption principle mark Length + complement + digit replacement
* @ Encryption steps:
* Disrupt a-z, A-Z, 0-9 62 characters, take the first M (the maximum number of digits) as the mark length string, replace the string with digits from the M + 1 to the M + 10 digits, and the remaining digits are the complement strings.
* 1. Calculate the number length n, and take the nth bit of garbled code as the mark length.
* 2. Calculate the length of the complement. The length of the encrypted string is N-1-n. Obtains the complement String Based on the specified algorithm.
* 3. Replace the number with a string based on the number to obtain the encrypted numeric string.
* Mark length character + complement character string + digital encrypted string = encrypted string
* Usage:
* $ Obj = new XDeode (9 );
* $ E_txt = $ obj-> encode (123 );
* Echo $ e_txt .'
';
* Echo $ key-> decode ($ e_txt );
*/

Class XDeode {
Private $ strbase = "external ";
Private $ key, $ length, $ codelen, $ codenums, $ codeext;
Function _ construct ($ length = 9, $ key = 2543.5415412812 ){
$ This-> key = $ key;
$ This-> 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 );
// The length of the number marked first in the ciphertext
$ Begin = substr ($ this-> codelen, $ numslen-1,1 );

// Ciphertext extended bit
$ 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];
}
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;
}
}

/***** Example ****/
$ 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}
";
}
?>


Running example result:

[2, 9950] = [vmizxPPga] = [9950]
[2, 9951] = [vSNSSPPgk] = [9951]
[9952] = [vNQNyPPgV] = [9952]
[9953] = [vyyJJPPgj] = [9953]
[2, 9954] = [vNzQzPPgq] = [9954]
[2, 9955] = [vyNzmPPgg] = [9955]
[9956] = [vXxSNPPge] = [9956]
[9957] = [vXJJJPPgW] = [9957]
[9958] = [vXziQPPgU] = [9958]
[1, 9959] = [viXxSPPgP] = [9959]
[2, 9960] = [vQxmyPPea] = [9960]
[2, 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.