$input ='http://mlaan2.home.xs4all.nl/ispack/isetup-5.5.3.exe';
/**
*加密函数
*$input 要被加密的字符串
*$key 密钥
*/
$key = randomkeys(8);//生成随机密匙
function do_mencrypt($input, $key)
{
$input = base64_encode(trim($input));
//$key = substr(md5($key), 0, 4);
$td = mcrypt_module_open('des', '', 'ecb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$encrypted_data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return trim(base64_encode($encrypted_data));
}
print_r(do_mencrypt($input, $key));
echo "<br/>";
/**
*解密函数
*$input 要被解密的字符串
*$key 密钥
*/
$input1 = do_mencrypt($input, $key);
function do_mdecrypt($input1, $key)
{
$input1 = base64_decode(trim($input1));
$td = mcrypt_module_open('des', '', 'ecb', '');
//$key = substr(md5($key), 0, 4);
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$decrypted_data = mdecrypt_generic($td, $input1);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return trim(base64_decode($decrypted_data));
}
print_r(do_mdecrypt($input1, $key));
#2.rand key: "CWSTOAYD":生成随机密匙,统一用字母或者数字,长度为8.
function randomkeys($length)
{
$pattern = '1234567890';
for($i=0;$i<$length;$i++)
{
$key .= $pattern{rand(0,9)}; //生成php随机数
}
return $key;
}