Principle and common implementation of Base64 encoding

This article is mainly for the purpose of introducing the encryption algorithm and laying the groundwork.
This is the foundation, what is the cultivation of a programmer, these foundations are cultivation.
It may not be used at ordinary times, but it must be understood.
If you can't even explain this principle, don't play the King of Glory and Jedi survival League of Legends and the Peace Elite, it is time to make up.

Base64 encoding definition

Base64 encoding is an encoding method based on 64 characters.
All binary data can be represented by these 64 codes.
How to encode, what are the 64 characters, is based on a specification: RFC2045

Base64 encoding features

Base64 encoding is reversible.
The encoded result is not readable.
Wide applicability, any data that can be converted into binary numbers can be Base64 encoded.

Base64 encoding principle

Base64 encoding input parameter is a binary number.
First, there is a coding comparison table, with a total of 64 indexes from 0 to 63, corresponding to (AZ) plus (az) plus (0-9) plus (+) plus (/) exactly 64 characters .
Insert picture description here
The encoding has the following steps:
1. Split the data into several groups, each group of 3 bytes, that is, 24 bits.
2. Each group of data is reorganized with 24 bits and divided into 4 parts, then each part is 6 bits.
3. Fill the high bits of each part with two 0s, then each part becomes 8 bits, that is, one byte. At this time, this group is a total of 4 bytes.
4. Change the value of each byte to decimal, because the upper two bits are 0, so the decimal number of the byte is 63, the minimum is 0, which is exactly the index pair of the 64 encoding tables above Up, and then get the corresponding character. Together, it is the final encoded value.

If the total number of bytes of data is not a multiple of 3, then there is less than 3 bytes in the last group. In this case, continue to compile according to the above coding rules (bytes are replaced by bits, and every 6 high bits are filled with two 0s to become a byte)
Case 1: The
last group has only two bytes, that is 16 , The
first 6 digits, the upper two digits are filled with two 0s, and it becomes a byte, and the comparison table gets the corresponding first character; the
middle 6 digits, the higher digits are filled with two 0s, and becomes a byte, the comparison table gets the corresponding The second character; the
last 4 digits, the upper 4 digits are filled with 4 0s, and it becomes a byte. The corresponding third character is obtained in the comparison table; the
fourth character is replaced by = by default.
Case 2
There is only one byte in the last group, that is, 8 bits, the
first 6 bits, the high bits are filled with two 0s, and become a byte, and the comparison table gets the corresponding first character; the
last 2 bits, the high bits are filled with 6 0, becomes a byte, and the corresponding second character is obtained in the comparison table; the
third character and the fourth character are replaced by = by default.

This is why, sometimes the encoding result you see will have = or == at the end, which is not enough.
In addition, every 76 characters plus a newline character, this is a regulation.

Decoding is the reverse operation, is it very simple. If you don't consider performance, you can try to write a Base64 encoding tool.

Base64 derivative encoding

Base 16
Base 32
Url Base64

Having seen the principle of Base64, Base16 and Base32 are similar, both are split bytes, and then the high bit is filled with 0, so that the new byte, the maximum is 16 or 32, and then according to the table to find the corresponding character.

The appearance of url Base64 is because the standard Base64, the last two characters + / cannot be used as parameters in the url, so the main change of url Base64 is to replace these two characters with-and _.

Common implementation of Base64 encoding

There are many implementations of Base64, two common: JDK and Apache

JDK Base64
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.IOException;

public class Base64Test {
    public static void main(String[] args) throws IOException {
        String str = "hello word";
        BASE64Encoder base64Encoder = new BASE64Encoder();
        // JDK Base64加密
        String encode = base64Encoder.encode(str.getBytes());
        System.out.println(encode);

        BASE64Decoder base64Decoder = new BASE64Decoder();
        // JDK Base64解密
        byte[] bytes = base64Decoder.decodeBuffer(encode);
        String res = new String(bytes);
        System.out.println(res);
    }
}
Apache Base64

Introduce pom dependencies:

<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
	<groupId>commons-codec</groupId>
	<artifactId>commons-codec</artifactId>
	<version>1.14</version>
</dependency>
import org.apache.commons.codec.binary.Base64;

public class ApacheBase64Test {
    public static void main(String[] args) {
        String str = "hello word";
        // Apache Base64加密
        byte[] encodeBytes = Base64.encodeBase64(str.getBytes());
        System.out.println(new String(encodeBytes));

        // Apache Base64解密
        byte[] decodeBytes = Base64.decodeBase64(encodeBytes);
        System.out.println(new String(decodeBytes));
    }
}

Of course, the results of the above two are:

aGVsbG8gd29yZA==
hello word
Published 203 original articles · praised 186 · 210,000 views

Guess you like

Origin blog.csdn.net/java_zhangshuai/article/details/105548280