📜  Java8 Base64编码和解码

📅  最后修改于: 2020-10-13 03:31:21             🧑  作者: Mango

Java Base64编码和解码

Java提供了一个Base64类来处理加密。您可以使用提供的方法对数据进行加密和解密。您需要在源文件中导入java.util.Base64才能使用其方法。

此类提供了三种不同的编码器和解码器,用于在每个级别上加密信息。您可以在以下级别使用这些方法。

基本编码和解码

它使用Java在RFC 4648和RFC 2045中指定的Base64字母表进行编码和解码操作。编码器不添加任何行分隔字符。解码器拒绝包含base64字母之外的字符的数据。

URL和文件名的编码和解码

它使用Java在RFC 4648中指定的Base64字母表进行编码和解码操作。编码器不添加任何行分隔字符。解码器拒绝包含base64字母之外的字符的数据。

哑剧

它使用RFC 2045中指定的Base64字母表进行编码和解码操作。编码后的输出必须以不超过76个字符的行表示,并使用回车符’\ r’,后跟换行符’\ n’作为行分隔符。没有行分隔符添加到编码的输出的末尾。在base64字母表中找不到的所有行分隔符或其他字符在解码操作中将被忽略。

Base64的嵌套类

Class Description
Base64.Decoder This class implements a decoder for decoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.
Base64.Encoder This class implements an encoder for encoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.

Base64方法

Methods Description
public static Base64.Decoder getDecoder() It returns a Base64.Decoder that decodes using the Basic type base64 encoding scheme.
public static Base64.Encoder getEncoder() It returns a Base64.Encoder that encodes using the Basic type base64 encoding scheme.
public static Base64.Decoder getUrlDecoder() It returns a Base64.Decoder that decodes using the URL and Filename safe type base64 encoding scheme.
public static Base64.Decoder getMimeDecoder() It returns a Base64.Decoder that decodes using the MIME type base64 decoding scheme.
public static Base64.Encoder getMimeEncoder() It Returns a Base64.Encoder that encodes using the MIME type base64 encoding scheme.
public static Base64.Encoder getMimeEncoder(int lineLength, byte[] lineSeparator) It returns a Base64.Encoder that encodes using the MIME type base64 encoding scheme with specified line length and line separators.
public static Base64.Encoder getUrlEncoder() It returns a Base64.Encoder that encodes using the URL and Filename safe type base64 encoding scheme.

Base64.Decoder方法

Methods Description
public byte[] decode(byte[] src) It decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into a newly-allocated output byte array. The returned byte array is of the length of the resulting bytes.
public byte[] decode(String src) It decodes a Base64 encoded String into a newly-allocated byte array using the Base64 encoding scheme.
public int decode(byte[] src, byte[] dst) It decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into the given output byte array, starting at offset 0.
public ByteBuffer decode(ByteBuffer buffer) It decodes all bytes from the input byte buffer using the Base64 encoding scheme, writing the results into a newly-allocated ByteBuffer.
public InputStream wrap(InputStream is) It returns an input stream for decoding Base64 encoded byte stream.

Base64.Encoder方法

Methods Description
public byte[] encode(byte[] src) It encodes all bytes from the specified byte array into a newly-allocated byte array using the Base64 encoding scheme. The returned byte array is of the length of the resulting bytes.
public int encode(byte[] src, byte[] dst) It encodes all bytes from the specified byte array using the Base64 encoding scheme, writing the resulting bytes to the given output byte array, starting at offset 0.
public String encodeToString(byte[] src) It encodes the specified byte array into a String using the Base64 encoding scheme.
public ByteBuffer encode(ByteBuffer buffer) It encodes all remaining bytes from the specified byte buffer into a newly-allocated ByteBuffer using the Base64 encoding scheme. Upon return, the source buffer’s position will be updated to its limit; its limit will not have been changed. The returned output buffer’s position will be zero and its limit will be the number of resulting encoded bytes.
public OutputStream wrap(OutputStream os) It wraps an output stream for encoding byte data using the Base64 encoding scheme.
public Base64.Encoder withoutPadding() It returns an encoder instance that encodes equivalently to this one, but without adding any padding character at the end of the encoded byte data.

Java Base64示例:基本编码和解码

import java.util.Base64;
publicclass Base64BasicEncryptionExample {
publicstaticvoid main(String[] args) {
// Getting encoder
Base64.Encoder encoder = Base64.getEncoder();
// Creating byte array
bytebyteArr[] = {1,2};
// encoding byte array
bytebyteArr2[] = encoder.encode(byteArr);
System.out.println("Encoded byte array: "+byteArr2);
bytebyteArr3[] = newbyte[5];// Make sure it has enough size to store copied bytes
intx = encoder.encode(byteArr,byteArr3);// Returns number of bytes written
System.out.println("Encoded byte array written to another array: "+byteArr3);
System.out.println("Number of bytes written: "+x);

// Encoding string
String str = encoder.encodeToString("JavaTpoint".getBytes());
System.out.println("Encoded string: "+str);
// Getting decoder
Base64.Decoder decoder = Base64.getDecoder();
// Decoding string
String dStr = new String(decoder.decode(str));
System.out.println("Decoded string: "+dStr);
}
}

输出:

Encoded byte array: [B@6bc7c054
Encoded byte array written to another array: [B@232204a1
Number of bytes written: 4
Encoded string: SmF2YVRwb2ludA==
Decoded string: JavaTpoint

Java Base64示例:URL编码和解码

import java.util.Base64;
publicclass Base64BasicEncryptionExample {
publicstaticvoid main(String[] args) {
// Getting encoder
Base64.Encoder encoder = Base64.getUrlEncoder();
// Encoding URL
String eStr = encoder.encodeToString("http://www.javatpoint.com/java-tutorial/".getBytes());
System.out.println("Encoded URL: "+eStr);
// Getting decoder
Base64.Decoder decoder = Base64.getUrlDecoder();
// Decoding URl
String dStr = new String(decoder.decode(eStr));
System.out.println("Decoded URL: "+dStr);
}
}

输出:

Encoded URL: aHR0cDovL3d3dy5qYXZhdHBvaW50LmNvbS9qYXZhLXR1dG9yaWFsLw==
Decoded URL: http://www.javatpoint.com/java-tutorial/

Java Base64示例:MIME编码和解码

package Base64Encryption;
import java.util.Base64;
publicclass Base64BasicEncryptionExample {
publicstaticvoid main(String[] args) {
// Getting MIME encoder
Base64.Encoder encoder = Base64.getMimeEncoder();
String message = "Hello, \nYou are informed regarding your inconsistency of work";
String eStr = encoder.encodeToString(message.getBytes());
System.out.println("Encoded MIME message: "+eStr);

// Getting MIME decoder
Base64.Decoder decoder = Base64.getMimeDecoder();
// Decoding MIME encoded message
String dStr = new String(decoder.decode(eStr));
System.out.println("Decoded message: "+dStr); 
}
}

输出:

Encoded MIME message: SGVsbG8sIApZb3UgYXJlIGluZm9ybWVkIHJlZ2FyZGluZyB5b3VyIGluY29uc2lzdGVuY3kgb2Yg
d29yaw==
Decoded message: Hello, 
You are informed regarding your inconsistency of work