Share The PHP encryption extension Library Mcrypt installation and application tips _ PHP Tutorial

Source: Internet
Author: User
Share the installation and application skills of the PHP encrypted Extension Library Mcrypt. Among them, the Mcrypt extension library can implement encryption and decryption, that is, both plaintext encryption and ciphertext restoration. 1. the PHP encryption extension Library Mcrypt is not installed in the standard PHP installation process

Among them, the Mcrypt extension library can implement encryption and decryption, that is, both plaintext encryption and ciphertext restoration.

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 windowssystem32, 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:

 
 
  1. <? Php
  2. $ Str = "What is my name? I will not tell him the average person! ";
  3. // Encrypted content
  4. $ Key = "key: 111 ";
  5. // Key
  6. $ Cipher = MCRYPT_DES;
  7. // Password type
  8. $ Modes = MCRYPT_MODE_ECB;
  9. // Password mode
  10. $ Iv = mcrypt_create_iv (mcrypt_get_iv_size
    ($ Cipher, $ modes), MCRYPT_RAND); // Initialization vector
  11. Echo "encrypted plaintext:". $ str ."

    ";

  12. $ Str_encrypt = mcrypt_encrypt ($ cipher,
    $ Key, $ str, $ modes, $ iv );
  13. // Encryption function
  14. Echo "encrypted ciphertext:". $ str_encrypt ."

    ";

  15. $ Str_decrypt = mcrypt_decrypt ($ cipher,
    $ Key, $ str_encrypt, $ modes, $ iv );
  16. // Decryption function
  17. Echo "Restore:". $ str_decrypt;
  18. ?>

Running result:

Encrypted plaintext: What is my name? I will not tell him the average person!
Encrypted ciphertext: ciphertext?] Why? Q? why?
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 the data cannot be restored.


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

Related Article

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.