


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 is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

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

Zend Studio 13.0.1
Powerful PHP integrated development environment