


Detailed analysis of mcrypt enabled encryption and decryption process_PHP tutorial
The Mcrypt extension library can implement encryption and decryption functions, that is, it can not only encrypt plaintext but also restore ciphertext.
1. PHP encryption extension library Mcrypt installation
Mrcypt is not installed during the standard PHP installation process, but the main directory of PHP contains the libmcrypt.dll and libmhash.dll files (libmhash.dll is the Mhash extension library and can be installed together here). First, copy these two files to the system directory windowssystem32, then press the Ctrl+F shortcut key in the PHP.ini file to pop up the search box, and find; extension=php-mcrypt.dll and; extension=php_mhash.dll statement, then remove the preceding ";"; finally, save and restart the Apache server to take effect.
2. Algorithm and encryption mode of PHP encryption extension library Mcrypt
Mcrypt library supports more than 20 encryption algorithms and 8 encryption modes. Specifically, you can use the functions mcrypt_list_algorithms() and mcrypt_list_modes () to display, the results are as follows:
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
The encryption modes supported by Mcrypt are: cbc cfb ctr ecb ncfb nofb ofb stream
These algorithms and modes should be expressed as constants in applications. When writing, add the prefixes MCRYPT_ and MCRYPT_ to represent them, such as the following example of Mcrypt application:
DES algorithm is represented as MCRYPT_DES;
ECB mode is represented as MCRYPT_MODE_ECB;
3. PHP encryption extension library Mcrypt application
First look at an example to understand the workflow of Mcrypt, and then look at the functions used in some processes:
$str = "I am Li Yun";
$key = "123qwe.019860905061X";
$cipher = MCRYPT_RIJNDAEL_128;
$mode = MCRYPT_MODE_ECB;
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$mode),MCRYPT_RAND);
echo "Original text: ".$str."
";
$str_encrypt = mcrypt_encrypt($cipher,$key,$str,$mode,$iv);
echo "After encryption The content is: ".$str_encrypt."
";
$str_decrypt = mcrypt_decrypt($cipher,$key,$str_encrypt,$mode,$iv);
echo "Decrypted content: ".$str_decrypt."
";
Run result:
Original text: I am Li Yun
The encrypted content is: B @鴹�=(I argue Z%
The decrypted content: I am Li Yun
<1>As you can see from the example, before using the PHP encryption extension library Mcrypt to encrypt and decrypt data, an initialization vector, referred to as iv for short, is first created. 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 iv; source is the source of iv, and the value MCRYPT_RAND is the system random number.
<2>The function mcrypt_get_iv_size($cipher,$modes) returns the initialization vector size. 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 to be encrypted, mode——algorithm mode, iv——initialization vector
<4>Decryption function mcrypt_decrypt($cipher,$key,$str_encrypt,$modes,$iv); The parameters of this function and the encryption function are almost the same. The only difference is data. That is to say, data is the data that needs to be decrypted $str_encrypt, not the original data $str.
//Writing in the manual:
// Specify the size of the initialization vector iv:
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
//Create the initialization vector:
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
//Encrypt the password :
$key = "123qwe.019860905061x";
//Original content (unencrypted):
$text = "My name is Adam Li!";
echo $text. "< br>n";
//Encrypted content:
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
echo $crypttext. "n
" ;
//Decrypt the encrypted content:
$str_decrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
echo $str_decrypt;
The following is an example of an encryption/decryption request:
$request_params = array(
'controller' => 'todo',
'action' => 'read',
'username' => "bl",
'userpass' => "a1 "
);
$private_key = "28e336ac6c9423d946ba02d19c6a2632";
//encrypt request
$enc_request = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $private_key, json_encode($request_params), MCRYPT_MODE_ECB));
echo "CRYPT:".$enc_request."
//decrypt request
$params = json_decode(trim(mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $private_key, base64_decode($enc_request), MCRYPT_MODE_ECB )),true);
echo "ENCRYPT:
";
//print result
var_dump($params);
Note: The parameters cipher, key and in the encryption and decryption functions The modes must correspond one to one, otherwise the data cannot be restored.

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To extend PHP function functionality, you can use extensions and third-party modules. Extensions provide additional functions and classes that can be installed and enabled through the pecl package manager. Third-party modules provide specific functionality and can be installed through the Composer package manager. Practical examples include using extensions to parse complex JSON data and using modules to validate data.

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

[Analysis of the meaning and usage of midpoint in PHP] In PHP, midpoint (.) is a commonly used operator used to connect two strings or properties or methods of objects. In this article, we’ll take a deep dive into the meaning and usage of midpoints in PHP, illustrating them with concrete code examples. 1. Connect string midpoint operator. The most common usage in PHP is to connect two strings. By placing . between two strings, you can splice them together to form a new string. $string1=&qu

Analysis of new features of Win11: How to skip logging in to a Microsoft account. With the release of Windows 11, many users have found that it brings more convenience and new features. However, some users may not like having their system tied to a Microsoft account and wish to skip this step. This article will introduce some methods to help users skip logging in to a Microsoft account in Windows 11 and achieve a more private and autonomous experience. First, let’s understand why some users are reluctant to log in to their Microsoft account. On the one hand, some users worry that they

"Understanding VSCode: What is this tool used for?" 》As a programmer, whether you are a beginner or an experienced developer, you cannot do without the use of code editing tools. Among many editing tools, Visual Studio Code (VSCode for short) is very popular among developers as an open source, lightweight, and powerful code editor. So, what exactly is VSCode used for? This article will delve into the functions and uses of VSCode and provide specific code examples to help readers

PHP function return value types can be expressed as type description syntax, which clearly specifies the return value type of each function. Understanding return value types is critical to creating extensions that are compatible with the PHP core engine, avoiding unexpected conversions, improving efficiency, and enhancing code readability. Specifically, extension functions can define a return value type so that the PHP engine can optimize code execution based on that type and allow developers to explicitly handle the return value. In practice, extension functions can return PHP objects, and PHP code can handle the returned results according to the return value type.

What is GateToken(GT) currency? GT (GateToken) is the native asset on the GateChain chain and the official platform currency of Gate.io. The value of GT coins is closely related to the development of Gate.io and GateChain ecology. What is GateChain? GateChain was born in 2018 and is a new generation of high-performance public chain launched by Gate.io. GateChain focuses on protecting the security of users' on-chain assets and providing convenient decentralized transaction services. GateChain's goal is to build an enterprise-level secure and efficient decentralized digital asset storage, distribution and transaction ecosystem. Gatechain has original

Mobile Hejiaqin APP is a comprehensive software that integrates family management, intelligent control, and family communication. It aims to create a comfortable, intelligent and harmonious home environment for users through intelligent and convenient operations. Through this application, users can easily control and manage various smart devices at home and enjoy the convenience brought by smart life. So what are the specific functions of the Mobile and Jiaqin App? Users who want to know more about it can follow this article to learn more about it! Tutorial on how to use the Mobile and Jiaqin app: What are the uses of the Mobile and Jiaqin app? Even if you don’t know IT, you can easily manage the network. 2. No matter how many smart products you have, one app is enough. 3. Even if you are thousands of miles away from home, you can still "go home" to watch it. See 4. Rich functions, enjoy smart life
