当前位置:   article > 正文

base64编码Encode和Decode编码的几种方式_base64.encode

base64.encode

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。

  1. import java.io.IOException;
  2. import java.io.UnsupportedEncodingException;
  3. import java.util.Date;
  4. import sun.misc.BASE64Decoder;
  5. import sun.misc.BASE64Encoder;
  6. import org.apache.commons.codec.binary.Base64;
  7. /**
  8. * 首先这算法是编码, 不是压缩, 编码后只会增加字节数;
  9. * 算法简单, 几乎不会影响效率;
  10. * 算法可逆, 解码很方便, 不用于私密信息通信;
  11. * 虽然解码方便, 但毕竟编码了, 肉眼还是不能直接看出原始内容;
  12. * 加密后的字符串只有[0-9a-zA-Z+/=], 不可打印字符(包括转移字符)也可传输;
  13. *
  14. */
  15. public class BASE64EncoderDecoder {
  16. /**
  17. * 实际测试编码与解码速度的话,Java 8提供的Base64比Apache Commons Codec提供的还要快,Apache Commons Codec提供的比sun.misc提供的还要快。
  18. * 因此在Java上若要使用Base64,这个Java 8的java.util提供的Base64类是首选!
  19. * @param args
  20. */
  21. public static void main(String[] args){
  22. //base642SunMiscDemo();
  23. base642ApacheCommonsCodecDemo();
  24. //base642UtilDemo();
  25. }
  26. /**
  27. * 早期在Java上做Base64的编码与解码,会使用到JDK里sun.misc套件下的BASE64Encoder和BASE64Decoder这两个类别,用法如下:
  28. *
  29. * Base64的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder的sun.misc.BASE64Encoder/BASE64Decoder类。
  30. * 这个类是sun公司的内部方法,并没有在java api中公开过,不属于JDK标准库范畴,但在JDK中包含了该类,可以直接使用。
  31. * 但是在Eclipse和MyEclipse中直接使用,却找不到该类。解决方法如下:
  32. *
  33. * 1.右键项目--》Build Path --》Configure Build Path 选择Libraries页签
  34. * 2.展开JRE System Library[javaSE-1.6]树,如果之前没有定义规则,会显示No rules defined,选中第一项(Access rules:No rules defined)。
  35. * 3.点击左边的Edit...按钮,弹出的下一窗口再点Add...
  36. * 4.现在到了Add Access Rule对话框,在Resolution下拉列表框中选择Accessible,Rule Pattern文本框写上需要访问的包路径(sun/misc/*或**)。
  37. * 5.一直点击OK,把前面的所有窗口都点掉就完成了。
  38. *
  39. * 加密解密10个100000次时间
  40. * 第0次运行时间:262
  41. * 第1次运行时间:96
  42. * 第2次运行时间:103
  43. * 第3次运行时间:66
  44. * 第4次运行时间:52
  45. * 第5次运行时间:114
  46. * 第6次运行时间:56
  47. * 第7次运行时间:51
  48. * 第8次运行时间:51
  49. * 第9次运行时间:50
  50. */
  51. public static void base642SunMiscDemo() {
  52. BASE64Encoder encoder = new BASE64Encoder();
  53. BASE64Decoder decoder = new BASE64Decoder();
  54. String str = "字串文字";
  55. String strEncoder = null;
  56. String strDecoder = null;
  57. for(int x = 0; x < 10; x++){
  58. Long startDate = new Date().getTime();
  59. for(int i = 0; i < 10000; i++){
  60. //编码
  61. try {
  62. strEncoder = encoder.encode(str.getBytes("UTF-8"));
  63. //System.out.println("strEncoder=" + strEncoder);
  64. } catch (UnsupportedEncodingException e) {
  65. e.printStackTrace();
  66. }
  67. //解码
  68. try {
  69. strDecoder = new String(decoder.decodeBuffer(strEncoder), "UTF-8");
  70. //System.out.println("strDecoder=" + strDecoder);
  71. } catch (UnsupportedEncodingException e) {
  72. e.printStackTrace();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. Long endDate = new Date().getTime();
  78. System.out.println("第" + x + "次运行时间:" + (endDate - startDate));
  79. }
  80. }
  81. /**
  82. * Apache Commons Codec有提供Base64的编码与解码功能,会使用到org.apache.commons.codec.binary套件下的Base64类别,用法如下:
  83. *
  84. * 缺点是需要引用Apache Commons Codec,很麻烦。
  85. * commons-codec是Apache下面的一个加解密开发包
  86. * 个人地址:https://pan.baidu.com/s/18hM2zGgOMFRuKhvQx1AyWA 密码:g850
  87. *
  88. * 官方地址为:http://commons.apache.org/codec/
  89. * 官方下载地址:http://commons.apache.org/codec/download_codec.cgi
  90. * 在线文档:http://commons.apache.org/codec/userguide.html
  91. *
  92. * 加密解密10个100000次时间
  93. * 第0次运行时间:92
  94. * 第1次运行时间:38
  95. * 第2次运行时间:41
  96. * 第3次运行时间:19
  97. * 第4次运行时间:83
  98. * 第5次运行时间:30
  99. * 第6次运行时间:25
  100. * 第7次运行时间:19
  101. * 第8次运行时间:72
  102. * 第9次运行时间:20
  103. */
  104. public static void base642ApacheCommonsCodecDemo() {
  105. Base64 base64 = new Base64();
  106. String str = "字串文字";
  107. String strEncode = null;
  108. String strDecode = null;
  109. for(int x = 0; x < 10; x++){
  110. Long startDate = new Date().getTime();
  111. for(int i = 0; i < 10000; i++){
  112. byte[] b = null;
  113. //编码
  114. try {
  115. strEncode = new String(base64.encode(str.getBytes("UTF-8")), "UTF-8");
  116. //System.out.println("strEncode=" + strEncode);
  117. } catch (UnsupportedEncodingException e) {
  118. e.printStackTrace();
  119. }
  120. //解码
  121. try {
  122. strDecode = new String(base64.decode(strEncode.getBytes("UTF-8")), "UTF-8");
  123. //System.out.println("strDecode=" + strDecode);
  124. } catch (UnsupportedEncodingException e) {
  125. e.printStackTrace();
  126. }
  127. }
  128. Long endDate = new Date().getTime();
  129. System.out.println("第" + x + "次运行时间:" + (endDate - startDate));
  130. }
  131. }
  132. /**
  133. * Java 8之后的作法
  134. * Java 8的java.util套件中,新增了Base64的类别,可以用来处理Base64的编码与解码,用法如下:
  135. *
  136. * 加密解密10个100000次时间
  137. * 第0次运行时间:没有调用
  138. */
  139. public static void base642UtilDemo() {
  140. java.util.Base64.Encoder encoder = java.util.Base64.getEncoder();
  141. java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
  142. String str = "字串文字";
  143. String strEncoder = null;
  144. String strDecoder = null;
  145. for(int x = 0; x < 10; x++){
  146. Long startDate = new Date().getTime();
  147. for(int i = 0; i < 100000; i++){
  148. //编码
  149. try {
  150. strEncoder = encoder.encodeToString(str.getBytes("UTF-8"));
  151. } catch (UnsupportedEncodingException e) {
  152. e.printStackTrace();
  153. }
  154. //解码
  155. try {
  156. strDecoder = new String(decoder.decode(strEncoder), "UTF-8");
  157. } catch (UnsupportedEncodingException e) {
  158. e.printStackTrace();
  159. }
  160. }
  161. Long endDate = new Date().getTime();
  162. System.out.println("第" + x + "次运行时间:" + (endDate - startDate));
  163. }
  164. }
  165. }

 

希望对你有帮助,祝你有一个好心情,加油!

若有错误、不全、可优化的点,欢迎纠正与补充!

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号