1. Project background and introduction
In daily development, Base64 encoding is a common data processing method and is widely used in the following scenarios:
- Data transmission: Convert binary data to ASCII strings for easy transmission in text protocols (such as JSON, XML).
- Data storage: Avoid data parsing errors caused by special characters, such as storing binary data in URLs or cookies.
- Encryption Assistance: Used for encoding keys, certificates, public and private keys, etc., butBase64 is not an encryption algorithm itself, it's just the way to encode it.
This project will show how to use Java built-inBase64
The class performs Base64 encoding (encryption) and decode (decryption).
2. Related knowledge
2.1 Base64 Introduction
Base64 is a encoding method based on 64 printable characters, which can transformConvert any binary data to ASCII string, avoid encoding problems during transmission or storage.
Base64 uses 64 characters as follows (lower and lowercase letters + numbers + two symbols):
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789
+/
Additionally, Base64 may be filled at the end=
, to make its length multiple of 4.
2.2 Java Base64 Class
Starting with Java 8,.Base64
Three Base64 encoders are provided:
Encoder | illustrate |
---|---|
() |
Standard Base64 encoding |
() |
URL-safe Base64 encoding (+ Replace with- ,/ Replace with_ ) |
() |
MIME encoding (line wrap per 76 characters) |
Accordingly, the decoder()
The Base64 string can be converted back to the original data.
3. Project implementation ideas
-
Encoding (encryption):
- pass
().encodeToString(byte[])
Method, convert the string to Base64 encoding format.
- pass
-
Decode (decryption):
- pass
().decode(String)
Method, convert Base64 encoded data back to the original string.
- pass
-
Implement different Base64 variants:
- Standard Base64: Used for regular data encoding.
-
URL Base64: Suitable for URL transmission, avoiding
+
and/
Affect URL parsing. - MIME Base64: Suitable for email or other scenarios where line breaks are required.
4. Complete code implementation
The following Java code implements Base64 encoding and decoding functions, and supports three modes (standard, URL security, MIME).
import .Base64; /** * Base64EncoderDecoder class is used to demonstrate the Base64 encoding and decoding capabilities provided by Java 8. * This class supports three encoding modes: standard Base64, URL security Base64, and MIME Base64. */ public class Base64EncoderDecoder { /** * Use standard Base64 encoding strings * * @param input Original string * @return Base64 Encoded string */ public static String encodeBase64(String input) { return ().encodeToString(()); } /** * Use standard Base64 to decode strings * * @param base64Str Base64 Encoded String * @return The decoded original string */ public static String decodeBase64(String base64Str) { byte[] decodedBytes = ().decode(base64Str); return new String(decodedBytes); } /** * Use URL-safe Base64 encoded strings * * @param input Original string * @return URL Safe Base64 encoded string */ public static String encodeBase64Url(String input) { return ().encodeToString(()); } /** * Use URL-safe Base64 to decode strings * * @param base64UrlStr URL Secure Base64 encoded string * @return The decoded original string */ public static String decodeBase64Url(String base64UrlStr) { byte[] decodedBytes = ().decode(base64UrlStr); return new String(decodedBytes); } /** * Encoded strings using MIME Base64 (for mail and new text) * * @param input Original string * @return MIME Base64 encoded string */ public static String encodeBase64Mime(String input) { return ().encodeToString(()); } /** * Main method, test Base64 encoding and decoding * * @param args Command line parameters */ public static void main(String[] args) { String originalString = "Hello, Base64!"; // Standard Base64 encoding/decoding String encoded = encodeBase64(originalString); String decoded = decodeBase64(encoded); ("Raw String: " + originalString); ("Base64 Encoding: " + encoded); ("Base64 Decoding: " + decoded); // URL Base64 encoding/decoding String urlEncoded = encodeBase64Url(originalString); String urlDecoded = decodeBase64Url(urlEncoded); ("\nURL Security Base64 Encoding: " + urlEncoded); ("URL Security Base64 Decoding: " + urlDecoded); // MIME Base64 encoding (used for line-break text such as email) String mimeEncoded = encodeBase64Mime(originalString); ("\nMIME Base64 Encoding: " + mimeEncoded); } }
5. Code interpretation
5.1 Standard Base64 encoding
- use
().encodeToString(byte[])
Convert strings to Base64 encoding format. - Used when decoding
().decode(String)
, convert back to the original string.
5.2 URL Security Base64 Encoding
-
()
The generated Base64 string is avoided+
and/
, suitable for URL transfer. -
()
Detect.
5.3 MIME Base64 encoding
-
()
Applicable to scenarios where line breaks are required (such as emails, long text). - because
MIME
The form of Base64 may contain line breaks, so you need to pay attention to dealing with line breaks when decoding.
6. Project Summary and Expansion
6.1 Gains from this project
- Master Base64 basic principles: Understand how Base64 converts binary data into printable characters and its application scenarios.
-
Familiar with Java Base64 API: Learn to use
Base64
Classes are standard, URL-security and MIME encoding/decoding. - Distinguish between encoding and encryption: Base64 is only a encoding method and does not have encryption capabilities. It should be used in combination with encryption algorithms (such as AES + Base64).
6.2 Possible expansion directions
- Combined with AES and RSA encryption: Base64 can be used to store and transfer encrypted data.
- Combined with JWT certification: JWT's payload is encoded in Base64.
- Applied to HTTP authentication: Basic Auth uses Base64 to transfer username and password (but is not secure and should be combined with HTTPS).
Through the implementation of this project, readers can not only master the basic methods of Base64 encoding/decoding, but also better understand its practical application in information transmission and data storage. Hope this article will help you understand Base64 technology in depth and apply it flexibly in your projects.
Summarize
This is the end of this article about BASE64 encryption/decryption in java. For more related java BASE64 encryption/decryption content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!