MCrypt enable encryption and decryption process detailed parsing _php tutorial

Source: Internet
Author: User
Tags mcrypt
MCrypt Extension Library can implement encryption and decryption function, that is, the plaintext can be encrypted, or ciphertext can be restored.

1.PHP Encryption Extension Library MCrypt installation
The standard PHP installation process did not install MRCYPT, but the main directory of PHP contains the Libmcrypt.dll and Libmhash.dll files (Libmhash.dll is the Mhash extension library, which can be installed together). First, copy these 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; Extension=php-mcrypt.dll and; Extension=php_mhash.dll these two statements, then remove the previous ";", and finally, save and restart the Apache server to take effect.

2.PHP Encryption extension Library MCrypt algorithm and encryption mode
The MCrypt library supports more than 20 encryption algorithms and 8 encryption modes, which can be displayed through functions mcrypt_list_algorithms () and Mcrypt_list_modes (), with the following results:

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

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

These algorithms and patterns in the application to be expressed as represented, when written with the prefix Mcrypt_ and mcrypt_ to represent, such as the following MCrypt application example:
DES algorithm is expressed as mcrypt_des;
The ECB model is expressed as MCRYPT_MODE_ECB;

3.PHP Encryption Extension Library MCrypt application
Take a look at an example, understand the workflow of MCrypt, and then look at the functions used by some of the processes:
Copy the Code code as follows:
$str = "I am Levin";
$key = "123qwe.019860905061x";
$cipher = mcrypt_rijndael_128;
$mode = MCRYPT_MODE_ECB;
$iv = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $mode), Mcrypt_rand);

echo "Original:" $str. "
";
$str _encrypt = Mcrypt_encrypt ($cipher, $key, $str, $mode, $IV);
echo "Encrypted content is:". $str _encrypt. "
";

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

echo "Decrypted content:" $str _decrypt. "
";

Operation Result:

The original: I am Levin
The encrypted content is: b@ 鴹 = (I argue 蝣 z%
Decrypted content: I'm Levin.

<1> As can be seen in the example, using PHP encryption extension library MCrypt to encrypt and decrypt data, first create an initialization vector, referred to as IV. by $iv = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $modes), mcrypt_rand); the creation of an initialization vector requires two parameters: size Specifies the magnitude of IV; Source is the origin of 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 parameter cipher and mode respectively refer to the algorithm and encryption mode.

<3> Cryptographic functions $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)--required to encrypt, 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, except that the data is the only one that is $str_encrypt, rather than the raw data $str, which is to be decrypted.

//manual:
copy code The code is as follows:
//Specify the size of the initialization vector IV:
$iv _size = mcrypt_get_iv_size (mcrypt_rijndael_256, MCRYPT_MODE_ECB);
//Create initialization vector:
$iv = Mcrypt_create_iv ($iv _size, Mcrypt_rand);
//Encryption Password:
$key = "123qwe.019860905061x";
//original content (unencrypted):
$text = "My name is Adam li!";
Echo $text. "
\ n";
///encrypted content:
$crypttext = Mcrypt_encrypt (mcrypt_rijndael_256, $key, $text, MCRYPT_MODE_ECB, $iv);
Echo $ Crypttext. "\ n
";
Decrypt encrypted content:
$str _decrypt = Mcrypt_decrypt (mcrypt_rijndael_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
Echo $ Str_decrypt;

Below is an example of an Add/Decrypt request:
copy code code as follows:
$request _params = Array (
' controller ' = ' todo ',
' action ' = ' read ',
' username ' = ' bl ',
' use Rpass ' = ' 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. "
";

Decrypt request
$params = Json_decode (Trim (Mcrypt_decrypt (mcrypt_rijndael_256, $private _key, Base64_decode ($enc _request), Mcrypt_ MODE_ECB)), true);
echo "ENCRYPT:
";

Print result
Var_dump ($params);

Note: the parameters in the encryption and decryption functions cipher, key, and mode must correspond to one by one, otherwise the data cannot be restored.

http://www.bkjia.com/PHPjc/328142.html www.bkjia.com true http://www.bkjia.com/PHPjc/328142.html techarticle mcrypt Extension Library can implement encryption and decryption function, that is, the plaintext can be encrypted, or ciphertext can be restored. 1.PHP Encryption Extension Library MCrypt installed in the standard PHP installation process and did not put the Mr ...

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