您的位置:首页 > 编程语言 > Java开发

JAVA 使用BASE64加密、解密

2012-11-21 09:48 507 查看
import sun.misc.BASE64Decoder;(此Jar位于rt.jar)
/**
* 用BASE64加密
* @param str
* @return
*/
private static String getBASE64(String str) {
byte[] b = str.getBytes();
String s = null;
if (b != null) {
s = new sun.misc.BASE64Encoder().encode(b);
}
return s;
}

/**
* 解密BASE64字窜
* @param s
* @return
*/
private static String getFromBASE64(String s) {
byte[] b = null;
if (s != null) {
BASE64Decoder decoder = new BASE64Decoder();
try {
b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) {
e.printStackTrace();
}
}
return new String(b);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: