SoFunction
Updated on 2025-03-02

PHP encryption extension library Mcrypt installation and instance

Brief introduction to mcrypt
When writing code programs, in addition to ensuring the high performance of the code, there is another very important thing that PHP programmers have, that is, the security of the program. In addition to several encryption functions that come with PHP, it also has more comprehensive PHP encryption extension libraries Mcrypt and Mhash.
Among them, the Mcrypt extension library can implement encryption and decryption functions, which is to encrypt plain text and restore ciphertext.
mcrypt is an important encryption support extension library in PHP. In the Linux environment: this library is not enabled by default. In the window environment: PHP>=5.3, mcrypt extension is enabled by default.
1. Installation of Mcrypt() library
mcypt is a very powerful encryption algorithm extension library. Mcrypt is not installed during the standard PHP installation, but the PHP home directory contains files, so we only need to remove the semicolon in front of this line in the PHP configuration file: extension=php_mcrypt.dll, and then restart the server to use this extension library.
Supported algorithms and encryption modes
The Mcrypt library supports more than 20 encryption algorithms and 8 encryption modes. Specifically, the [1] encryption algorithm can be displayed through the 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 should be represented by constants in applications, and should be represented by prefixes MCRYPT_ and MCRYPT_ when writing, as shown below.

example

The DES algorithm is expressed as MCRYPT_DES;
ECB mode is represented as MCRYPT_MODE_ECB;

Copy the codeThe code is as follows:

<?php
$str = "What is my name? I don't tell him ordinary people!"; //Encrypted content
$key = "key:111"; //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 "encrypted plaintext: ".$str."<p>";
$str_encrypt = mcrypt_encrypt($cipher,$key,$str,$modes,$iv); // Encryption function
echo "encrypted ciphertext: ".$str_encrypt." <p>";
$str_decrypt = mcrypt_decrypt($cipher,$key,$str_encrypt,$modes,$iv); //Decrypt function
echo "Restore:".$str_decrypt;
?>
 

Running results:
Encrypted plaintext: What is my name? I won’t tell him ordinary people!
Encrypted ciphertext: Key??]??q???L laugh ??"? ?
Restore: What is my name? I won’t tell him ordinary people!
<1> As can be seen from the example, before using the PHP encryption extension library Mcrypt to encrypt and decrypt data, an initialization vector is first created, referred to as iv for short. From $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND); it can be seen that creating an initialization vector requires two parameters: size specifies the size of the iv; source is the source of the iv, where the value MCRYPT_RAND is the system random number.
<2>The function mcrypt_get_iv_size($cipher,$modes) returns the initialization vector size, and the parameters cipher and mode refer to 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 as follows: cipher—encryption algorithm, key—key, data(str)—data that needs to be encrypted, mode—algorithm mode, iv—initialization vector
<4>Decrypt function mcrypt_decrypt($cipher,$key,$str_encrypt,$modes,$iv); This function is almost the same as the encryption function. The only difference is data, that is, data is the data that needs to be decrypted, rather than the original data $str.
Note: The parameters cipher, key and mode in the encryption and decryption functions must correspond one by one, otherwise the data cannot be restored.

Summarize
mcrypt library constant
The Mcrypt library supports more than 20 encryption algorithms and 8 encryption modes. You can view it through the functions mcrypt_list_algorithms() and mcrypt_list_modes().