Detailed description of mcrypt enabling, encryption, and decryption Processes

Source: Internet
Author: User
Tags mcrypt

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

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 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. Then let's take a look at the functions used in some of the processes:

$ Str = "I am Li Yun"; $ key = "123qwe. 019860905061X "; $ cipher = require; $ mode = MCRYPT_MODE_ECB; $ iv = mcrypt_create_iv (encrypt ($ cipher, $ mode), MCRYPT_RAND); echo" Original :". $ str. "<br>"; $ str_encrypt = mcrypt_encrypt ($ cipher, $ key, $ str, $ mode, $ iv); echo "the encrypted content is :". $ str_encrypt. "<br>"; $ str_decrypt = mcrypt_decrypt ($ cipher, $ key, $ str_encrypt, $ mode, $ iv); echo "decrypted content :". $ str_decrypt. "<br> ";

Running result:

Original article: Li Yun

The encrypted content is: B @ brief = (I debate between Z %

Decrypted content: I am Li Yun

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

// Method in the manual:

// Specify the size of the initialization vector iv: $ iv_size = mcrypt_get_iv_size (encrypt, MCRYPT_MODE_ECB); // create the initialization vector: $ iv = mcrypt_create_iv ($ iv_size, MCRYPT_RAND ); // encryption password: $ key = "123qwe. 019860905061x "; // original content (unencrypted): $ text =" My name is Adam Li! "; Echo $ text. "<br> \ n"; // encrypted content: $ crypttext = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $ key, $ text, MCRYPT_MODE_ECB, $ iv); echo $ crypttext. "\ n <br>"; // decrypt encrypted content: $ str_decrypt = mcrypt_decrypt (encrypt, $ key, $ crypttext, MCRYPT_MODE_ECB, $ iv); echo $ str_decrypt;

The following is an example of an encryption/Decryption request:

$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."<br/>";//decrypt request$params = json_decode(trim(mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $private_key, base64_decode($enc_request), MCRYPT_MODE_ECB )),true);echo "ENCRYPT:<br/>";//print resultvar_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.

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.