The role of data encryption in our lives has become increasingly important, especially given the large number of transactions and data transmissions that occur on the web. For information that does not need to be restored to the original data, we can encrypt the data using MD5, SHA1 and other irreversible encryption algorithms, but important information such as transaction information that needs to be restored to the original data must be encrypted using a reversible encryption algorithm. Of course you can write a reversible encryption algorithm to encrypt and decrypt the calculation. In this article we describe the use of the MCrypt module for encryption and decryption operations.
The advantage of MCrypt is not only that it provides more encryption algorithms, is published with the PHP package under Windows, but that it can add/decrypt data, and it also provides 35 functions for processing data, including the DES algorithm.
Copy Code code as follows:
/**
+-----------------------------------------------------
* Mcrypt Encryption/decryption
* @param String $date data to encrypt and decrypt
* @param String $mode encode default to encryption/decode for decryption
* @return String
* @author zxing@97md.net Mon Sep 22:59:28 CST 2009
+-----------------------------------------------------
* @example
*/
function Zxingcrypt ($date, $mode = ' encode ') {
$key = MD5 (' zxing ');//MD5 Hashishen into a key, note that the encryption and decryption key must be unified
if ($mode = = ' decode ') {
$date = Base64_decode ($date);
}
if (function_exists (' Mcrypt_create_iv ')) {
$iv _size = mcrypt_get_iv_size (mcrypt_rijndael_256, MCRYPT_MODE_ECB);
$iv = Mcrypt_create_iv ($iv _size, Mcrypt_rand);
}
if (Isset ($iv) && $mode = = ' encode ') {
$passcrypt = Mcrypt_encrypt (mcrypt_rijndael_256, $key, $date, MCRYPT_MODE_ECB, $IV);
}elseif (Isset ($iv) && $mode = = ' decode ') {
$passcrypt = Mcrypt_decrypt (mcrypt_rijndael_256, $key, $date, MCRYPT_MODE_ECB, $IV);
}
if ($mode = = ' encode ') {
$passcrypt = Base64_encode ($passcrypt);
}
return $passcrypt;
}
Other user's Code
Copy Code code as follows:
<?php
$td = Mcrypt_module_open (Mcrypt_des, ', ' ECB ', ');//Use MCrypt _des algorithm, ECB mode
$iv = Mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), Mcrypt_rand);
$ks = mcrypt_enc_get_key_size ($TD);
$key = "ery secret key";//Key
$key = substr (MD5 ($key), 0, $ks);
Mcrypt_generic_init ($TD, $key, $IV);//initial processing
//encryption
$encrypted = Mcrypt_generic ($TD, ' This is very important Data ');
//End processing
Mcrypt_generic_deinit ($TD);
//Initial decryption processing
Mcrypt_generic_init ($TD, $key, $IV);
//decryption
$decrypted = Mdecrypt_generic ($TD, $encrypted);
//End
Mcrypt_generic_deinit ($TD);
Mcrypt_module_close ($TD);
//After decryption, there may be subsequent, and need to remove
echo trim ($decrypted). "\ n";
?>