PHP Encryption Extension Library MCrypt installation and application examples _php tutorial

Source: Internet
Author: User
Tags mcrypt
In PHP if we want to use PHP mcrypt encryption extension Library must first install this encryption extension library, and then can be used, because it is the same as the GD library by default is not installed OH.

MCrypt Brief Introduction

PHP Programmers in the code program, in addition to ensure the high performance of the code, there is one thing is very important, that is the security of the program. In addition to several cryptographic functions, PHP also features a more comprehensive PHP encryption extension library MCrypt and Mhash.
Among them, the MCrypt extension Library can implement the encryption and decryption function, that is, the plaintext can be encrypted or ciphertext restored.
MCrypt is an important encryption support extension Library in PHP, under the Linux environment: the library is not turned on by default. Window environment: php>=5.3, the mcrypt extension is turned on by default.

1. Installation of Mcrypt () library

Mcypt is a very powerful encryption algorithm extension library. In the standard PHP installation process did not put MCrypt installed, but the main directory of PHP contains the Libmcrypt.dll file, so we only use the PHP configuration file in this line: Extension=php_ Mcrypt.dll the semicolon in front of you, 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, and the [1] encryption algorithm can be displayed through functions mcrypt_list_algorithms () and Mcrypt_list_modes ().
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 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;

The code is as follows Copy Code

$str = "My name is?" The average person I don't tell him! "; 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);//initialization vector
echo "Encrypted plaintext:" $str. "

";
$str _encrypt = Mcrypt_encrypt ($cipher, $key, $str, $modes, $IV); Cryptographic functions
echo "Cipher cipher:" $str _encrypt. "

";
$str _decrypt = Mcrypt_decrypt ($cipher, $key, $str _encrypt, $modes, $IV); Decryption function
echo "Restore:". $str _decrypt;
?>

Operation Result:

Encrypt plaintext: What's my name? The average person I don't tell him!
Encryption text: Matte?? Q??? L laugh?????

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


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

http://www.bkjia.com/PHPjc/632833.html www.bkjia.com true http://www.bkjia.com/PHPjc/632833.html techarticle in PHP If we want to use PHP mcrypt encryption extension Library must first install this encryption extension library, and then can be used, because it is the same as the GD library by default is not installed OH. MCrypt Jane ...

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