关于base64编码Encode和Decode编码的几种方式

摘要:
System.out.println(encodedText);finalBASE64解码器解码器=newBASE64解码器();//编码finalStringencodedText=编码器。编码(textByte);FinalStringtext=“字符串文本”;System.out.println(encodedText);

关于base64编码Encode和Decode编码的几种方式

Base64是一种能将任意Binary资料用64种字元组合成字串的方法,而这个Binary资料和字串资料彼此之间是可以互相转换的,十分方便。在实际应用上,Base64除了能将Binary资料可视化之外,也常用来表示字串加密过后的内容。如果要使用Java 程式语言来实作Base64的编码与解码功能,可以参考本篇文章的作法。

早期作法

早期在Java上做Base64的编码与解码,会使用到JDK里sun.misc套件下的BASE64Encoder和BASE64Decoder这两个类别,用法如下:

final BASE64Encoder encoder = new BASE64Encoder();
final BASE64Decoder decoder = new BASE64Decoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//编码
final String encodedText = encoder.encode(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(decoder.decodeBuffer(encodedText), "UTF-8"));

final BASE64Encoder encoder = new BASE64Encoder();
final BASE64Decoder decoder = new BASE64Decoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//编码
final String encodedText = encoder.encode(textByte);
System.out.println(encodedText);

//解码
System.out.println(new String(decoder.decodeBuffer(encodedText), "UTF-8"));

从以上程式可以发现,在Java用Base64一点都不难,不用几行程式码就解决了!只是这个sun.mis c套件所提供的Base64功能,编码和解码的效率并不太好,而且在以后的Java版本可能就不被支援了,完全不建议使用。

Apache Commons Codec作法

Apache Commons Codec有提供Base64的编码与解码功能,会使用到org.apache.commons.codec.binary套件下的Base64类别,用法如下:

final Base64 base64 = new Base64();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//编码
final String encodedText = base64.encodeToString(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(base64.decode(encodedText), "UTF-8"));

final Base64 base64 = new Base64();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//编码
final String encodedText = base64.encodeToString(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(base64.decode(encodedText), "UTF-8"));

以上的程式码看起来又比早期用sun.mis c套件还要更精简,效能实际执行起来也快了不少。缺点是需要引用Apache Commons Codec,很麻烦。

Java 8之后的作法

Java 8的java.util套件中,新增了Base64的类别,可以用来处理Base64的编码与解码,用法如下:

final Base64.Decoder decoder = Base64.getDecoder();
final Base64.Encoder encoder = Base64.getEncoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//编码
final String encodedText = encoder.encodeToString(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(decoder.decode(encodedText), "UTF-8"));

final Base64.Decoder decoder = Base64.getDecoder();
final Base64.Encoder encoder = Base64.getEncoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//编码
final String encodedText = encoder.encodeToString(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
 

与sun.mis c套件和Apache Commons Codec所提供的Base64编解码器来比较的话,Java 8提供的Base64拥有更好的效能。实际测试编码与解码速度的话,Java 8提供的Base64,要比sun.mis c套件提供的还要快至少11倍,比Apache Commons Codec提供的还要快至少3倍。因此在Java上若要使用Base64,这个Java 8底下的java .util套件所提供的Base64类别绝对是首选!

免责声明:文章转载自《关于base64编码Encode和Decode编码的几种方式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇curl c/c++ api接口使用例程 Mr_Von的专栏 博客频道 CSDN.NET2015程序员推荐书单下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

密码学解密网站汇总

0x00.综合 网站中包含大多编码的解码。http://web2hack.org/xssee/https://www.sojson.com/http://web.chacuo.net/ python的反编译 https://tool.lu/pyc/ 0x01.文字倒序排列 http://www.qqxiuzi.cn/zh/daoxu/ 0x02.cmd5解...

Apache Commons-Codec的使用

处理常用的编码方法的工具类包 例如DES、SHA1、MD5、Base64等. 导入包: <!-- 这个是编码解码的 --><dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId...

SILK编码语音转WAV格式

“  SILK音频编码格式介绍、WAV文件封装格式介绍以及SILK音频编码到WAV的转换。” 在语音相关的协议还原中,经常会遇到语音编码的问题,通常语音编码的数据无法直接展示,需要转换成WAV,MP3等格式,才能播放。这个转换过程,是首先将音频编码数据使用对应解码库解码为PCM流,然后再将PCM流根据封装格式的要求,进行编码封装,最后得到可供通用播放器...

JSP:服务器端和前端互传中文信息出现乱码

在前端和服务器端交互的过程中主要是通过HTTP协议进行交互的,而在Servlet接口中有提供一个HttpServlet类用于创建应用于HTTP协议的Servlet. 在Servlet容器中提供了ServletRequest和ServletResponse两个类,通过这两个类的对象来获得数据和发送数据。到这里我们就知道如果需要设置编码格式,那么我们就需要设置...

C#操作MySQL时,出现的中文乱码的解决方案

最终解决方案:1、选用gb2312编码2、设置my.ini文件中的默认编码   分别在[mysql]和[mysqld]配置段中增加或修改default_charater_set=gb2312 3、创建数据库时编码选用gb2312 4、创建表时编码选用gb2312,Collation选用gb2312_chines_ci(gb2312_bin没有去试验)...

java websocket @ServerEndpoint注解说明

http://www.blogjava.net/qbna350816/archive/2016/07/24/431302.html https://segmentfault.com/q/1010000004955225 https://www.cnblogs.com/interdrp/p/4091056.html 框架是workermansocket.io...