Encryption and decryption functions in PHP and DES encryption decryption

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

Examples, examples of PHP encryption decryption

The code is as follows Copy Code

Cryptographic functions


/*
* 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:

/*
* 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:

/*
* 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

$str = ' Author: www.111cn.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

The code is as follows Copy Code

<?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:

$str = ' 12345678 ';

$key = ' 1234ABCD ';
$crypt = new DES ($key);
$mstr = $crypt->encrypt ($STR);
$str = $crypt->decrypt ($MSTR);

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

Example 2

  code is as follows copy code


/**
* 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 in the URL
*/
Function Getargs () {
$string = Base64_decode ($_get[' args ')); br> parse_str ($string, $g);
return $g;
}

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.