Java教程

Java Base64编码和解码

Java提供了Base64类来处理加密。您可以使用提供的方法对数据进行加密和解密。您需要在源文件中导入java.util.Base64才能使用其方法。
此类提供了三种不同的编码器和解码器,用于在每个级别加密信息。您可以在以下级别使用这些方法。

基本编码和解码

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

URL和文件名编码和解码

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

MIME

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

Base64的嵌套类

说明
Base64.Decoder 此类通过RFC 4648和RFC 2045中指定的Base64编码方案实现了用于解码字节数据的解码器。
Base64.Encoder 此类实现了一个编码器,用于使用RFC 4648和RFC 2045中指定的Base64编码方案对字节数据进行编码。

Base64方法

方法 说明
public static Base64.Decoder getDecoder() 它将返回Base64.Decoder,该解码器使用基本类型的base64编码方案进行解码。
public static Base64.Encoder getEncoder() 它返回Base64.Encoder,它使用基本类型base64编码方案进行编码。
public static Base64.Decoder getUrlDecoder() 它返回一个Base64.Decoder,该解码器使用URL和Filename安全类型base64编码方案进行解码。
public static Base64.Decoder getMimeDecoder() 它返回一个Base64.Decoder,它使用MIME类型的base64解码方案进行解码。
public static Base64.Encoder getMimeEncoder() 它返回一个Base64.Encoder,它使用MIME类型的base64编码方案进行编码。
public static Base64.Encoder getMimeEncoder(int lineLength,byte [] lineSeparator) 它返回Base64.Encoder,该编码器使用MIME类型base64编码方案进行编码,并具有指定的行长和行分隔符。
public static Base64.Encoder getUrlEncoder() 它返回一个Base64.Encoder,它使用URL和Filename安全类型base64编码方案进行编码。

Base64.Decoder方法

方法 说明
public byte[]decode(byte [] src) 它将使用Base64编码方案解码输入字节数组中的所有字节,并将结果写入新分配的输出字节数组中。返回的字节数组具有所得字节的长度。
public byte[]decode(String src) 它使用Base64编码方案将Base64编码的String解码为新分配的字节数组。
public int decode(byte[] src,byte[] dst) 它使用Base64编码方案对输入字节数组中的所有字节进行解码,并将结果从偏移量0开始写入给定的输出字节数组中。
public ByteBuffer decode(ByteBuffer buffer) 它使用Base64编码方案对输入字节缓冲区中的所有字节进行解码,并将结果写入新分配的ByteBuffer中。
public InputStream wrap(InputStream is) 它返回用于解码Base64编码的字节流的输入流。

Base64.Encoder方法

方法 说明
public byte[] encode(byte[] src) 它使用Base64编码方案将指定字节数组中的所有字节编码为新分配的字节数组。返回的字节数组具有所得字节的长度。
public int encode(byte [] src,byte [] dst) 它使用Base64编码方案对指定字节数组中的所有字节进行编码,并将结果字节从偏移量0开始写入给定的输出字节数组。
public String encodeToString(byte [] src) 它使用Base64编码方案将指定的字节数组编码为String。
public ByteBuffer encode(ByteBuffer buffer) 它使用Base64编码方案将指定字节缓冲区中的所有剩余字节编码为新分配的ByteBuffer。返回时,源缓冲区的位置将更新到其极限;其限制将不会更改。返回的输出缓冲区的位置将为零,其限制将为生成的编码字节数。
public OutputStream wrap(OutputStream os) 它包装输出流以使用Base64编码方案对字节数据进行编码。
public Base64.Encoder withoutPadding() 它返回一个编码器实例,该实例与该实例等效,但是在编码的字节数据的末尾没有添加任何填充字符。

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("lidihuo".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: lidihuo

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.lidihuo.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.lidihuo.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
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4