Like attention, no more lost, your support means a lot to me!
🔥 Hi, I’m Chouchou. GitHub · Android-Notebook has been included in this article. Welcome to grow up with Chouchou Peng. (Contact information at GitHub)
preface
- Those of you who are familiar with network communication have heard of it
Base64
Encoding. For example, let’s say a piece of data passesMD5 、SHA
And so on means after encryption, afterBase64
Encoded as a string, it can be easily transmitted over the network. thenBase64
Is that some kind of encryption algorithm? - In this article, I will lead you to understand
Base64
The basic principle of implementation, I hope to help
series
- The cryptography | truth! What do you think is the Base64 encryption algorithm?”
- Say the cryptography | ready! What is the hash algorithm?”
- The cryptography | sense!, signature and digital certificates are?”
Related articles
- “Computer network | graphic DNS & HTTPDNS principle”
- The Android | mountain, can offend jade! Read an article/v1 / v2 v3 signature mechanism”
directory
1. Fundamentals
Base64 is an encoding method for representing binary streams as 64 characters. Standard Base64 uses the following index tables:
For example, the string “Base64 encoding “is encoded as QmFzZTY0IOe8lueggQ==. Of course, this implies that utF-8 is used as the premise of character encoding. If other character encoding methods are used, Base64 encoding will not be the same result. Many online codec sites actually use UTF-8 by default, but it is not specified.
1.1 Standard Base64 encoding procedures
The following explains the Base64 encoding steps:
- Step 1: Data entry
In this step, you need to convert the raw data (strings, images, audio, whatever) into a binary stream. The string example, for example, needs to be character encoded into a binary stream.
-
Step 2: Group conversion
- Starting from the header of the binary stream, each group of 6 bits is formed. If there are less than 6 bits, 0 is added in the lower part
- Every six bits form a new byte, with the highest two bits complementation zeros, at which point the binary has been obtained
Base64
coding
-
Step 3: Convert to a string
Each byte of the binary Base64 encoding is mapped to A character, such as 0000 0000 to A and 0011 1111 to /, at which point the Base64 encoding string is obtained
- Step 4: End fill
The standard Base64 encoded string is a multiple of 4 in length, otherwise = is appended at the end. For example, the previous QmFzZTY0IOe8lueggQ== length is 20 after adding two =.
The whole coding step is not complicated, and we use a schematic diagram to express it as:
1.2 Non-standard Base64
- Url Base 64
Standard Base64 uses ‘/’, which conflicts with urls and file systems, so the URL Base64 algorithm extends to replace the ‘+’ and ‘/’ symbols with the ‘-‘ and ‘_’ symbols.
- MIME Base 64
This is a MIME-friendly format that outputs 76 characters per line, with a carriage return newline \r\n appended at the end of each line, and a carriage return newline whether or not each line is 76 characters long
1.3 significance
Base64 can convert any data into a portable string, avoiding distortion during transmission. Base64 was originally intended to solve the problem of not being able to use non-ASCII characters directly in E-mail. After a piece of data is encoded into ASCII string through Base64, it can be restored to the original data through Base64 decoding at the receiving end without worrying about distortion during transmission.
Most of the time, we use Base64 encoding as the transmission/storage format for encrypted data. For example, after a piece of plaintext data is encrypted by MEANS of MD5 and SHA, and encoded into a string by Base64, it can be easily transmitted and stored. For another example, the digital certificate on the Internet is actually transmitted in the form of Base64 encoding. We can check the digital certificate on baidu’s official website on the browser:
- 1. Check the digital certificate on baidu’s official website
- 2. Save the certificate to the local PC
- 3. The certificate is stored in Base64 encoding format
It should be noted that Base64 is not an encryption method and plaintext can be directly restored to plaintext using Base64 encoded strings through index tables. Therefore, Base64 can only be used as a data storage format.
2. Algorithm implementation
2.1 Java environment
Prior to Java 8, there was no implementation of the Base64 algorithm in the JDK, which is actually quite surprising. Although sun.misc.BASE64Encoder is available in the source code, it is not actually a public API, but is used internally by the Sun team and is best avoided in production. Since Java 8, the JDK has finally supplemented the Base64 implementation, for example:
import java.util.Base64; Standard Base64 system.out.println (base64.getencoder ().encodeToString("".getBytes())); Url Base 64 System.out.println(Base64.getUrlEncoder().encodeToString("".getBytes())); MIME Base 64 System.out.println(Base64.getMimeEncoder().encodeToString("".getBytes()));Copy the code
Bouncy Castle and Apache also provided algorithmic implementations of Base64 prior to Java 8.
2.2 the Android environment
The Android SDK provides algorithmic implementations of Base64, such as:
import android.util.Base64;
System.out.println(Base64.encodeToString("".getBytes(),Base64.DEFAULT));
Copy the code
Compared with the algorithm implementation of Java 8, the API provided by Android is more flexible, and you can customize the output of the algorithm through Flag.
3. Summary
Base 64
Can convert any data into a portable string, avoiding distortion problems during transmission.- It’s important to note that,
Base 64
It’s not a way to encrypt, it’s just a way to encode. A lot of times, we’re going toBase64
Encoding the transmission/storage format of encrypted data
The resources
- The Art of Java Encryption and Decryption (Chapter 5). By Dong Liang
- The Definitive GUIDE to HTTP. David Gourley, Brian Totty, et al
- Base64 — Wikipedia
Recommended reading
- Java | show you understand the ServiceLoader principle and design idea
- Java | please outline the structure of a Class file
- Java chat | compilation process (compile front-end & compile the back-end)
- Java | why Java implementation platform neutrality?
- How many letters are there in the Android | a process Context object is not much (right)
- Android | show you understand NativeAllocationRegistry principle and design idea
- Android | article bring you a comprehensive understanding of AspectJ framework
- Computer composition principle | Unicode utf-8 and what is the relationship?
- | why floating-point arithmetic of computer constitute principle is not accurate? (Ali written test)