You get to know one article Base64, simple!

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

Here Insert Picture Description

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:
    Here Insert Picture Description

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 #

  1. All characters into ASCII code;
    as: A ⇒ 65
  2. The ASCII code is converted to 8-bit binary;
    eg: 65 ⇒0100 0001
  3. 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 00010100 0001 0000 0000 0000 0000(Two complement of eight 0
    010000010000000000000000
  4. Unified two 6-bit binary complement front gather 8 0;
    such 010000as: 010000, 000000, , 000000
    00010000, 00010000, 00000000,00000000
  5. After complement binary 0 to decimal; eg:
    , , , ⇒ 16, 16, 0,000010000000100000000000000000000
  6. Base64 encoding acquired from the corresponding decimal Base64 encoding table;
    such as: 16, 16, 0,0
    ⇒ Q, Q, 0,0

detail:

  1. Conversion when the three byte of data into the buffer has a 24bit, the first to occupy the high byte.
  2. 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.
  3. Continues until all of the input data conversion is complete.
  4. If the last remaining two data inputs, plus a "=" After the encoding result;
  5. If the last remaining one input data, the encoded result plus 2 "=";
  6. 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==

Here Insert Picture Description

# 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.

Published 501 original articles · won praise 112 · views 20000 +

Guess you like

Origin blog.csdn.net/LawssssCat/article/details/104854281