Base64 encoding

Source: Internet
Author: User

Tags: base64 encoding

Base64. As the name implies, is a method of representing binary data based on 64 printable characters. (It is not an encryption algorithm). For 64 printed characters, we need 6 bits to fully represent it. So how do we use 8 bits to represent printable characters that require only 6 bits to be fully represented? since 2 of the 6 is equal to 64, we can use every 6 bits as one unit, corresponding to a printable character. Three bytes have 24 bits, which correspond to 4 Base64 units, 3 bytes need to be represented by 4 printable characters. It can be used as the transmission encoding for e-mail. printable characters in Base64 include the letter A-Z, A-Z, the number 0-9, which is 62 characters in total, and two printable symbols that differ from one system to another.

Base64 Index Table: (original table)

650) this.width=650; "src=" Http:// "title=" 20151118123911.png "alt=" Wkiol1zmay2qtn6daabm8jt-btu798.png "/>

Coding Process Example:


650) this.width=650; "src=" Http:// "title=" 20151118124326.png "alt=" Wkiom1zmah6wx2b4aaaw9bryhgq515.png "/>

The above three bytes are the original text, the following four bytes are the converted Base64 encoding, the first two bits are 0.

After the conversion, we then get the string we want by converting the binary to printable characters (that is, the final Base64 encoding)

We can see that after Base64 encoding, the string is theoretically 1/3 longer than before, which is the original 4/3. (except for the following two cases).

If the number of bytes to encode cannot be divisible by 3 and the last 1 or 2 bytes, then the following method can be used to process: First use the 0-byte value at the end of the top, so that it is divisible by 3, and then the Base64 encoding. After the encoded base64 text, add one or two ' = ' sign, which represents the number of bytes to be replenished. That is, when the last remaining eight-bit byte (a byte), the last 6-bit base64 byte block has four bits is 0 value, and the last two equals, if the last two bits of eight bytes (2 byte), the last 6 bits of the base byte block has two bits is the 0 value, and finally append an equal sign.

650) this.width=650; "src=" Http:// "title=" 20151118124346.png "alt=" Wkiol1zmaoehnhhwaaaryc3v3vs071.png "/>

Base64 encoding generally has a = number, and the length of the ciphertext is also basically base64 encoding. (The length of the base64 encoding is not fixed) (the equal sign is the result of modifying 64 printable characters depending on the usage).

Application of Base64


This article from "The World is white, hum" blog, please be sure to keep this source

Base64 encoding

Related Article

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: