PHP encryption and decryption function and DES encryption and decryption instance _ PHP Tutorial

Source: Internet
Author: User
Tags crypt
PHP and DES encryption and decryption instances. Example of the encryption and decryption function in PHP and the DES encryption and decryption instance. example of the php encryption and decryption function: the code is as follows: ** function: encrypt strings * parameter 1: encryption and decryption functions in PHP and DES encryption and decryption instances

Example: example of php encryption and decryption

Encryption function:

The code is as follows:

/*
* Function: encrypt strings.
* Parameter 1: content to be encrypted
* Parameter 2: 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 = 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: decrypts strings.
* Parameter 1: ciphertext to be decrypted
* Parameter 2: Key
*/
Function passport_decrypt ($ str, $ key) {// decrypt the function
$ Str = passport_key (base64_decode ($ str), $ key );
$ Tmp = '';
For ($ I = 0; $ I $ Md5 = $ str [$ I];
$ Tmp. = $ str [++ $ I] ^ $ md5;
}
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 = strlen ($ encrypt_key )? 0: $ ctr;
$ Tmp. = $ str [$ I] ^ $ encrypt_key [$ ctr ++];
}
Return $ tmp;
}


Use

The code is as follows:

$ Str = 'Author: www.jb51.net ';
$ Key = '000000 ';
$ Encrypt = passport_encrypt ($ str, $ key );
$ Decrypt = passport_decrypt ($ encrypt, $ key );
Echo 'original: ', $ str ."
";
Echo 'ciphertext: ', $ encrypt ."
";
Echo 'Translation: ', $ decrypt ."
";


The specific code of the DES encryption and decryption function is as follows:

The code is as follows:

Class DES
{
Var $ key;
Var $ iv; // Offset

Function DES ($ key, $ iv = 0 ){
// The key length is 8, for example, 1234 abcd.
$ 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 ){
// Encrypted, returns an 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 );
}
}
?>


The test results are as follows:

The code is as follows:

$ Str = '000000 ';
$ 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:

/**
* Parameters that need to be passed in the url through the get method for encryption
*/
Function args_encode ($ data ){
If (is_array ($ data )){
$ String = http_build_query ($ data );
Return base64_encode ($ string );
} Else {
Return false;
}
}

/**
* Get the parameters passed by the get method in the url
*/
Function getargs (){
$ String = base64_decode ($ _ GET ['args ']);
Parse_str ($ string, $ g );
Return $ g;
}

Example of encryption: example of php encryption and decryption encryption function: the code is as follows:/** function: encryption of strings * parameter 1: encryption required...

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.