MCrypt enable encryption and decryption process detailed parsing _php tips

Source: Internet
Author: User
Tags decrypt mcrypt

The MCrypt extension library can be encrypted and decrypted, which can either encrypt plaintext or restore ciphertext.

1.PHP Encryption Extension Library MCrypt installation
The Mrcypt installation was not installed during the standard PHP installation, but the PHP home directory contains Libmcrypt.dll and Libmhash.dll files (Libmhash.dll is the Mhash extension library, which can be installed together). First, copy the two files to the system directory Windows\System32, and then press ctrl+f shortcut key in the php.ini file to jump out of the lookup box and find it; Extension=php-mcrypt.dll and; Extension=php_mhash.dll the two statements, then remove the preceding ";", and finally, save and restart the Apache server to take effect.

The algorithm and encryption mode of 2.PHP encryption extension Library MCrypt
The MCrypt library supports more than 20 encryption algorithms and 8 encryption modes, which can be displayed by function mcrypt_list_algorithms () and mcrypt_list_modes (), and the results are as follows:

MCrypt supported algorithms are:cast-128 gost rijndael-128 twofish arcfour cast-256 loki97, rijndael-192 Saferplus wake Blowfish-compat des rijndael-256 Serpent xtea blowfish Enigma RC2 TripleDES

the encryption modes supported by MCrypt are:CBC CFB Ctr ECB NCFB NOFB OFB stream

These algorithms and patterns in the application to be expressed in Changshilai, write with the prefix Mcrypt_ and Mcrypt_ to express, as the following mcrypt application examples:
DES algorithm is expressed as mcrypt_des;
ECB model is expressed as MCRYPT_MODE_ECB;

3.PHP Encryption Extension Library MCrypt application
Take a look at an example to understand the MCrypt workflow, and then look at some of the functions used by the process:

Copy Code code as follows:

$str = "I am Liyun";
$key = "123qwe.019860905061x";
$cipher = mcrypt_rijndael_128;
$iv = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $mode), Mcrypt_rand);

echo "Original:" $str. " <br> ";
$str _encrypt = Mcrypt_encrypt ($cipher, $key, $str, $mode, $IV);
echo "is encrypted with the following:". $str _encrypt. " <br> ";

$str _decrypt = Mcrypt_decrypt ($cipher, $key, $str _encrypt, $mode, $IV);

echo "Decrypted content:". $str _decrypt. " <br> ";

Run Result:

The original: I am Liyun
The content after encrypting is: b@ 鴹 = (I argue 蝣 z%
Decrypted content: This is Liyun.

<1> As can be seen from the example, the use of the PHP encryption extension Library MCrypt Data encryption and decryption, first created an initialization vector, referred to as IV. by the $IV = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $modes), Mcrypt_rand); it takes two parameters to create the initialization vector: size Specifies the size of IV; source is IV , where the value Mcrypt_rand is the system random number.

The <2> function mcrypt_get_iv_size ($cipher, $modes) returns the initialization vector size, and the cipher and mode respectively refer to the algorithm and the encryption mode.

<3> Cryptographic function $str_encrypt = Mcrypt_encrypt ($cipher, $key, $str, $modes, $IV); The 5 parameters of the function are as follows: cipher--encryption algorithm, key--key, Data (str)--Need encrypted data, mode--algorithm mode, iv--initialization vector

<4> decryption function Mcrypt_decrypt ($cipher, $key, $str _encrypt, $modes, $IV); The function is almost the same as the parameters of the cryptographic function, and the only difference is data, which means that it is $str_encrypt, rather than the original data, to be decrypted. $str

The writing in the manual:

Copy Code code as follows:

Specifies the size of the initialization vector IV:
$iv _size = mcrypt_get_iv_size (mcrypt_rijndael_256, MCRYPT_MODE_ECB);
To create an initialization vector:
$iv = Mcrypt_create_iv ($iv _size, Mcrypt_rand);
Encrypted password:
$key = "123qwe.019860905061x";
Original content (unencrypted):
$text = "My name is Adam li!";
Echo $text. "<br>\n";
Content after encryption:
$crypttext = Mcrypt_encrypt (mcrypt_rijndael_256, $key, $text, MCRYPT_MODE_ECB, $IV);
Echo $crypttext. "\n<br>";
To decrypt a content that has already been encrypted:
$str _decrypt = Mcrypt_decrypt (mcrypt_rijndael_256, $key, $crypttext, MCRYPT_MODE_ECB, $IV);
echo $str _decrypt;

Here is an example of a plus/decryption request:
Copy Code code as follows:

$request _params = Array (
' Controller ' => ' Todo ',
' Action ' => ' read ',
' username ' => ' bl ',
' Userpass ' => ' A1 '

$private _key = "28e336ac6c9423d946ba02d19c6a2632";

Encrypt request
$enc _request = Base64_encode (Mcrypt_encrypt (mcrypt_rijndael_256, $private _key, Json_encode ($request _params), MCrypt _MODE_ECB));
echo "CRYPT:". $enc _request. " <br/> ";

Decrypt request
$params = Json_decode (Trim (mcrypt_decrypt mcrypt_rijndael_256, $private _key, Base64_decode ($enc _request), Mcrypt_ MODE_ECB)), true);
echo "Encrypt:<br/>";

Print result
Var_dump ($params);

Note:The parameter cipher, key, and mode in the encryption and decryption function must correspond one by one, otherwise the data cannot be restored.

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