Home > Backend Development > PHP Tutorial > PHP encryption extension library—Mcrypt extension library example usage

PHP encryption extension library—Mcrypt extension library example usage

黄舟
Release: 2023-03-07 12:56:01
Original
4224 people have browsed it

PHP encryption extension library—Mcrypt extension library

Before starting the main text of this article, let’s first understand what is the PHP encryption extension library: PHP In addition to several encryption functions (md5, crypt, sha1), there are also some comprehensive encryption extension libraries in PHP! Just like PHP does not originally support operating a certain function, but if you want to support it in the new version, you can provide it in an extended way. In this way, when we configure PHP, if we do not use this function, we can prevent PHP from loading. It thus saves server resources and improves its performance.

In the previous three articles "PHP encryption function—crypt() function encryption", "PHP encryption function—md5() function encryption" and "PHP encryption function—sha1() function encryption"Introduced the PHP encryption function, then we will introduce to you the PHP encryption extension library!

There are two PHP encryption extension libraries, Mcrypt and Mhash. In this article, we will introduce Mcrypt first!

1.Mcrypt library installation

mcypt is a very powerful encryption algorithm extension library. Mcrypt is not installed during the standard PHP installation process, but the libmcrypt.dll file is included in the PHP home directory, so we only use the semicolon in front of the line extension=php_mcrypt.dll in the PHP configuration file; "Remove it, then restart the server to use this extension library.

2.Mcrypt library constants

The mcrypt library supports more than 20 encryption algorithms and 8 encryption modes. We can directly use the functions mcrypt_list_algorithms() and mcrypt_list_modes( ) to view, the specific code is as follows:

<?php
$atr = mcrypt_list_algorithms();  //函数返回 Mcrypt支持的加密算法数组
echo "支持算法有:";
foreach ($atr as $atr_value){
    echo "<br>".$atr_value;
}
$arr = mcrypt_list_modes();     //函数返回 Mcrypt支持的加密模式数组
echo "<p>支持加密模式有:";
foreach ($arr as $arr_value){
    echo "<br>".$arr_value;
}
?>
Copy after login

The output result is:

PHP encryption extension library—Mcrypt extension library example usage

Note: These algorithms and modes are In actual applications, constants need to be used to represent them. When writing, add the prefixes: MCRYPT_ and MCRYPT_MODE_ to represent them. Example:

TWOFISH algorithm is represented as: MCRYPT_TWOFISH.

CBC encryption mode is expressed as: MCRYPT_MODE_CBC.

3.Mcrypt application

Use Mcrypt for encryption and decryption. If you don’t want to use md5(), sha1() and other functions, just call them directly. In order to let everyone clearly understand the workflow of Mcrypt, let's introduce it directly with code. The specific code is as follows:

<?php
header("Content-Type:text/html; charset=utf-8");
$str = "被加密的内容:PHH中文网www.php.cn";  //加密文本
$key = "key:111";                               //密钥
$cipher = MCRYPT_DES;                           //密码类型
$modes = MCRYPT_MODE_ECB;                      //密码模式
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND); //初始化向量
echo "加密前:".$str."<p>";
$str_encrypt = mcrypt_encrypt($cipher,$key,$str,$modes,$iv);       //加密函数
echo "加密后:".$str_encrypt."<p>";
$str_decrypt =mcrypt_decrypt($cipher,$key,$str_encrypt,$modes,$iv);  //解密函数
echo "还原:".$str_decrypt."<p>";
?>
Copy after login

The output result is:

PHP encryption extension library—Mcrypt extension library example usage

Below we introduce several functions that appeared in the above example:

1.mcrypt_create_iv:

The syntax format is as follows:

string mcrypt_create_iv ( int $size [, int $source = MCRYPT_DEV_URANDOM ] )
Copy after login

Use Mcrypt for data encryption. Before decryption, an initialization vector (referred to as iv) must first be created. Creating an initialization vector requires two parameters: size specifies the size of iv, and source is the source of iv. source can take the following values:

  • MCRYPT_RAND: system random number.

  • MCRYPT_DEV_RANDOM: Read the data in the directory /dev/random (UNIX system).

  • MCRYPT_DEV_URANDOM: Read the data in the directory /dev/urandom (UNIX system).

2.mcrypt_get_iv_size:

Syntax format:

int mcrypt_get_iv_size ( string $cipher , string $mode )
Copy after login

The size of the initialization vector (iv) returned by this function. The two parameters in the function are the encryption algorithm (cipher) and encryption mode (mode) just introduced earlier.

3.mcrypt_encrypt:

The syntax format is as follows:

string mcrypt_encrypt ( string $cipher , string $key , string $data , string $mode [, string $iv ] )
Copy after login

After initializing the vector, you can use the mcrypt_encrypt() encryption function to encrypt the data. The meanings of the five parameters of this function are as follows:

(1)cipher: encryption algorithm. In the above example, it is the variable $cipher. The encryption algorithm here can be different from the encryption algorithm in the initialization vector.

(2)key: Key. The variable $key in the above example.

(3)mode: encryption mode.

(4)iv: Initialization vector.

4.mcrypt_decrypt

The syntax format is as follows:

string mcrypt_decrypt ( string $cipher , string $key , string $data , string $mode [, string $iv ] )
Copy after login

The parameters of the decryption function mcrypt_decrypt() and the encryption function mcrypt_encrypt() are almost the same. The only difference is the parameter data, where data is the data that needs to be decrypted, not the original data.

Note: The cipher, key and mode parameters in the encryption function and decryption function must be consistent, otherwise the data will not be restored.

In the next article, we will continue to introduce the PHP encryption extension library: Mhash extension library. For details, please read "PHP Encryption Extension Library—Mhash Extension Library"!

【Related Recommendations】

1. Relevant topic recommendations: "PHP Encryption Function"

2.PHP encryption extension library-Mhash extension library instance usage detailed explanation



The above is the detailed content of PHP encryption extension library—Mcrypt extension library example usage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template