How to enable encryption and decryption in mcrypt-PHP Tutorial

Source: Internet
Author: User
Tags mcrypt
Enable encryption and detailed parsing of the decryption process in mcrypt. The Mcrypt extension library can implement encryption and decryption, that is, it can encrypt or restore the plaintext. 1. when the PHP encryption extension Library Mcrypt is installed in the standard PHP installation process, the Mr Mcrypt extension library is not allowed to implement encryption and decryption, that is, it can both encrypt the plaintext and restore the ciphertext.

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:

The code is as follows:


$ Str = "I am Li Yun ";
$ Key = "123qwe. 019860905061X ";
$ Cipher = MCRYPT_RIJNDAEL_128;
$ Mode = MCRYPT_MODE_ECB;
$ Iv = mcrypt_create_iv (mcrypt_get_iv_size ($ cipher, $ mode), MCRYPT_RAND );

Echo "original:". $ str ."
";
$ Str_encrypt = mcrypt_encrypt ($ cipher, $ key, $ str, $ mode, $ iv );
Echo "the encrypted content is:". $ str_encrypt ."
";

$ Str_decrypt = mcrypt_decrypt ($ cipher, $ key, $ str_encrypt, $ mode, $ iv );

Echo "decrypted content:". $ str_decrypt ."
";


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 divided 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:

The code is as follows:


// Specify the iv size of the initialization vector:
$ Iv_size = mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB );
// Create the initialization vector:
$ Iv = mcrypt_create_iv ($ iv_size, MCRYPT_RAND );
// Encrypted password:
$ Key = "123qwe. 019860905061x ";
// Original content (unencrypted ):
$ Text = "My name is Adam Li! ";
Echo $ text ."
\ N ";
// Encrypted content:
$ Crypttext = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $ key, $ text, MCRYPT_MODE_ECB, $ iv );
Echo $ crypttext. "\ n
";
// Decrypt the encrypted content:
$ Str_decrypt = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $ key, $ crypttext, MCRYPT_MODE_ECB, $ iv );
Echo $ str_decrypt;


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

The code is as follows:


$ 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 ."
";

// Decrypt request
$ Params = json_decode (trim (mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $ private_key, base64_decode ($ enc_request), MCRYPT_MODE_ECB), true );
Echo "ENCRYPT:
";

// Print result
Var_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.

Bytes. 1. the PHP encryption extension Library Mcrypt is not installed in the standard PHP installation process...

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.