The Base64 encoding rule is a method that uses 64 characters to represent any binary data.
Usually use Notepad to open exe, JPG, PDF of these files, we will see a lot of garbled, because the binary file contains many characters can not be displayed and printed, so if you want to make Notepad such a text processing software processing binary data, you need a binary to string conversion method. Base64 is one of the most common binary encoding methods.
The BASE64 principle is simple:
1. First, prepare an array containing 64 characters
[' A ', ' B ', ' C ', ... ' A ', ' B ', ' C ', ... ' 0 ', ' 1 ', ... ' + ', '/']
2. Then, the binary data is processed, every 3 bytes a group, altogether is 3x8=24bit, is divided into 4 groups, each group exactly 6 bit:
Base64-encode
So we get 4 numbers as index, then look up the table, get the corresponding 4 characters, is the encoded string.
3. Therefore, the BASE64 encoding will encode 3 bytes of binary data into 4 bytes of text data, the length of 33%, the advantage is that the encoded text data can be displayed directly in the message body, Web pages and so on.
4. If the binary data you want to encode is not a multiple of 3, there will be 1 or 2 bytes left in the end. Base64 with \x00 bytes at the end of the top, and then at the end of the code to add 1 or 2 = number, indicating how many bytes were filled, decoding, will be automatically removed.