Installing and using PHP's MCrypt extension
When programmers write code programs, in addition to ensuring the high performance of the code, there is also a point is very important, that is the security of the program. In addition to the various encryption functions, PHP has a more comprehensive PHP encryption extension library and.
Among them, the MCrypt extension Library can implement the encryption and decryption function, that is, the plaintext can be encrypted or ciphertext restored. PHP encryption extension Library MCrypt algorithm and encryption mode
The MCrypt library supports more than 20 encryption algorithms and 8 encryption modes, which can be displayed through functions mcrypt_list_algorithms () and Mcrypt_list_modes (), with the following results:
MCrypt supported algorithms 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
MCrypt supported encryption modes are:CBC CFB Ctr ECB NCFB NOFB OFB stream
These algorithms and patterns in the application to be expressed as represented, when written with the prefix Mcrypt_ and mcrypt_mode_ to represent, such as the following MCrypt application example:
DES algorithm is expressed as mcrypt_des;
The ECB model is expressed as MCRYPT_MODE_ECB;
PHP Encryption Extension Library MCrypt application
Take a look at an example, understand the workflow of MCrypt, and then look at the functions used by some of the processes:
Encrypt contents//key//password type//password mode
$str = "Hello";
$key = "123";
$cipher = Mcrypt_des;
$modes = MCRYPT_MODE_ECB;
$iv = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $modes), mcrypt_rand);//initialization vector
$str _encrypt = Mcrypt_encrypt ($cipher, $key, $str, $modes, $IV); Cryptographic functions
echo "Cipher cipher:" $str _encrypt. " ";
$str _decrypt = Mcrypt_decrypt ($cipher, $key, $str _encrypt, $modes, $IV); Decryption function
echo "Restore:". $str _decrypt;
Operation Result:
Encrypted plaintext: Hello
Encryption text: C|b? u?| @
Restore: Hello
<1> as can be seen in the example, using PHP encryption extension library MCrypt to encrypt and decrypt data, first create an initialization vector, referred to as IV. by $iv = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $modes), mcrypt_rand); the creation of an initialization vector requires two parameters: size Specifies the magnitude of IV; Source is the origin of IV , where the value Mcrypt_rand is the system random number.
The <2> function mcrypt_get_iv_size ($cipher, $modes) returns the initialization vector size, and the parameter cipher and mode respectively refer to the algorithm and encryption mode.
<3> cryptographic Functions $str_encrypt = Mcrypt_encrypt ($cipher, $key, $str, $modes, $IV); The 5 parameters of the function are as follows: cipher--encryption algorithm, key--key, Data (str)--required to encrypt, mode--algorithm mode, iv--initialization vector
<4> decryption function Mcrypt_decrypt ($cipher, $key, $str _encrypt, $modes, $IV); The function is almost the same as the parameters of the cryptographic function, except that the data is the only one that is $str_encrypt, rather than the raw data $str, which is to be decrypted.
Note: The parameters in the encryption and decryption functions cipher, key, and mode must correspond to one by one, otherwise the data cannot be restored.
Libmcrypt-2.5.8.tar.gz
Mhash-0.9.9.tar.gz
Mcrypt-2.6.8.tar.gz
"A few articles and together, the following special attention, but I found that the installation of mcrypt need to solve the libmcrypt and Mhash dependence, these good solution, no yum can be a bit."
Now you should have Libmcrypt as a shared component (but not a PHP shared component). To run the command:
# Ldconfig
It will allow shared objects to be used in C + + development. The MCrypt dynamic components are compiled into PHP below. First, you need to php-devel the ' phpize ' command contained in the package.
When you have php-devel in your currently running PHP, enter:
# CD Ext/mcrypt
# phpize
# aclocal
#./configure
# Make Clean
# make
# make Install
Now the PHP installation directory should have a mcrypt.so file, added in/etc/php.ini:
Extension=mcrypt.so
Then restart it so that we have successfully installed the MCrypt feature.
Special attention:
1, phpize need to be extracted under the source code package execution, after the execution of the Configure script generated
2. When running./configure, if prompted
Configure:error:Cannot find Php-config. Please use--with-php-config=path
Please use:
./configure--with-php-config=/php-install-path/bin/php-config #而不是使用php. ini
Transferred from: http://www.cnblogs.com/thinksasa/archive/2012/09/14/2684660.html
PHP's MCrypt