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

Source: Internet
Author: User
Tags crypt decrypt md5 sha1 encryption

This article describes the encryption and decryption function in PHP and DES encryption and decryption application, share for everyone to reference. Specifically as follows:

Examples, examples of PHP encryption decryption

Cryptographic functions:

Copy Code 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:

Copy Code code as follows:
/*
* 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 functions:
Copy Code 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
Copy Code code as follows:
$str = ' Author: www.jb51.net ';
$key = ' 123456 ';
$encrypt =passport_encrypt ($str, $key);
$decrypt =passport_decrypt ($encrypt, $key);
Echo ' original: ', $str. ' <br>Echo ' redaction: ', $encrypt. ' <br>echo ' translation: ', $decrypt. ' <br>

Des encryption and decryption function specific code is as follows:
Copy Code code as follows:
<?php
Class DES
{
var $key;
var $iv; Offset amount

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, returning 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) {
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 is the test result:
Copy Code code 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 Code code 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;
}

PS: About encryption technology, the site also provides the following encryption tools for your reference to use:

MD5 Online encryption tool: Http://tools.jb51.net/password/CreateMD5Password

Escape Encryption/decryption tool: http://tools.jb51.net/password/escapepwd

Online SHA1 encryption tool: Http://tools.jb51.net/password/sha1encode

short link (short URL) online generation tool: http://tools.jb51.net/password/dwzcreate

short link (short URL) online Restore tool: Http://tools.jb51.net/password/unshorturl

High Strength password generator: Http://tools.jb51.net/password/CreateStrongPassword

I hope this article will help you with your PHP program design.

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.