Encryption and decryption function in PHP and DES encryption and decryption instance

Source: Internet
Author: User
Tags chr crypt decrypt md5 php and strlen

Examples, examples of PHP encryption decryption

Cryptographic functions:

code as follows:/*


* Function: Encrypt string to handle


* Parameter one: Content that needs to be encrypted


* Parameter two: Key


*/


function Passport_encrypt ($str, $key) {//cryptographic functions


Srand (Double) microtime () * 1000000);


$encrypt _key=md5 (rand (0, 32000));


$ctr = 0;


$tmp = ';


for ($i =0; $i <strlen ($STR); $i + +) {


$ctr = $ctr ==strlen ($encrypt _key) 0: $ctr;


$tmp. = $encrypt _key[$ctr]. ($str [$i] ^ $encrypt _key[$ctr + +]);


 }


return Base64_encode (Passport_key ($tmp, $key));


}

The decryption code is as follows:

code as follows:/*


* Function: Decryption of strings


* Parameter one: Need to decrypt the ciphertext


* Parameter two: Key


*/


function Passport_decrypt ($str, $key) {//decryption functions


$str =passport_key (Base64_decode ($STR), $key);


$tmp = ';


for ($i =0; $i <strlen ($STR); $i + +) {


$md 5= $str [$i];


$tmp. = $str [+ + $i] ^ $MD 5;


 }


return $tmp;


}


Auxiliary functions:

code as follows:/*


* Auxiliary function


*/


function Passport_key ($str, $encrypt _key) {


$encrypt _key=md5 ($encrypt _key);


$ctr = 0;


$tmp = ';


for ($i =0; $i <strlen ($STR); $i + +) {


$ctr = $ctr ==strlen ($encrypt _key) 0: $ctr;


$tmp. = $str [$i] ^ $encrypt _key[$ctr + +];


 }


return $tmp;


}


Use the following example

The

code is as follows: $str = ' Author: www.jb51.net ';


$key = ' 123456 ';


$encrypt =passport_encrypt ($str, $key);


$decrypt =passport_decrypt ($encrypt, $key);


Echo ' original: ', $str. ' <br><hr> ";


Echo ' redaction: ', $encrypt. ' <br><hr> ";


echo ' translation: ', $decrypt. ' <br><hr> ";


Des encryption and decryption function specific code is as follows:

code is as follows: <?php


class DES


{


var $key;


var $iv; Offset


 


function DES ($key, $iv = 0) {


//key Length 8 For example: 1234ABCD


$this->key = $key;


if ($iv = = 0) {


$this->iv = $key;


} else {


$this->iv = $iv; Mcrypt_create_iv (Mcrypt_get_block_size (Mcrypt_des, MCRYPT_MODE_CBC), mcrypt_dev_random);


  }


 }


 


function Encrypt ($str) {


//encryption, returns uppercase hexadecimal strings


$size = Mcrypt_get_block_size (Mcrypt_des, MCRYPT_MODE_CBC);


$str = $this->pkcs5pad ($str, $size);


return Strtoupper (Bin2Hex MCRYPT_CBC (mcrypt_des, $this->key, $str, Mcrypt_encrypt, $this->iv));


 }


 


function Decrypt ($str) {


//Decryption


$strBin = $this->hex2bin (strtolower ($STR));


$str = MCRYPT_CBC (mcrypt_des, $this->key, $strBin, Mcrypt_decrypt, $this->iv);


$str = $this->pkcs5unpad ($STR);


return $str;


 }


 


function Hex2bin ($hexData) {


$binData = "";


for ($i = 0; $i < strlen ($hexData); $i + + 2) {


$binData. = Chr (Hexdec (substr ($hexData, $i, 2));


  }


return $binData;


 }


 


function Pkcs5pad ($text, $blocksize) {


$pad = $blocksize-(strlen ($text)% $blocksize);


return $text. Str_repeat (Chr ($pad), $pad);


 }


 


function Pkcs5unpad ($text) {


$pad = Ord ($text {strlen ($text)-1});


if ($pad > strlen ($text))


return false;


if (strspn ($text, Chr ($pad), strlen ($text)-$pad)!= $pad)


return false;


return substr ($text, 0,-1 * $pad);


 }


}


?>


Here is the test result:

The

code is as follows: $str = ' 12345678 ';


$key = ' 1234ABCD ';


$crypt = new DES ($key);


$mstr = $crypt->encrypt ($STR);


$str = $crypt->decrypt ($MSTR);


echo $str. ' <=> '. $mstr;


Example 2

The code is as follows:

code is as follows:/**


* Encrypt parameters that need to be passed in the URL by get way


*/


function Args_encode ($data) {


if (Is_array ($data)) {


$string = Http_build_query ($data);


return Base64_encode ($string);


} else {


return false;


}


}

/**
* Gets the parameters passed by get mode in the URL
*/
function Getargs () {
$string = Base64_decode ($_get[' args '));
Parse_str ($string, $g);
return $g;
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.