For a bit of research base64 encoding and URIEncode

The role of Base64 encoding

The coded arbitrary binary bit string into a character string by the code ASCii 64 displayable characters.

 

Why base64 encoding?

All files are essentially composed of 0,1 bit string, text files, binary files is that the only difference between how an operating system interpret the file contents. The front end of the most commonly used html, css, js are text files, and text files are all bits of the operating system to interpret as the character encoding (such as UTF-8 encoding in accordance with rules to interpret), so when we want in a text file when data is stored in binary files (such as saving a picture in the css file), you will encounter problems - for example, the operating system will force the binary data originally belonged to the picture as UTF-8 encoded string to decode, then we You get a bunch of unintelligible gibberish on the page, and may even undermine the real text data area.

Of course, this problem is solvable - we can use to show normal text characters to encode binary data, such as text and then save the css file, (such as <img> tag to render graphic content) and then when actually using these data decoded. This is what base64 done.

 

Why yard table has 64 characters?

Because the code is only visible characters ASCii 95, rounded down (n-th power of 2) is 64.

Which specific 64? AZ , AZ , 0-9 , and + / these two symbols . 26 + 26 + 10 + 64 2 exactly.

In addition, there is sometimes a character as a placeholder Base64 code string at the end, i.e., equal sign  =  . An equal sign indicates the end of encoding the complement of the original bit string of 2bit 0. Equal sign may appear only 1 or 2, the following will explain why.

 

Why the base64 encoded file size becomes larger?

64 characters can be represented 6bit data (2 ^ 6 = 64), and a character code to ASCii one byte (1byte = 8bit), that is, in fact, is Base64 encoded with 8 bits in the binary string to denote the 6 bits, the encoded binary string of the original volume was 4/3.

For this reason, the front base64 encoding applies only to small files, because much of the increase in volume, you can also save a network request; but when the file size is relatively large, the site will affect the speed of the initial loading and rendering (decoding base64 It will consume a large file performance), this time the file or put CDN better.

 

Why fill 0 base64 only two cases?

Consider another constraint: the operating system, file system read and write operations, are operated in bytes, and one byte is equal to 8bit, therefore, base64 encoding target, which is the number of bits 8 multiples, and encoding is base64 encoded 6bit each removed therefrom, which may occur at the end of the binary string except where endless - and there are only two cases:

1. After the remaining 1 byte to be coded, removed from 6bit, yet remaining 2bit coding (8--6 = 2), then the need to make 4 0.

2. The remaining 2 bytes to be encoded, then removed from 12bit, yet remaining coding 4bit (8 * 2--6 * 2 = 4), then the need to make 2 0.

When the remaining 3 bytes, corresponds exactly four 6bit, need not 0s.

So we may see 1 or 2 equal sign Base64 string in the tail comes out of this.

 

Similarities and differences with the Base64 encoded URI

The same point : They are used for a given character set to a wider range of data representation.

Difference : URI encoding is beyond the URI for the legal character set (is ASCii can display a subset of character sets, removing unsafe and reserved characters) character to do outside the scope of the code, and to do base64 encoding for binary data - is a text encoding, one encoding binary data.

Two Tips

1. The nature of the text is binary data, it can also be used to do base64 encoding force

2. slashes base64 encoding / = sign and are not valid URI character string base64 encoding so on can not be directly linked with the parameters

 

Guess you like

Origin www.cnblogs.com/leegent/p/11878492.html