Encryption and decryption method implemented by php combined with md5, php combined with md5 encryption and decryption_PHP tutorial

WBOY
Release: 2016-07-12 08:59:53
Original
1356 people have browsed it

Encryption and decryption method implemented by PHP combined with md5, PHP combined with md5 encryption and decryption

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 is combined with 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"); //显示乱码

Copy after login

PHP’s commonly used encryption and decryption functions are base64_encode and base64_decode.

Readers who are interested in more content related to PHP encryption and decryption can check out the special topic of this site: "Summary of PHP Encryption Methods"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • PHP encryption and decryption example analysis
  • SSL encryption and decryption, verification, and signature methods under PHP (very simple)
  • Detailed explanation of PHP encryption and decryption string functions with source code download
  • PHP encapsulated string encryption and decryption function
  • thinkphp WeChat development (message encryption and decryption)
  • Detailed explanation of PHP encryption and decryption functions
  • PHP implementation of enhanced encryption and decryption class example
  • php source code analysis of DZX1.5 encryption and decryption function authcode usage
  • PHP decrypts Unicode and Escape encrypted strings
  • How to use PHP rsa encryption and decryption
  • Summary of PHP encryption and decryption strings
  • Analysis of PHP encryption and decryption class examples
  • Detailed explanation of how to use PHP rsa encryption and decryption

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1095688.htmlTechArticleEncryption and decryption method implemented by php combined with md5, php combined with md5 encryption and decryption This article describes the encryption implemented by php combined with md5 Decryption method. Share it with everyone for your reference, the details are as follows: Most...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template