A set of PHP encryption and decryption function sharing

Source: Internet
Author: User

The code is as follows:




<?php


/**


* 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));


}

/**
* Function: Decrypt the string for processing
* 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 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;
}
$str = ' Author: WWW.JB51.NET;
$key = ' jb51net ';
$encrypt =passport_encrypt ($str, $key);
$decrypt =passport_decrypt ($encrypt, $key);

Echo ' original: ', $str. ' <br>Echo ' redaction: ', $encrypt. ' <br>echo ' translation: ', $decrypt. ' <br>?>

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.