转载链接:java 实现base64编码的三种方式、各方式性能比较、最后给出最优工具类 小白实操记录
转载内容:
简单介绍了base64编码,给出三种实现方式性能比较,可直接使用最后提供的工具类,性能比较不必细看,没营养。
1 base64编码
将字符串编码成[0-9a-zA-Z+/=]的格式,通过解码又可以换成成原有的样子,就是这么简单。
我爱编码!--> 5oiR54ix57yW56CB77yB---我爱编码
2 三种实现方式性能比较
package util.base64;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import org.apache.commons.codec.binary.Base64;
/**
*
* base64编码实现的三种实现方式性能测试,推荐使用Java8提供的方法
*
*/
public class BASE64EncoderTest {
/**
* 实际测试编码与解码速度的话,Java 8提供的Base64比Apache Commons Codec提供的还要快
* ,Apache Commons Codec提供的比sun.misc提供的还要快。
* 因此在Java上若要使用Base64,这个Java 8的java.util提供的Base64类是首选!
*
*
*
* @param args
*/
public static void main(String[] args){
sunMiscDemo();
apacheCommonsCodecDemo();
java8UtilDemo();
}
/**
* 早期在Java上做Base64的编码与解码,会使用到JDK里sun.misc套件下的BASE64Encoder和
* BASE64Decoder这两个类别,用法如下:
*Base64的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder的sun.misc.
* BASE64Encoder/BASE64Decoder类。
* 这个类是sun公司的内部方法,并没有在java api中公开过,不属于JDK标准库范畴,但在JDK中包含了该类,可以直接使用。
* 但是在Eclipse和MyEclipse中直接使用,却找不到该类。解决方法如下:
*/
public static void sunMiscDemo() {
BASE64Encoder encoder = new BASE64Encoder();
BASE64Decoder decoder = new BASE64Decoder();
String str = "字串文字";
String strEncoder = null;
String strDecoder = null;
Long startDate = new Date().getTime();
for(int i = 0; i < 1000000; i++){
//编码
try {
strEncoder = encoder.encode(str.getBytes("UTF-8"));
//System.out.println("strEncoder=" + strEncoder);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//解码
try {
strDecoder = new String(decoder.decodeBuffer(strEncoder), "UTF-8");
//System.out.println("strDecoder=" + strDecoder);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Long endDate = new Date().getTime();
System.out.println("SunMis test 100w次 加密解密耗时:" + (endDate - startDate));
}
/**
* Apache Commons Codec有提供Base64的编码与解码功能,会使用到
* org.apache.commons.codec.binary套件下的Base64类别
*/
public static void apacheCommonsCodecDemo() {
Base64 base64 = new Base64();
String str = "字串文字";
String strEncode = null;
String strDecode = null;
Long startDate = new Date().getTime();
for(int i = 0; i < 1000000; i++){
byte[] b = null;
//编码
try {
strEncode = new String(base64.encode(str.getBytes("UTF-8")), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//解码
try {
strDecode = new String(base64.decode(strEncode.getBytes("UTF-8")), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
Long endDate = new Date().getTime();
System.out.println("ApacheCommons test 100w次耗时:" + (endDate - startDate));
}
/**
* Java 8之后的作法
* Java 8的java.util套件中,新增了Base64的类别,可以用来处理Base64的编码与解码,用法如下:
*
*/
public static void java8UtilDemo() {
java.util.Base64.Encoder encoder = java.util.Base64.getEncoder();
java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
String str = "字串文字";
String strEncoder = null;
String strDecoder = null;
Long startDate = new Date().getTime();
for(int i = 0; i < 1000000; i++){
//编码
try {
strEncoder = encoder.encodeToString(str.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//解码
try {
strDecoder = new String(decoder.decode(strEncoder), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
Long endDate = new Date().getTime();
System.out.println("Java8 test 100w次耗时:" + (endDate - startDate));
}
}
运行结果:
3 最优方式工具类
package util.base64;
import java.util.Base64;
/**
* base64 加密解密
*/
public class Base64UtilXb {
public static void main(String[] args) {
System.out.println(encrypt("我爱编码!"));
}
/**
* BASE64加密
*/
public static String encrypt(String str){
if(str==null)return null;
byte[] bytes = str.getBytes();
//Base64 加密
String encoded = Base64.getEncoder().encodeToString(bytes);
System.out.println("Base 64 加密后:" + encoded);
return encoded;
}
/**
* BASE64解密
* @throws Exception
*/
public static String decrypt(String key) {
if(key==null)return null;
byte[] decoded = Base64.getDecoder().decode(key);
String decodeStr = new String(decoded);
System.out.println("Base 64 解密后:" + decodeStr);
return decodeStr;
}
}