Home > Article > Backend Development > Encryption and decryption method implemented by php combined with md5
The example in this article describes the encryption and decryption method implemented by php combined with md5. Share it with everyone for your reference, the details are as follows:
I recently found a good thing when sorting out the code, which combines the md5 encryption and decryption algorithm. There are relatively few online information about the encryption and decryption algorithms of PHP combined with MD5. In fact, it is in the PHP manual. Just change it. Post it here. To use this algorithm, you need to load a php module mcrypt, otherwise it will not be used.
//加密 function string2secret($str) { $key = "123"; $td = mcrypt_module_open(MCRYPT_DES,'','ecb',''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); $ks = mcrypt_enc_get_key_size($td); $key = substr(md5($key), 0, $ks); mcrypt_generic_init($td, $key, $iv); $secret = mcrypt_generic($td, $str); mcrypt_generic_deinit($td); mcrypt_module_close($td); return $secret; } //解密 function secret2string($sec) { $key = "123"; $td = mcrypt_module_open(MCRYPT_DES,'','ecb',''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); $ks = mcrypt_enc_get_key_size($td); $key = substr(md5($key), 0, $ks); mcrypt_generic_init($td, $key, $iv); $string = mdecrypt_generic($td, $sec); mcrypt_generic_deinit($td); mcrypt_module_close($td); return trim($string); } echo secret2string(string2secret("11111111111111111")); //显示结果是11111111111111111 echo string2secret("11111111111111111"); //显示乱码
PHP’s commonly used encryption and decryption functions, base64_encode, base64_decode.
I hope this article will be helpful to everyone in PHP programming.
For more articles related to encryption and decryption methods implemented by PHP combined with md5, please pay attention to the PHP Chinese website!