java byte字节数组 base64加密
时间: 2023-11-17 16:06:20 浏览: 36
Java中可以使用Base64类进行byte数组和字符串之间的相互转换,具体可以参考以下代码:
```java
import java.util.Base64;
public class Base64Example {
public static void main(String[] args) {
// 定义byte数组
byte[] bytes = {0x01, 0x02, 0x03, 0x04, 0x05};
// 将byte数组转换为Base64编码的字符串
String encodedString = Base64.getEncoder().encodeToString(bytes);
System.out.println("Base64编码的字符串:" + encodedString);
// 将Base64编码的字符串转换为byte数组
byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
System.out.print("转换后的byte数组:");
for (byte b : decodedBytes) {
System.out.print(b + " ");
}
}
}
```
输出结果:
```
Base64编码的字符串:AQIDBAU=
转换后的byte数组:1 2 3 4 5
```
其中,`Base64.getEncoder().encodeToString(bytes)`方法将byte数组转换为Base64编码的字符串,`Base64.getDecoder().decode(encodedString)`方法将Base64编码的字符串转换为byte数组。