Data encryption has become increasingly important in our lives, especially given the vast amounts of transactions that occur and the vast amounts of data that are transmitted over the Internet. For information that does not need to be restored to the original data, we can use irreversible encryption algorithms such as MD5 and SHA1 to encrypt the data. However, important information such as transaction information that needs to be restored to the original data must be encrypted using a restoreable encryption algorithm. Of course, you can write a reversible encryption algorithm yourself to perform encryption and decryption calculations. In this article we introduce the use of the mcrypt module for encryption and decryption operations.
The advantage of Mcrypt is not only that it provides many encryption algorithms, which are released together with the PHP package under Windows, but also that it can encrypt/decrypt data. In addition, it also provides encryption algorithms including the DES algorithm. 35 functions for processing data.
Copy code The code is as follows:
/**
+------------------------------------------------ -------
* Mcrypt encryption/decryption
* @param String $date Data to be encrypted and decrypted
* @param String $mode encode Default is encryption/decode is decryption
* @return String
* @author zxing@97md.net Mon Sep 14 22:59:28 CST 2009
+--------------------- --------------------------------
* @example
*/
function ZxingCrypt($date,$ mode = 'encode'){
$key = md5('zxing');//Use MD5 hash to generate a key. 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;
}
Code from other netizens
Copy code The code is as follows:
$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
http://www.bkjia.com/PHPjc/323941.html
www.bkjia.com
truehttp: //www.bkjia.com/PHPjc/323941.htmlTechArticleData encryption has become more and more important in our lives, especially considering what happens on the Internet Large volumes of transactions and large amounts of data transferred. For those who do not need to restore to the original data...