The status of data encryption in our lives has become increasingly important, especially given the large amount of data that is being traded and transmitted over the network. For information that does not need to be restored to the original data we can encrypt the data using an irreversible encryption algorithm such as MD5, SHA1, but important information such as transactional 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 computation yourself. In this article we describe the use of the MCrypt module for cryptographic decryption operations.
The advantage of MCrypt is not only that it provides more cryptographic algorithms, it is distributed with PHP packages under Windows, but also that it can add/decrypt data, and it also provides 35 functions for processing data, including the DES algorithm.
Copy CodeThe code is as follows:
/**
+-----------------------------------------------------
* Mcrypt Encryption/decryption
* @param String $date The data to encrypt and decrypt
* @param String $mode encode default to encrypt/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 ');//Hashishen a key with MD5, note that the encryption and decryption keys 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 CodeThe code is as follows:
$TD = Mcrypt_module_open (Mcrypt_des, ', ' ECB ', '); Using the mcrypt_des algorithm, the 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);
Decrypt
$decrypted = Mdecrypt_generic ($TD, $encrypted);
End
Mcrypt_generic_deinit ($TD);
Mcrypt_module_close ($TD);
After decryption, there may be a follow-up, you need to remove
echo Trim ($decrypted). "\ n";
?>
http://www.bkjia.com/PHPjc/323941.html www.bkjia.com true http://www.bkjia.com/PHPjc/323941.html techarticle The status of data encryption in our lives has become increasingly important, especially given the large amount of data that is being traded and transmitted over the network. For those that do not need to be restored to the original data ...