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