import java.io.UnsupportedEncodingException;
/**
* Base64工具类
* @author liqijing
*
*/
public class Base64Tools {
// 加密
@SuppressWarnings("restriction")
public static String getBase64(String str) {
byte[] b = null;
String s = null;
try {
b = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (b != null) {
s = new sun.misc.BASE64Encoder().encode(b);
}
return s;
}
// 解密
@SuppressWarnings("restriction")
public static String getFromBase64(String s) {
byte[] b = null;
String result = null;
if (s != null) {
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
try {
b = decoder.decodeBuffer(s);
result = new String(b, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
}
Base64编码与解码
最新推荐文章于 2024-02-23 19:30:06 发布