package com.lzkj.csp.bas.util;

 @SuppressWarnings("restriction")
public class Base64Utils { private final static sun.misc.BASE64Encoder base64Encoder = new sun.misc.BASE64Encoder();
private final static sun.misc.BASE64Decoder base64Decoder = new sun.misc.BASE64Decoder(); /**
* Base64-encode the given byte array to a String.
*
* @param src the original byte array (may be {@code null})
* @return the encoded byte array as a UTF-8 String (or {@code null} if the
* input was {@code null})
*/
public static String encodeToString(byte[] src) {
if (src == null)
return null;
return base64Encoder.encode(src);
} /**
* Base64-encode the given byte array to a String.
*
* @param src the original byte array (may be {@code null})
* @param followRFC2045 true Encoded lines be no more than 76 characters long
* false Encoded lines be more than 76 characters long
* @return
* @see <a href="https://tools.ietf.org/html/rfc2045"/>
* @see <a href="https://en.wikipedia.org/wiki/Base64#MIME"/>
* @see <a href="https://en.wikipedia.org/wiki/Request_for_Comments"/>
*/
public static String encodeToString(byte[] src, boolean followRFC2045) {
if (src == null)
return null;
String encode = base64Encoder.encode(src);
if (!followRFC2045) {
encode = encode.replaceAll(System.getProperty("line.separator"), "");
}
return encode;
} /**
* Base64-decode the given byte array from an UTF-8 String.
*
* @param src the encoded UTF-8 String (may be {@code null})
* @return the original byte array (or {@code null} if the input was
* {@code null})
*/
public static byte[] decodeFromString(String src) {
if (src == null)
return null;
try {
byte[] decodeBuffer = base64Decoder.decodeBuffer(src);
return decodeBuffer;
} catch (Exception e) {
throw new RuntimeException("decodeFromString failed.", e);
}
}
}
说明:以上的工具类中加密方法(encodeToString)有两个,一个是普通的base64加遵循RFC2045机密规则,此规则在加密后的符76位自动添加换行符,所以在此工具类中重写了encodeToString(String src ,boolean followRFC2045),true:遵从、false:不遵从

最新文章

  1. DelphiXE2 DataSnap开发技巧收集
  2. Centos是什么
  3. Android控件之ImageView(显示图片的控件)
  4. apache环境Zf2要装的intl验证
  5. Magento中调用JS文件的几种方法
  6. Macbook之安装opencv
  7. Jquery实现图片左右自动滚动
  8. Ubuntu 12.04 搭建 Eclipse Android 开发环境(转)
  9. Qt Creator error: LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
  10. js原生设计模式——12装饰者模式
  11. CF #284 div1 D. Traffic Jams in the Land 线段树
  12. WebService学习------小实例开发(号码归属地查询)
  13. CentOS 7 系统下 GitLab 搭建
  14. (83)Wangdao.com第十七天_JavaScript 定时器
  15. JSP 隐式对象
  16. hihocoder1524
  17. SAP的软件维护费用,交还是不交?
  18. 89. Gray Code返回位运算的所有生成值
  19. Consul内部分享ppt
  20. VS中修改工程名的解决方案

热门文章

  1. JDBC 工具类封装
  2. sql注入notebook
  3. web--ajax--json
  4. 文件包含漏洞(pikachu)
  5. 2019-2020-1 20199325《Linux内核原理与分析》第三周作业
  6. C# 基础知识系列- 14 IO篇 文件的操作
  7. vuex-persist数据持久化存储插件
  8. Excel中拆分列
  9. Windows 怎么启动 apache
  10. POJ - 3278 Catch That Cow 简单搜索