您的位置:首页 > 其它

base64加密解密实现方法

2013-05-24 20:54 645 查看
/**
* Base64加密算法
* @param str
* @return
*/
public static String base64Encode(String str) throws Exception {
String retStr = "";
if(StringUtils.isBlank(str)) {
return "";
}
try{
//BASE64加密算法
BASE64Encoder base64 = new BASE64Encoder();
byte[] xmlStr = str.getBytes();
retStr = base64.encode(xmlStr);
}catch(Exception e){
throw new RuntimeException("Base64编码 加密 失败!");
}
return retStr;
}

/**
* Base64解码算法
* @param str
* @return
*/
public static String base64Decode(String str) throws Exception{
if(StringUtils.isBlank(str)) {
return "";
}
byte[] bt = null;
String retStr = "";
try{
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
bt = decoder.decodeBuffer(str);
retStr = new String(bt);
}catch(Exception e){
throw new RuntimeException("XML字符串Base64解码失败");
}
return retStr;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: