BASE64Encoder

来源:互联网 发布:网络工程公司 编辑:程序博客网 时间:2024/06/05 20:05

在项目过程中,用到对某段字符串进行BASE64加密,然后百度了下,找到解决方案,并记录下来,加深印象。

使用到的jar包:import sun.misc.BASE64Decoder; 

// 将 s 进行 BASE64 编码 
public static String getBASE64(String s) { 
  if (s == null) 
      return null; 
  return (new sun.misc.BASE64Encoder()).encode( s.getBytes() ); 



// 将 BASE64 编码的字符串 s 进行解码 
public static String getFromBASE64(String s) { 
if (s == null) return null; 
BASE64Decoder decoder = new BASE64Decoder(); 
try { 
byte[] b = decoder.decodeBuffer(s); 
return new String(b); 
} catch (Exception e) { 
return null; 

}

0 0