Home > Article > Backend Development > Mcrypt reversible encryption algorithm in PHP_PHP tutorial
Mcrypt reversible encryption algorithm in PHP
$td = mcrypt_module_open(MCRYPT_DES,'','ecb',''); //Use MCRYPT_DES algorithm, ecb mode
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$ks = mcrypt_enc_get_key_size($td);
$key = "ery secret key";//Key
$key = substr(md5($key), 0, $ks);
mcrypt_generic_init($td, $key, $iv); //Initial processing
//Encryption
$encrypted = mcrypt_generic($td, 'This is very important data');
//End processing
mcrypt_generic_deinit($td);
//Initial decryption processing
mcrypt_generic_init($td, $key, $iv);
//Decryption
$decrypted = mdecrypt_generic($td, $encrypted);
//End
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
//After decryption, there may be subsequent ones that need to be removed
echo trim($decrypted) . "n";