PHP Cryptographic decryption function and DES encryption decryption instance _php tutorial

Source: Internet
Author: User
Tags crypt

Encrypted decryption function in PHP and DES encryption and decryption instance


Examples, examples of PHP encryption and decryption

Cryptographic functions:

The code is as follows:

/*
* Function: Encrypt string processing
* Parameter one: Content that needs to be encrypted
* Parameter two: key
*/
function Passport_encrypt ($STR, $key) {//encryption function
Srand (Double) microtime () * 1000000);
$encrypt _key=md5 (rand (0, 32000));
$ctr = 0;
$tmp = ";
for ($i =0; $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:

The code is as follows:

/*
* Function: Decryption of strings
* Parameter one: ciphertext to be decrypted
* Parameter two: key
*/
function Passport_decrypt ($STR, $key) {//decryption functions
$str =passport_key (Base64_decode ($STR), $key);
$tmp = ";
for ($i =0; $i <>
$MD 5= $str [$i];
$tmp. = $str [+ + $i] ^ $MD 5;
}
return $tmp;
}


Auxiliary functions:

The code is as follows:

/*
* Auxiliary functions
*/
function Passport_key ($str, $encrypt _key) {
$encrypt _key=md5 ($encrypt _key);
$ctr = 0;
$tmp = ";
for ($i =0; $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. '
";
Echo ' ciphertext: ', $encrypt. "
";
echo ' translation: ', $decrypt. '
";


The specific code for DES cryptographic decryption functions is as follows:

The code is as follows:

Class DES
{
var $key;
var $iv; Offset amount

function DES ($key, $iv = 0) {
Key Length 8 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) {
Encrypt, return uppercase hexadecimal string
$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) {
Decrypt
$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 are the test results:

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:

The code is as follows:

/**
* Encrypt the parameters that need to be passed in the URL by the Get method
*/
function Args_encode ($data) {
if (Is_array ($data)) {
$string = Http_build_query ($data);
Return Base64_encode ($string);
} else {
return false;
}
}

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

http://www.bkjia.com/PHPjc/896780.html www.bkjia.com true http://www.bkjia.com/PHPjc/896780.html techarticle PHP Encryption decryption function and DES encryption and decryption examples, PHP encryption decryption example cryptographic function: Code as follows:/* Function: Encrypt the string * parameter one: Need to encrypt ...

  • 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.