Base64 is a character represented by 64 arbitrary binary data.
# Base64 encoding table (standard)
I believe we all know the ASCII code, from AZ, az, 0-9, and some other special characters, theseCharacter has only a number to indicate。
Similarly Base64 have such a code.
Range "AZ", "az", "0-9", "+", "/" a total of 64 characters.
We give a look at the table,This is simpler than ASCII encoding and more, only 64。
Because the index is starting from 0, so the final index is 63.
use
-
Base64 encoding is suitable for small pieces of content, such as digital signature certificate, Cookie content and so on.
-
Transfer pictures on the web:
coded picture, the characters will+
and/
were changed-
and_
so64 characters are not the escape character. (Custom code set)
so that we can convert binary picture base64-encoded format, in html text.
# Coding process
Here, take a look at how the binary is converted to base64.
- Processing of binary data, each set of 3 bytes (
b1
b2
b2
), a total of 3x8 = 24bit, classified as group 4 (n1
n2
n3
n4
), each exactly 6 bit:
we know,6bit magnitude binary number is converted back to decimal range of 0 to 64
In this way, we can put each 6bit base64-encoded binary number and correspondence table up!
Process flow #
- All characters into ASCII code;
as: A ⇒ 65 - The ASCII code is converted to 8-bit binary;
eg: 65 ⇒0100 0001
- The normalized into a binary three groups (three in the back fill less than 0) of 24 bits and then split into 4 groups of 6;
as:0100 0001
⇒0100 0001 0000 0000 0000 0000
(Two complement of eight 0)
⇒010000
、010000
、000000
、000000
- Unified two 6-bit binary complement front gather 8 0;
such010000
as:010000
,000000
, ,000000
⇒00010000
,00010000
,00000000
,00000000
- After complement binary 0 to decimal; eg:
, , , ⇒ 16, 16, 0,000010000
00010000
00000000
00000000
- Base64 encoding acquired from the corresponding decimal Base64 encoding table;
such as: 16, 16, 0,0
⇒ Q, Q, 0,0
detail:
- Conversion when the three byte of data into the buffer has a 24bit, the first to occupy the high byte.
- Insufficient data 3byte words, in the buffer remaining bit with zeros. Then, in each case taking 6 'bit, in accordance with the selected look-up table to select the value corresponding to the character as the encoded output.
- Continues until all of the input data conversion is complete.
- If the last remaining two data inputs, plus a "=" After the encoding result;
- If the last remaining one input data, the encoded result plus 2 "=";
- If you do not rest any data, we do not add anything, so we can ensure the correctness of data reduction.
Because the two front up of eight 0
Therefore, the final output:QQ==
# Description
-
Base64-encoded binary data encoding would 4 bytes 3 bytes of text data, 33% increase in length, the benefits of the encoded text data can be displayed directly in the message body, web pages.
-
Base64 is a coding method by the look-up table, the encryption can not be used, even if the custom code table too.