What is base-64 encoding?

Source: Internet
Author: User

You should be a senior online reader, and the younger brother is brave enough to ask, when surfing the internet, in addition to soaking the MM, bumping the Forum, throwing bricks ...... In addition, what activities are most carried out? By the way, you must say: It's sending and receiving emails! (Who dare say they have confiscated/sent an email? Pulled out and shot !!)

There is a security issue when sending/receiving e-mail-Imagine that you spent a whole day writing a love letter to the department, he was intercepted by Michael Zhang from the dormitory next door during the sending process (is he a hacker ??), What's worse, he's your rival ...... Day, the consequences are unimaginable !! Therefore, we must have a relatively reliable encryption method to convert the plaintext of an email. At least we need something that cannot be seen at a Glance by others, the encoding/decoding speed is also fast enough. (Now, you can imagine another story. Michael's guy intercepted your love letter, but he saw it: "Why? How is it messy? Spam !!" -- In this way, you will not be able to escape the difficulties ?!)

Base64 is the encryption method generated in this context. It features: 1. The speed is very fast. 2. You can convert string a to string B. If you look at string B, you cannot guess the content of string. Believe it? Let's take a look at the following string:

Xoo6w6osu7btrbniwdna z8letctnzbfxzoy12koh

What is it? Have you guessed it? In fact, it is the following text produced by base64 encoding:

Hello! Welcome to the colorful world of Lao Luo!

The introduction is complete. Let's start to explore things.

Base64 is one of the most common encoding methods used to transmit 8-bit code on the network. For details, refer to rfc2045 ~ Rfc2049, which has the mime detailed specification.

Base64 requires that each three 8-bit bytes be converted into four 6-bit bytes (3*8 = 4*6 = 24), and then 6-bit bytes be added with two more high 0 values, it consists of four 8-bit bytes. That is to say, the converted string is theoretically 1/3 longer than the original one.

Will it be too abstract? Not afraid. Let's look at an example:

Before Conversion Aaaaaabb Ccccdddd Eeffffff  
After conversion 00 aaaaaa 00 bbcccc 00 ddddee 00 ffffff

Should it be clear? The above three bytes are the original text, and the following four bytes are base64 encoded after conversion, and the first two are both 0.

After conversion, we use a code table to obtain the desired string (that is, the final base64 encoding). This table is as follows: (from rfc2045)

Table 1: The base64 alphabet

Value encoding value Encoding
0 A 17 R 34 I 51 Z
1 B 18 S 35 J 52 0
2 C 19 t 36 K 53 1
3 D 20 u 37 L 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6g 23x40 o 57 5
7 H 24 y 41 P 58 6
8 I 25 Z 42 Q 59 7
9 J 26 A 43 R 60 8
10 K 27 B 44 s 61 9
11 l 28 C 45 t 62 +
12 m 29 D 46 U 63/
13 N 30 E 47 v
14 O 31 F 48 W (PAD) =
15 p 32G 49 x
16 Q 33 H 50 y

Let's take a look at the actual example to help you better understand it!

Before Conversion 10101101 10111010 01110110  
After conversion 00101011 00011011 00101001 00110110
Decimal 43 27 41 54
Values in the corresponding code table R B P 2

Therefore, the base64 value of the preceding 24-bit encoding is rbp2.
Similarly, the original code is obtained by reorganizing the binary join of rbq2 to get three 8-bit values.
(Decoding is only the inverse process of encoding. I will not talk about it here. In addition, there are still many RFC related to mime. If you need details, please search for it yourself .)

The coding process is similar to that of programming:

The first character shifts two places to the right to obtain the location of the base64 table of the first target character. Based on this value, the corresponding character on the table is the first target character.
Then, move the first character four places to the left and the second character four places to the right to obtain the second target character.
Then, remove the second character from the left to the second and the third character to the right to get the third target character.
Finally, take the right 6 digits of the third character to obtain the fourth target character.

After each of the preceding steps, perform the and operation on the result and 0x3f to get the encoded characters.

So easy! That's all !!!

But wait ...... If you are smart, you may ask that the number of bytes in the original text should be a multiple of 3. What if this condition cannot be met?

The solution is as follows: the bytes in the original text can be supplemented with 0, and the base64 encoding is replaced with the = number during conversion. This is why some base64 encoding ends with one or two equal signs, but the equal signs can only be two at most. Because:

Remainder = number of original bytes mod 3

Therefore, the remainder can only be one of the three numbers 0, 1, and 2 in any case. If the remainder is 0, it indicates that the number of original bytes is exactly a multiple of 3 (ideally ). If it is 1, in order to make base64 encoding a multiple of 4, we need to add two equal signs. Similarly, if it is 2, we need to add one equal sign.

At this point, should everyone be clear? If you still have any questions, go back and take a closer look. It is not hard to understand.

The following is a program that demonstrates base64 encoding/decoding, hoping to be useful to you. At the same time, I hope you can help me improve it and use it for more purposes. Don't forget to let me know! (I am too busy now)

P.s. Why is it called base64:
Base64 this encoding method enables smooth transmission of Chinese text or images over the network. The base64 encoded string contains only English letters (uppercase and lowercase), Arabic numerals, plus signs, and backslash (backslash). It contains 64 basic characters and does not contain other special characters. Therefore, it is named base64. The encoded string is about 1/3 longer than the original string length.

Http://www.codeproject.com/KB/GDI-plus/image-base-64-converter.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.