JAVA中有没有Base64加密解密方法?一个字节流经过Base64的加密然后再解密的过程,高手发段代码学习下

解决方案 »

  1.   

    package something;
     import sun.misc.BASE64Decoder;
     /**
     *  BASE64 编码与解码
     * @author jifeng
     *
     */
     public class Base64Test {
         // 将 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;
            }
         }
         
         public static void main(String[] args){
          
         // String temp = "Subject: =?gb2312?B?1tC5+tbQufrW0Ln61tC5+tbQufrW0Ln61tC5+tbQufo=?=";
          String temp = "1tC5+rPJwaI";
          String str = getFromBASE64(temp);
          System.out.println(str);
         }
     }