赞
踩
Base64编码的原理:https://www.cnblogs.com/libin-1/p/6165485.html
Base64可以将ASCII字符串或者是二进制编码成只包含A—Z,a—z,0—9,+,/ 这64个字符( 26个大写字母,26个小写字母,10个数字,1个+,一个 / 刚好64个字符)。这64个字符用6个bit位就可以全部表示出来,一个字节有8个bit 位,那么还剩下两个bit位,这两个bit位用0来补充。其实,一个Base64字符仍然是8个bit位,但是有效部分只有右边的6个 bit,左边两个永远是0。
- 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;
-
- /**
- * 首先这算法是编码, 不是压缩, 编码后只会增加字节数;
- * 算法简单, 几乎不会影响效率;
- * 算法可逆, 解码很方便, 不用于私密信息通信;
- * 虽然解码方便, 但毕竟编码了, 肉眼还是不能直接看出原始内容;
- * 加密后的字符串只有[0-9a-zA-Z+/=], 不可打印字符(包括转移字符)也可传输;
- *
- */
- public class BASE64EncoderDecoder {
-
- /**
- * 实际测试编码与解码速度的话,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){
- //base642SunMiscDemo();
- base642ApacheCommonsCodecDemo();
- //base642UtilDemo();
- }
-
- /**
- * 早期在Java上做Base64的编码与解码,会使用到JDK里sun.misc套件下的BASE64Encoder和BASE64Decoder这两个类别,用法如下:
- *
- * Base64的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder的sun.misc.BASE64Encoder/BASE64Decoder类。
- * 这个类是sun公司的内部方法,并没有在java api中公开过,不属于JDK标准库范畴,但在JDK中包含了该类,可以直接使用。
- * 但是在Eclipse和MyEclipse中直接使用,却找不到该类。解决方法如下:
- *
- * 1.右键项目--》Build Path --》Configure Build Path 选择Libraries页签
- * 2.展开JRE System Library[javaSE-1.6]树,如果之前没有定义规则,会显示No rules defined,选中第一项(Access rules:No rules defined)。
- * 3.点击左边的Edit...按钮,弹出的下一窗口再点Add...
- * 4.现在到了Add Access Rule对话框,在Resolution下拉列表框中选择Accessible,Rule Pattern文本框写上需要访问的包路径(sun/misc/*或**)。
- * 5.一直点击OK,把前面的所有窗口都点掉就完成了。
- *
- * 加密解密10个100000次时间
- * 第0次运行时间:262
- * 第1次运行时间:96
- * 第2次运行时间:103
- * 第3次运行时间:66
- * 第4次运行时间:52
- * 第5次运行时间:114
- * 第6次运行时间:56
- * 第7次运行时间:51
- * 第8次运行时间:51
- * 第9次运行时间:50
- */
- public static void base642SunMiscDemo() {
- BASE64Encoder encoder = new BASE64Encoder();
- BASE64Decoder decoder = new BASE64Decoder();
- String str = "字串文字";
- String strEncoder = null;
- String strDecoder = null;
- for(int x = 0; x < 10; x++){
- Long startDate = new Date().getTime();
- for(int i = 0; i < 10000; 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("第" + x + "次运行时间:" + (endDate - startDate));
- }
- }
-
- /**
- * Apache Commons Codec有提供Base64的编码与解码功能,会使用到org.apache.commons.codec.binary套件下的Base64类别,用法如下:
- *
- * 缺点是需要引用Apache Commons Codec,很麻烦。
- * commons-codec是Apache下面的一个加解密开发包
- * 个人地址:https://pan.baidu.com/s/18hM2zGgOMFRuKhvQx1AyWA 密码:g850
- *
- * 官方地址为:http://commons.apache.org/codec/
- * 官方下载地址:http://commons.apache.org/codec/download_codec.cgi
- * 在线文档:http://commons.apache.org/codec/userguide.html
- *
- * 加密解密10个100000次时间
- * 第0次运行时间:92
- * 第1次运行时间:38
- * 第2次运行时间:41
- * 第3次运行时间:19
- * 第4次运行时间:83
- * 第5次运行时间:30
- * 第6次运行时间:25
- * 第7次运行时间:19
- * 第8次运行时间:72
- * 第9次运行时间:20
- */
- public static void base642ApacheCommonsCodecDemo() {
- Base64 base64 = new Base64();
- String str = "字串文字";
- String strEncode = null;
- String strDecode = null;
- for(int x = 0; x < 10; x++){
- Long startDate = new Date().getTime();
- for(int i = 0; i < 10000; i++){
- byte[] b = null;
- //编码
- try {
- strEncode = new String(base64.encode(str.getBytes("UTF-8")), "UTF-8");
- //System.out.println("strEncode=" + strEncode);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
-
- //解码
- try {
- strDecode = new String(base64.decode(strEncode.getBytes("UTF-8")), "UTF-8");
- //System.out.println("strDecode=" + strDecode);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- Long endDate = new Date().getTime();
- System.out.println("第" + x + "次运行时间:" + (endDate - startDate));
- }
- }
-
- /**
- * Java 8之后的作法
- * Java 8的java.util套件中,新增了Base64的类别,可以用来处理Base64的编码与解码,用法如下:
- *
- * 加密解密10个100000次时间
- * 第0次运行时间:没有调用
- */
- public static void base642UtilDemo() {
- 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;
- for(int x = 0; x < 10; x++){
- Long startDate = new Date().getTime();
- for(int i = 0; i < 100000; 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("第" + x + "次运行时间:" + (endDate - startDate));
- }
- }
-
- }

希望对你有帮助,祝你有一个好心情,加油!
若有错误、不全、可优化的点,欢迎纠正与补充!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。