Overview

Base64 Encoding Explanation

Base64 is a method of representing binary data using 64 printable characters. Since 2^6 = 64, each 6 bits is a unit corresponding to a printable character.

Base64 is often used in situations where text data is typically processed to represent, transmit, and store binary data, including MIME emails and complex XML data.

Base64 encoding requires converting three 8-bit bytes (3*8=24) into four 6-bit bytes (4*6=24), then padding each 6-bit byte with two leading zeros to form an 8-bit byte. If the remaining characters are less than three bytes, they are padded with zeros. The output character is " = ", so the encoded text may end with one or two "=" characters.

To ensure that the output encoded bits are readable characters, Base64 uses an encoding table for unified conversion. The size of the encoding table is 2^6 = 64 , hence the name Base64 .

Printable characters in Base64 include the letters AZ and az , and the numbers 0-9 , for a total of 62 characters. In addition, two printable symbols vary between different systems.

The following are the basic steps of Base64 encoding:

  • Divide the data into groups of 3 bytes (24 bits).
  • Each byte is converted to an 8-bit binary form.
  • The 24-bit data is divided into groups of 6 bits each, resulting in 4 groups of 6 bits each.
  • Convert each 6-bit group to its corresponding Base64 character.
  • If the data is less than 3 bytes, padding is performed.
  • All the converted Base64 characters are concatenated to form the final encoded result.

Decoding Base64 encoding is the reverse of encoding. Each Base64 character is converted into a corresponding 6-bit binary value, and then these 6-bit values ​​are combined into the original binary data.

Base64 encoding has the following characteristics:

  • The length of the encoded data is always about 1/3 longer than the original data.
  • The encoded data can contain any combination of AZ, az, 0-9, and two additional characters.
  • Base64 encoding is a reversible encoding method, which can restore the original data by decoding.

Base64 encoding table

code valuecharactercode valuecharactercode valuecharactercode valuecharacter
0A16Q32g48w
1B17R33h49x
2C18S34i50y
3D19T35j51z
4E20U36k520
5F21V37l531
6G22W38m542
7H23X39n553
8I24Y40o564
9J25Z41p575
10K26a42q586
11L27b43r597
12M28c44s608
13N29d45t619
14O30e46u62+
15P31f47v63/