代码如下:
//$input stuff to decrypt
//$key the secret key to use
function do_mencrypt($input $key)
{
$input = str_replace(""n" "" $input);
$input = str_replace(""t" "" $input);
$input = str_replace(""r" "" $input);
$key = substr(md($key) );
$td = mcrypt_module_open(tripledes 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(chop(base_encode($encrypted_data)));
}
//$input stuff to decrypt
//$key the secret key to use
function do_mdecrypt($input $key)
{
$input = str_replace(""n" "" $input);
$input = str_replace(""t" "" $input);
$input = str_replace(""r" "" $input);
$input = trim(chop(base_decode($input)));
$td = mcrypt_module_open(tripledes ecb );
$key = substr(md($key) );
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td) MCRYPT_RAND);
mcrypt_generic_init($td $key $iv);
$decrypted_data = mdecrypt_generic($td $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return trim(chop($decrypted_data));
}