What functions does php md5() achieve?

藏色散人
Release: 2023-03-11 07:34:01
Original
1523 people have browsed it

php md5() can implement the encryption and decryption function. The implementation method is: 1. Encryption is achieved through the "function string2secret($str){...}" method; 2. Through the "function secret2string($sec) {...}" method to implement decryption.

What functions does php md5() achieve?

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

What functions does php md5() achieve?

php md5() can implement encryption and decryption functions.

The encryption and decryption method implemented by php combined with md5 is 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 frequently used encryption and decryption functions, base64_encode, base64_decode.

The definition and usage of the md5 function

md5() function calculates the MD5 hash of a string.

The md5() function uses RSA data security, including the MD5 message digest algorithm.

Explanation from RFC 1321 - MD5 message digest algorithm: The MD5 message digest algorithm takes information of any length as an input value and converts it into a 128-bit length "fingerprint information" or "message" Summary" value to represent this input value, and the converted value as the result. The MD5 algorithm is primarily designed for digital signature applications where larger files are encrypted using a public key in a cryptographic system such as RSA. (done by setting a private key) before compressing in a secure manner.

To calculate the MD5 hash of a file, use the md5_file() function.

Syntax

md5(string,raw)
Copy after login

Parameters

string Required. Specifies the string to be calculated.

raw

Optional. Specifies hexadecimal or binary output format:

TRUE - raw 16-character binary format

FALSE - default. 32-character hexadecimal number

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What functions does php md5() achieve?. For more information, please follow other related articles on the PHP Chinese website!

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