Among them, the Mcrypt extension library can implement encryption and decryption, that is, both plaintext encryption and ciphertext restoration.
1. Install the PHP encryption extension library Mcrypt
Mrcypt is not installed in the standard PHP installation process, but 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 windowssystem32, 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. Then let's take a look at the functions used in some of the processes:
- <? Php
- $ Str = "What is my name? I will not tell him the average person! ";
- // Encrypted content
- $ Key = "key: 111 ";
- // Key
- $ Cipher = MCRYPT_DES;
- // Password type
- $ Modes = MCRYPT_MODE_ECB;
- // Password Mode
- $ Iv = mcrypt_create_iv (mcrypt_get_iv_size
($ Cipher, $ modes), MCRYPT_RAND); // initialization Vector
- Echo "encrypted plaintext:". $ str. "<p> ";
- $ Str_encrypt = mcrypt_encrypt ($ cipher,
$ Key, $ str, $ modes, $ iv );
- // Encryption function
- Echo "encrypted ciphertext:". $ str_encrypt. "<p> ";
- $ Str_decrypt = mcrypt_decrypt ($ cipher,
$ Key, $ str_encrypt, $ modes, $ iv );
- // Decryption Function
- Echo "Restore:". $ str_decrypt;
- ?>
Running result:
Encrypted plaintext: What is my name? I will not tell him the average person!
Encrypted ciphertext: ciphertext?] Why? Q? Why?
Restore: What is my name? I will not tell him the average person!
<1> as you can see in the example, before using the PHP encrypted extension library Mcrypt to encrypt and decrypt data, an initialization vector (iv for short) is created. 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.
Note: The parameters cipher, key, and mode in the encryption and decryption functions must correspond one to one, otherwise the data cannot be restored.