Install and use the PHP encrypted Extension Library Mcrypt

Source: Internet
Author: User
The PHP encryption extension library includes Mcrypt and Mhash. among them, the Mcrypt extension library can implement encryption and decryption. today we are talking about the Mcrypt function and the simple introduction of the instance mcrypt.
When writing code programs, PHP programmers should ensure the high performance of the code, but also ensure the security of the program. In addition to several built-in encryption functions, PHP also provides a more comprehensive PHP encryption extension Library, Mcrypt and Mhash.
Among them, the Mcrypt extension library can implement encryption and decryption, that is, both plaintext encryption and ciphertext restoration.
Mcrypt is an important encryption supporting Extension Library in php. in linux: This library is disabled by default. In the window environment: PHP> = 5.3. the mcrypt extension is enabled by default.
1. install the Mcrypt () Library
Mcypt is a powerful Extension Library for encryption algorithms. In the standard PHP installation process, Mcrypt is not installed, but the main directory of PHP contains libmcrypt. dll file, so we only need to remove the semicolon in the PHP configuration file: extension = php_mcrypt.dll, and then restart the server to use this extension Library.
Supported algorithms and encryption modes
The Mcrypt library supports over 20 encryption algorithms and 8 encryption modes. you can use the mcrypt_list_algorithms () and mcrypt_list_modes () functions to display [1] encryption algorithms.
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
Encryption Mode
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 figure:

Example

The DES algorithm is represented as MCRYPT_DES;
ECB mode indicates MCRYPT_MODE_ECB;
The code is as follows:
$ 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 ."

";
$ Str_encrypt = mcrypt_encrypt ($ cipher, $ key, $ str, $ modes, $ iv); // encryption function
Echo "encrypted ciphertext:". $ str_encrypt ."

";
$ 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 ???] Q ??? L smile ?? "? ?
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, data cannot be restored.

Summary
Mcrypt Library constant
The Mcrypt library supports over 20 encryption algorithms and 8 encryption modes. You can use the mcrypt_list_algorithms () and mcrypt_list_modes () functions.

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.