Base64Util工具类
2018-05-09 本文已影响0人
陈煦缘
参考
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.UnsupportedEncodingException;
public class Base64Util {
// 加密
public static String encode(String str) {
byte[] b = null;
String s = null;
try {
b = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (b != null) {
s = new BASE64Encoder().encode(b);
}
return s;
}
// 解密
public static String decode(String s) {
byte[] b = null;
String result = null;
if (s != null) {
BASE64Decoder decoder = new BASE64Decoder();
try {
b = decoder.decodeBuffer(s);
result = new String(b, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static byte[] decodeImg(String imgStr) {
byte[] b = null;
if (!StringUtils.isNotBlank(imgStr)) {
return b;
}
try {
BASE64Decoder decoder = new BASE64Decoder();
b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; i++) {
if (b[i] < 0) {
b[i] += 256;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return b;
}
}