JAVAEE教程()JDk8封装的Base64加密方式
import sun.misc.BASE64Decoder;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
/**
* @author lpinfo
* @date 2019/5/9 13:41
**/
public class JDK8Base64Util {
===============明文字节字符转 base64字节字符
public static String encodeByteToBase64String(byte[] textbytes){
Base64.Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(textbytes);
}
public static String encodeStringToBase64String(String text){
Base64.Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(text.getBytes());
}
public static String decodeBase64StringToString(String base64String) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
byte[]bytes = decoder.decodeBuffer(base64String);
return new String(bytes);
}
public static void main(String[] args) throws IOException {
String text = "良品信息lpinfo.com";
String base64Str = "6Imv5ZOB5L+h5oGvbHBpbmZvLmNvbQ==";
//编码
String stringToBase64String = encodeStringToBase64String(text);
System.out.println("[encodeStringToBase64String]:"+stringToBase64String);
String byteToBase64String = encodeByteToBase64String(text.getBytes());
System.out.println("[encodeByteToBase64String]:"+byteToBase64String);
//解码
String base64StringToString = decodeBase64StringToString(base64Str);
System.out.println("[base64StringToString]:"+base64StringToString);
}
}
//测试结果