Home >Backend Development >PHP Tutorial >Introduction to how to enable, encrypt and decrypt php mcrypt
Run the results: Original text: I am Li Yun The encrypted content is: B @鴹?=(I褣Z% Decrypted content: I am Li Yun 1) Before using the PHP encryption extension library Mcrypt to encrypt and decrypt data, first create an initialization vector, referred to as iv for short. From $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND); it can be seen that creating an initialization vector requires two parameters: size specifies the size of iv; source is the source of iv, where the value MCRYPT_RAND is the system random number. 2) The function mcrypt_get_iv_size($cipher,$modes) returns the initialization vector size. The parameters cipher and mode refer to 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 as follows: cipher——encryption algorithm, key——key, data( str)——data that needs to be encrypted, mode——algorithm mode, iv——initialization vector 4), decryption function mcrypt_decrypt($cipher,$key,$str_encrypt,$modes,$iv); The parameters of this function and the encryption function are almost the same. The only difference is data, which means data is the data that needs to be decrypted$ str_encrypt, not the raw data $str. //Writing in the manual:
Example of encryption/decryption request:
Note: The parameters cipher, key and mode in the encryption and decryption functions must correspond one to one, otherwise the data cannot be restored. |