复制代码 代码如下:
/**
+-----------------------------------------------------
* Mcrypt 加密/解密
* @param String $date 要加密和解密的数据
* @param String $mode encode 默认为加密/decode 为解密
* @return String
* @author zxing@97md.net Mon Sep 14 22:59:28 CST 2009
+-----------------------------------------------------
* @example
*/
function ZxingCrypt($date,$mode = 'encode'){
$key = md5('zxing');//用MD5哈希生成一个密钥,注意加密和解密的密钥必须统一
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;
}
复制代码 代码如下:
<?php
$td = mcrypt_module_open(MCRYPT_DES,'','ecb',''); //使用MCRYPT_DES算法,ecb模式
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$ks = mcrypt_enc_get_key_size($td);
$key = "ery secret key";//密钥
$key = substr(md5($key), 0, $ks);
mcrypt_generic_init($td, $key, $iv); //初始处理
//加密
$encrypted = mcrypt_generic($td, 'This is very important data');
//结束处理
mcrypt_generic_deinit($td);
//初始解密处理
mcrypt_generic_init($td, $key, $iv);
//解密
$decrypted = mdecrypt_generic($td, $encrypted);
//结束
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
//解密后,可能会有后续的/0,需去掉
echo trim($decrypted) . "/n";
?>
新闻热点
疑难解答