<?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"; |