Home Backend Development PHP Problem How to solve the 16-bit character garbled problem of PHP md5 function

How to solve the 16-bit character garbled problem of PHP md5 function

Aug 18, 2020 am 09:22 AM

php md5 16-bit character garbled solution: 1. Convert the output 16-byte binary into hexadecimal; 2. Use "substr(md5($str),8,16)" Method to obtain the 16-character md5 ciphertext.

How to solve the 16-bit character garbled problem of PHP md5 function

Recommended: "PHP Video Tutorial"

garbled characters

PHP's md5 function is used to perform md5 operations on string parameters. This function has two parameters:

md5 ( string $str [, bool $raw_output = FALSE ] ) : string

The first parameter is the input string; the second parameter defaults to FALSE. When set to TRUE, a 16-bit md5 value can be output.

By default, md5(string $str) will return: a 32-character, hexadecimal hash value.
If you add the second parameter md5(string $str,TRUE), it will return: a hash value in original binary format with a length of 16 bytes.

From this we can see that when a binary format of 16 bytes length (corresponding to 16 characters, because it conforms to ASCII) is returned, since the browser characterizes it, it Garbled characters will be produced:

$str = "PHP";
echo "字符串:".$str."<br>";
echo "TRUE - 原始 16 字符二进制格式:".md5($str,TRUE)."<br>";
echo "FALSE - 32 字符十六进制格式:".md5($str)."<br>";
Copy after login

How to solve the 16-bit character garbled problem of PHP md5 function

Solution

So how to get a 16-bit md5 value that is not garbled? There are two methods:

  1. Convert the output 16-byte binary into hexadecimal;
  2. In the ciphertext of md5, the difference between 16-bit ciphertext and 32-bit ciphertext The 8th to 24th substrings are the same, so we can obtain the 16-character md5 ciphertext by intercepting: substr(md5($str),8,16). The second parameter and the third parameter represent starting from the 8th character (the subscript starts from 0), taking 16 characters.

Here we use the second method to solve the problem of garbled characters. Still using the above example:

$str = "PHP";
echo "字符串:".$str."<br>";
echo "TRUE - 原始 16 字符二进制格式(乱码):".md5($str,TRUE)."<br>";
echo "TRUE - 原始 16 字符二进制格式(不乱码):".substr(md5($str),8,16)."<br>";
echo "FALSE - 32 字符十六进制格式:".md5($str)."<br>";
Copy after login

How to solve the 16-bit character garbled problem of PHP md5 function

Note: If you need an uppercase md5 value, just use the strtoupper(...) function directly.

The above is the detailed content of How to solve the 16-bit character garbled problem of PHP md5 function. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24