Install and apply the PHP encrypted extension library Mcrypt

Source: Internet
Author: User
Tags mcrypt

In php, if we want to use the PHP Mcrypt to encrypt the extension library, we must first install the encrypted extension library and then use it, because it is not installed by default like the gd library.

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: Copy code

<? 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 ???] 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.