PHP encryption decryption function and DES encryption decryption instance, phpdes encryption decryption Instance _php tutorial

Source: Internet
Author: User
Tags crypt

Encrypted decryption function and DES encryption and decryption instance in PHP, Phpdes encryption and decryption instance


In this paper, the application of cryptographic decryption function and DES encryption and decryption in PHP is described, and it is shared for everyone's reference. Specific as follows:

Examples, examples of PHP encryption and decryption

Cryptographic functions:

Copy the Code Code 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:
Copy CodeThe 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:
Copy CodeThe 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
Copy CodeThe 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:
Copy CodeThe code is as follows: <?php
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:
Copy CodeThe 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:

Copy the Code code 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;
}

I hope this article is helpful to everyone's PHP programming.


PHP Cryptographic decryption function

Base64_encode Plus
Base64_decode Solution

PHP des cryptographic functions

Two functions are as follows:
Cryptographic functions: Encrypt
Function Encrypt ($encrypt, $key = "") {
$iv = Mcrypt_create_iv (mcrypt_get _iv_size (mcrypt_rijndael_256, MCRYPT_MODE_ECB), Mcrypt_rand);
$passcrypt = Mcrypt_encrypt (mcrypt_rijndael_256, $key, $encrypt, MCRYPT_MODE_ECB, $IV);
$encode = Base64_encode ($passcrypt);
return $encode;
}

Decryption function: Decrypt
Function Decrypt ($decrypt, $key = "") {
$decoded = Base64_decode ($decrypt);
$iv = MC Rypt_create_iv (Mcrypt_get_iv_size (mcrypt_rijndael_256, MCRYPT_MODE_ECB), Mcrypt_rand);
$decrypted = Mcrypt_decrypt (mcrypt_rijndael_256, $key, $decoded, MCRYPT_MODE_ECB, $IV);
return $decrypted;
}
 

http://www.bkjia.com/PHPjc/895109.html www.bkjia.com true http://www.bkjia.com/PHPjc/895109.html techarticle PHP encryption and decryption function and DES encryption and decryption instances, phpdes encryption and decryption instances in this paper, the encryption and decryption functions in PHP and DES Encryption and decryption of the application, shared for everyone for reference. ...

  • Related Article

    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.