The encryption methods in PHP are as follows
1. MD5 encryption
string md5 ( string $str [, bool $raw_output = false ] )
Parameters
str -- Original string.
raw_output -- If the optional raw_output is set to TRUE, the MD5 message digest will be returned in raw binary format with a length of 16 bytes.
This is an irreversible encryption, execute the following code
$password = '123456';
echo md5($password);
The result is e10adc3949ba59abbe56e057f20f883e
2. Crype encryption
string crypt ( string $str [, string $salt ] )
crypt() Returns a hashed string based on the standard UNIX DES algorithm or another alternative algorithm available on the system.
Parameters
str -- The string to be hashed.
salt -- Optional salt value string. If not provided, algorithm behavior will be determined by different algorithm implementations and may lead to unpredictable endings.
This is also an irreversible encryption, execute the following code
The result is teMGKvBPcptKo
An example of using automatic salt value is as follows:
The execution result is the output Password verified!
Examples of using crypt() with different hash types are as follows:
The results are as follows
Standard DES: rl.3StKT.4T8M
Extended DES: _J9..rasmBYk8r9AiWNc
MD5:
Blowfish: $2a$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi
SHA-256: $5$rounds=5000$usesomesillystri$KqJWpanXZHKq2BOB43TSaYhEWsQ1Lr5QNyPCDH/Tp.6
SHA-512: $6$rounds=5000$usesomesillystri$D4IrlXatmP7rx3P3InaxBeoomnAihCKRVQP22JZ6EY47Wc6BkroIuUUBOov1i.S5KPgErtP/EN5mcO.ChWQW21
On systems where the crypt() function supports multi-hashing, the following constants are set to 0 or 1 depending on whether the corresponding type is available:
CRYPT_EXT_DES - Extended hashing based on the DES algorithm. The salt is a 9-character string consisting of an underscore followed by the 4-byte cycle count and the 4-byte salt. They are encoded into printable characters, 6 bits each, with the least significant bits first. 0 to 63 are encoded as "./0-9A-Za-z". Using illegal characters in the salt will cause crypt() to fail.
CRYPT_MD5 - MD5 hashing uses a 12-character string salt starting with $1$.
CRYPT_BLOWFISH - The Blowfish algorithm uses the following salt: "$2a$", a two-digit cost parameter, "$", and a 64-bit string consisting of characters from "./0-9A-Za-z". Using characters outside this range in the salt will cause crypt() to return an empty string. The two-digit cost parameter is the base-2 logarithm of the number of cycles. Its range is 04-31. Exceeding this range will cause crypt() to fail.
CRYPT_SHA256 - The SHA-256 algorithm hashes using a 16-character string salt starting with $5$. If the salt string begins with "rounds=
CRYPT_SHA512 - The SHA-512 algorithm hashes using a 16-character string salt starting with $6$. If the salt string begins with "rounds=
3. Sha1 encryption
Parameters
str -- Input string.
raw_output -- If the optional raw_output parameter is set to TRUE, the sha1 digest will be returned in raw format with a length of 20 characters, otherwise the return value is a 40-character hexadecimal number.
This is also an irreversible encryption, execute the following code:
$password = '123456';
echo sha1($password);
The result obtained is 7c4a8d09ca3762af61e59520943dc26494f8941b
http://www.cmd5.com/
Do you think it is useless even if you add encryption? In fact, it is not the case. As long as your encryption is complex enough, the possibility of being cracked is smaller. For example, you can use a mixture of the above three encryption methods to encrypt. I will recommend to everyone a php encryption library.
4. URL encryption
string urlencode ( string $str )This function facilitates encoding a string and using it in the request part of the URL, and it also facilitates passing variables to the next page.
Returns a string in which all non-alphanumeric characters except -_. will be replaced with a percent sign (%) followed by two hexadecimal digits, and spaces are encoded as plus signs ( +). This encoding is the same as the encoding of WWW form POST data, and the same encoding as the application/x-www-form-urlencoded media type. For historical reasons, this encoding differs from the RFC1738 encoding in encoding spaces as plus signs (+).
string urldecode ( string $str )
Decode any %## in the given encoded string. The plus sign ('+') is decoded into a space character.
This is a reversible encryption. The urlencode method is used for encryption and the urldecode method is used for decryption. Execute the following code:
$url = 'http://www.xxx.com/CraryPrimitiveMan/';
$encodeUrl = urlencode($url);
echo $encodeUrl . "n";// If it is displayed on a web page, change n to
echo urldecode($encodeUrl);
The results obtained are as follows
http://www.xxx.com/CraryPrimitiveMan/
The method of encrypting URLs based on RFC 3986 is as follows:
5. Base64 information encoding and encryption
string base64_encode ( string $data )
Encode data using base64.
This encoding is designed to enable binary data to be transmitted over non-pure 8-bit transport layers, such as the body of an email.
Base64-encoded data takes up about 33% more space than the original data.
string base64_decode ( string $data [, bool $strict = false ] )
Decode base64 encoded data.
Parameters
data -- encoded data.
strict -- Returns FALSE if the input data exceeds the base64 alphabet.
Execute the following code:
The results are as follows
Recommend phpass
Tested with phpass 0.3, the standard way to protect user passwords by hashing them before storing them in the database. Many commonly used hashing algorithms such as md5 and even sha1 are not secure for password storage because hackers can easily crack passwords using those algorithms.
The most secure way to hash passwords is to use the bcrypt algorithm. The open source phpass library provides this functionality in an easy-to-use class.
The above is the introduction of this article about PHP encryption method. I hope you will like it.