Share PHP Encryption extension Library MCrypt installation and application tips _php Tutorial

Source: Internet
Author: User

Among them, the MCrypt extension Library can implement the encryption and decryption function, that is, the plaintext can be encrypted or ciphertext restored.

1.PHP Encryption Extension Library MCrypt installation

The standard PHP installation process did not install MRCYPT, but the main directory of PHP contains the Libmcrypt.dll and Libmhash.dll files (Libmhash.dll is the Mhash extension library, which can be installed together). First, copy these two files to the system directory Windowssystem32, and then press ctrl+f shortcut key in the php.ini file to jump out of the lookup box and find; Extension=php-mcrypt.dll and; Extension=php_mhash.dll these two statements, then remove the previous ";", and finally, save and restart the Apache server to take effect.

2.PHP Encryption extension Library MCrypt algorithm and encryption mode

The MCrypt library supports more than 20 encryption algorithms and 8 encryption modes, which can be displayed through functions mcrypt_list_algorithms () and Mcrypt_list_modes (), with the following results:

MCrypt supported algorithms are: cast-128 gost rijndael-128 twofish arcfour cast-256 loki97 rijndael-192 saferplus wake Blowfish-compat des Ri jndael-256 Serpent Xtea blowfish Enigma RC2 TripleDES

MCrypt supported encryption modes are: CBC CFB Ctr ECB NCFB NOFB OFB stream

These algorithms and patterns in the application to be expressed as represented, when written with the prefix Mcrypt_ and mcrypt_ to represent, such as the following MCrypt application example:

DES algorithm is expressed as mcrypt_des;
The ECB model is expressed as MCRYPT_MODE_ECB;

3.PHP Encryption Extension Library MCrypt application

Take a look at an example, understand the workflow of MCrypt, and then look at the functions used by some of the processes:

 
 
  1. < ? PHP
  2. $ Str = "My name is?" The average person I don't tell him! ";
  3. Encrypt content
  4. $ Key = "key:111" ;
  5. Secret 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. " < P > ";
  12. $ Str_encrypt = Mcrypt_encrypt ($cipher,
    $key, $str, $modes, $IV);
  13. Cryptographic functions
  14. echo "Cipher cipher:" $str _encrypt. " < P > ";
  15. $ Str_decrypt = Mcrypt_decrypt ($cipher,
    $key, $str _encrypt, $modes, $IV);
  16. Decryption function
  17. echo "Restore:". $str _decrypt;
  18. ?>

Operation Result:

Encrypt plaintext: What's my name? The average person I don't tell him!
Encryption text: Matte bowls?] 鸴 Q 攦 軄 l laugh 郺 葄 "簻 mo
Restore: My name is? The average person I don't tell him!

<1> as can be seen in the example, using PHP encryption extension library MCrypt to encrypt and decrypt data, first create an initialization vector, referred to as IV. by $iv = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $modes), mcrypt_rand); the creation of an initialization vector requires two parameters: size Specifies the magnitude of IV; Source is the origin of IV , where the value Mcrypt_rand is the system random number.

The <2> function mcrypt_get_iv_size ($cipher, $modes) returns the initialization vector size, and the parameter cipher and mode respectively refer to the algorithm and encryption mode.

<3> cryptographic Functions $str_encrypt = Mcrypt_encrypt ($cipher, $key, $str, $modes, $IV); The 5 parameters of the function are as follows: cipher--encryption algorithm, key--key, Data (str)--required to encrypt, mode--algorithm mode, iv--initialization vector

<4> decryption function Mcrypt_decrypt ($cipher, $key, $str _encrypt, $modes, $IV); The function is almost the same as the parameters of the cryptographic function, except that the data is the only one that is $str_encrypt, rather than the raw data $str, which is to be decrypted.

Note: The parameters in the encryption and decryption functions cipher, key, and mode must correspond to one by one, otherwise the data cannot be restored.


http://www.bkjia.com/PHPjc/446040.html www.bkjia.com true http://www.bkjia.com/PHPjc/446040.html techarticle among them, the MCrypt extension Library can implement the encryption and decryption function, that is, the plaintext can be encrypted or ciphertext restored. 1.PHP Encryption Extension Library MCrypt installed in the standard PHP installation process does not have ...

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