Home> php教程> php手册> body text

mcrypt 如何加/解密 ?

WBOY
Release: 2016-06-21 09:14:00
Original
847 people have browsed it

解密

PHP代码:--------------------------------------------------------------------------------
function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return(float) $sec +((float) $usec * 100000);
}
srand(make_seed());

/* 开启加密算法/ */
$td = mcrypt_module_open('twofish', '', 'ecb', '');

/* 建立 IV,并检测 key 的长度 */
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$ks = mcrypt_enc_get_key_size($td);

/* 生成 key */
$key = substr(md5('very secret key'), 0, $ks);

/* 初始化加密程序 */
mcrypt_generic_init($td, $key, $iv);

/* 加密, $encrypted 保存的是已经加密后的数据 */
print $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);

/* 显示原始字符串 */
echo trim($decrypted)."\n";



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!