1.1 base64
Encoding Principle
1.1.1
Summary
:
Base64 is a common encoding method in communication transmission.
(Note the encoding algorithm, not the encryption algorithm)
A common problem occurs during parameter transmission: Garbled characters are not involved in the use of English letters, however, if other languages are involved, garbled characters may occur, and the characters transmitted over the network are not all printable characters, such as videos and images.
Base64 is used to solve this problem. It represents binary data based on 64 printable characters.
When an email appears, it can only be transmitted in English. However, as the number of users increases, the number of Chinese and Japanese characters increases, but the characters cannot be effectively processed by the server or gateway, therefore, base64 is displayed. Base64 is also used for URL, cookie, and webpage transmission of a small amount of binary files.
1.1.2
Encoding Principle
:
We all know that computer data is stored in bytes, and a byte is composed of eight bits. Each bit is the smallest unit of information.
So1 byte = 8bit
(In this article, byte is used as the unit of measurement for computing storage capacity.
ByteIt is also an unsigned data type. The value ranges from 0 to 255 and cannot be negative. But it is not covered in the scope of this article .)
Step 1: divide each three bytes of the string to be converted into one group. Each byte occupies 8 bits, and there are 24 binary digits in total.
Step 2: divide each of the preceding 24 binary bits into six groups.
Step 3: Add two zeros in front of each group. Each group is changed from 6 to 8 binary bits. A total of 32 binary bits are four bytes.
Step 4: obtain the corresponding value based on the base64 encoding table.
For example:
1.1.3
Program functions
Programming
Python built-in base64 can be directly used for base64 encoding and decoding.
Related functions are also available in PHP.
base64_encode()base64_decode(string data)
Java contains the org. Apache. commons. codec. Binary. base64 class.
Encoding principle _ base64 encoding Principle