I just noticed that in your original example you are encrypting and decrypting both the key and the input - wouldn't you be wanting to decrypt the encrypted string? 🙂
Anyhow, I've never actually used these before so I thought I'd better start.
Starting with the second example in the Mcrypt chapter's intro ... (this is before I found the examples given under mcrypt_module_open):
$key = "this is a secret key";
$input = "Let us meet at 9 o'clock at the secret place.";
$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);
echo $encrypted_data;
I guess the decryption process is pretty similar.
// already have $key and $encrypted_data
// append '2' just to keep things separate
$td2 = mcrypt_module_open ('tripledes', '', 'ecb', '');
$iv2 = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td2), MCRYPT_RAND);
mcrypt_generic_init ($td2, $key, $iv2);
$output = mdecrypt_generic ($td2, $encrypted_data);
mcrypt_generic_deinit ($td2);
mcrypt_module_close ($td2);
echo $output;
I get a notice about mcrypt_generic_deinit being undefined. There's a user note on the manual page about that function that points out that libmcrypt's author has a tendency to change the API. So for now I just comment those lines out and see what happens.
That works. Now it's a matter of finding out just what mcrypt_generic_deinit is supposed to do, and why it was removed from the API. If that information is to be found anywhere, it's in the mcrypt man page. Huh, seems to be out of date - let's make a guess and combine mcrypt_generic_deinit() and mcrypt_module_close() into a single mcrypt_generic_end(). The manual says it's deprecated, but the installed PHP or mcrypt() extension appears not to recognise deinit(). It recognises end(), though, so that could be an adequate workaround.
Yep. That works too. But I think more investigation may be warranted - it may be that the need for deinit() is not so critical in this environment.