


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; } ?>
The output result is:
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>"; ?>
The output result is:
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 ] )
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 )
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 ] )
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 ] )
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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
