在项目过程中,对某个字符串进行BASE64加密,遍布百度找到、记录解决方案,加深印象。
要使用的jar包: import sun.misc.base64解码器;
//S编码为BASE64
publicstaticstringgetbase 64 (strings ) {
if(s==null ) )。
返回空值;
return (news un.misc.base64 encoder () ).encode (s.getbytes ) );
}
对用BASE64编码的字符串s进行解码
publicstaticstringgetfrombase 64 (strings )。
if(s==null )返回空值;
base64解码器解码器=new base64解码器(;
try {
byte[]b=decoder.decodebuffer(s;
返回新字符串(b;
}catch(exceptione ) {
返回空值;
}
}