PHP Cryptographic Extension Library MCrypt installation and instance _php instance

Source: Internet
Author: User
Tags mcrypt

MCrypt Simple Introduction
PHP programmers in writing code programs, in addition to ensure the high performance of the code, but also is very important, that is, the security of the program. In addition to the several encryption functions, PHP has a more full-featured PHP encryption extension Library MCrypt and Mhash.
Among them, the MCrypt extension Library can achieve encryption and decryption function, that is, both plaintext can be encrypted or ciphertext can be restored.
MCrypt is an important cryptographic support extension Library in PHP, under Linux: The Library is not opened by default. Window environment: php>=5.3, open MCrypt extension by default.
1, Mcrypt () library installation
Mcypt is a very powerful encryption algorithm extension library. In the standard PHP installation process did not put the MCrypt installation, but the PHP home directory contains libmcrypt.dll files, so we only use the PHP configuration file in this line: Extension=php_ Mcrypt.dll the semicolon before you remove it, and then restart the server to use the extension library.
Supported Algorithms and encryption modes
The MCrypt library supports more than 20 encryption algorithms and 8 encryption modes, which can be displayed by using function Mcrypt_list_algorithms () and Mcrypt_list_modes () to display [1] encryption algorithms
The algorithms supported by MCrypt are:
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
The encryption modes supported by MCrypt are:
Cbc
Cfb
Ctr
ECB
Ncfb
Nofb
Ofb
Stream
These algorithms and patterns in the application to be expressed in Changshilai, write the prefix mcrypt_ and mcrypt_ to express, such as the following MCrypt applied

Example

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

Copy Code code as follows:

<?php
$str = "My name is?" I don't tell him about the average person! "; Encrypt content
$key = "key:111"; Secret key
$cipher = Mcrypt_des; Password type
$modes = MCRYPT_MODE_ECB; Password mode
$iv = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $modes), mcrypt_rand);//Initialize vector
echo "Encrypt plaintext:". $str. " <p> ";
$str _encrypt = Mcrypt_encrypt ($cipher, $key, $str, $modes, $IV); Cryptographic functions
echo "Encrypted ciphertext:" $str _encrypt. "<p>";
$str _decrypt = Mcrypt_decrypt ($cipher, $key, $str _encrypt, $modes, $IV); Decryption function
echo "Restore:" $str _decrypt;
?>

Run Result:
Encrypted plaintext: My name is? I don't tell him about the average person!
Encryption text: Matte?? Q??? L laugh????
Restore: My name is? I don't tell him about the average person!
<1> as can be seen from the example, the use of the PHP encryption extension Library MCrypt Data encryption and decryption, first created an initialization vector, referred to as IV. by the $IV = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $modes), Mcrypt_rand); it takes two parameters to create the initialization vector: size Specifies the size of IV; source is 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 cipher and mode respectively refer to the algorithm and the encryption mode.
<3> Cryptographic function $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)--Need encrypted data, 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, and the only difference is data, which means that it is $str_encrypt, rather than the original data, to be decrypted. $str
Note: The parameter cipher, key and mode must correspond one by one in the encryption and decryption function, otherwise the data cannot be restored

Summarize
MCrypt Library Constants
The MCrypt library supports more than 20 encryption algorithms and 8 encryption modes. Can be viewed through functions mcrypt_list_algorithms () and Mcrypt_list_modes ().

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.