The Mcrypt Library supports more than 20 encryption algorithms and 8 encryption modes. You can use the mcrypt_list_algorithms () and mcrypt_list_modes () functions to display them.
The Mcrypt Library supports more than 20 encryption algorithms and 8 encryption modes. You can use the mcrypt_list_algorithms () and mcrypt_list_modes () functions to display them.
The Mcrypt extension library can implement encryption and decryption, that is, it can encrypt or restore the plaintext.
1. Install the PHP encryption extension library Mcrypt
In the standard PHP installation process, Mrcypt is not installed. The record-free space is required. However, the main directory of PHP contains libmcrypt. dll and libmhash. dll file (libmhash. dll is the Mhash extension library, which can be installed together here ). First, copy the two files to the system directory windows \ system32, and then in PHP. press Ctrl + F in the INI file to jump out of the search box, and find the; extension = php-mcrypt.dll and; extension = php_mhash.dll statements, and then remove the previous ";"; finally, save and restart the Apache server.
2. PHP encryption extension library Mcrypt algorithm and encryption mode
The Mcrypt Library supports more than 20 encryption algorithms and 8 encryption modes. You can use the mcrypt_list_algorithms () and mcrypt_list_modes () functions to display them. The results are as follows:
Mcrypt supports the following algorithms: 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 supports the following encryption modes: cbc cfb ctr ecb ncfb nofb ofb stream
These algorithms and modes must be expressed as constants in the application, and the prefix MCRYPT _ and MCRYPT _ should be added during writing, as shown in the following example of Mcrypt application:
The DES algorithm is represented as MCRYPT_DES;
ECB mode indicates MCRYPT_MODE_ECB;
3. PHP encryption extension library Mcrypt Application
Let's take a look at an example to learn about the Mcrypt workflow, Hong Kong server. Then let's take a look at the functions used in some processes:
The Code is as follows:
$ Str = "I am Li Yun ";
$ 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 "the encrypted content is:". $ str_encrypt ."
";
$ Str_decrypt = mcrypt_decrypt ($ cipher, $ key, $ str_encrypt, $ mode, $ iv );
Echo "decrypted content:". $ str_decrypt ."
";
Running result:
Original article: Li Yun
The encrypted content is: B @ brief = (I debate between Z %
Decrypted content: I am Li Yun
<1> in this example, we can see that before using the PHP encrypted extension library Mcrypt to encrypt and decrypt data, we first created an initialization vector, an American server, or iv. From $ iv = mcrypt_create_iv, the value MCRYPT_RAND is the system random number.
<2> the mcrypt_get_iv_size ($ cipher, $ modes) function returns the size of the initialization vector. The parameters cipher and mode indicate the algorithm and encryption mode respectively.
<3> encryption function $ str_encrypt = mcrypt_encrypt ($ cipher, $ key, $ str, $ modes, $ iv); the five parameters of this function are classified as follows: cipher-encryption algorithm, key-key, data (str)-data to be encrypted, mode-algorithm mode, iv-initialization Vector
<4> the decryption function mcrypt_decrypt ($ cipher, $ key, $ str_encrypt, $ modes, $ iv). This function has almost the same parameters as the encryption function. The only difference is data, that is to say, data is the data to be decrypted $ str_encrypt, rather than the original data $ str.
// Method in the manual:
The Code is as follows:
// Specify the iv size of the initialization vector:
$ Iv_size = mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB );
// Create the 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 ."
\ N ";
// Encrypted content:
$ Crypttext = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $ key, $ text, MCRYPT_MODE_ECB, $ iv );
Echo $ crypttext. "\ n
";
// Decrypt the encrypted content:
$ Str_decrypt = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $ key, $ crypttext, MCRYPT_MODE_ECB, $ iv );
Echo $ str_decrypt;
The following is an example of an encryption/Decryption request:
The Code is 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 ."
";
// 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 cipher, key, and mode in the encryption and decryption functions must correspond one to one, otherwise the data cannot be restored.