Detailed analysis of mcrypt enabled encryption and decryption process_PHP tutorial

WBOY
Release: 2016-07-21 14:59:17
Original
981 people have browsed it

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:

Copy code The code is as follows:

$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:

Copy code The code is as follows:

// 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:
Copy code The code is as follows:

$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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328142.htmlTechArticleThe Mcrypt extension library can realize encryption and decryption functions, that is, it can not only encrypt plain text, but also restore cipher text. 1. PHP encryption extension library Mcrypt installation In the standard PHP installation process, Mr...
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