Home >Backend Development >PHP Tutorial >A PHP encryption and decryption class for numbers_PHP tutorial

A PHP encryption and decryption class for numbers_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:35:28917browse

Copy code The code is as follows:

/**
* Encryption and decryption class
* This algorithm only supports encrypted numbers. It is more suitable for encryption and decryption of id fields in databases, and encryption of URLs displayed based on numbers.
* @author Late Autumn Bamboo
* @version alpha
* @Encryption principle mark length + padding + number replacement
* @Encryption steps:
* Change a-z,A-Z,0- 9 62 characters are scrambled, take the first M (maximum number of digits in a number) digits as the mark length string, take the M+1 to M+10th digits as the digit replacement string, and the rest are fill-in strings
* 1. Calculate the number length n, and take the nth digit of the garbled code as the mark length.
* 2. Calculate the length of the complement. The length of the encrypted string N -1 - n is the length of the complement. Obtain the complement string according to the specified algorithm.
* 3. Replace the numbers according to the number replacement string to get the digital encrypted string.
* Mark length character + padded 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 $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 first digit of the ciphertext mark
$begin = substr($this->codelen,$numslen - 1,1);

//Extension bits of ciphertext
$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}
";

}

?>

Results of running the example:

[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]

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/743929.htmlTechArticleCopy the code code as follows: ?php /** * Encryption and decryption class* This algorithm only supports encrypted numbers. It is more suitable for encryption and decryption of id fields in databases, and encryption of URLs displayed based on numbers. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn