I have to protect really sensitive information and I have to do it both ways: encryption and decryption. I'll be using this PHP code:
function encrypt($mprhase) {
$MASTERKEY = "KEY PHRASE!";
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $MASTERKEY, $iv);
$crypted_value = mcrypt_generic($td, $mprhase);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return base64_encode($crypted_value);
}
function decrypt($mprhase) {
$MASTERKEY = "KEY PHRASE!";
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $MASTERKEY, $iv);
$decrypted_value = mdecrypt_generic($td, base64_decode($mprhase));
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $decrypted_value;
}
How safe is it?
MCRYPT_DEV_URANDOM
would be considered safer than usingMCRYPT_RAND
, as the latter relies on the system's RNG. They're not always as random as they claim to be. I know it's a tad paranoid, but seeing as this is for the CS division, you never know... – Elias Van Ootegem Dec 21 '13 at 16:41