java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。本站提供基于Java框架struts,spring,hibernate等的桌面应用、web交互及移动终端的开发技巧与资料

保持永久学习的心态,将成就一个优秀的你,来 继续搞起java知识。

使用JDK的类 BASE64Decoder  BASE64Encoder

 

package test;

import sun.misc.BASE64Decoder;     
import sun.misc.BASE64Encoder;     
    
/**   
 * BASE64<a href="http://www.cfei.net/archives/tag/%e5%8a%a0%e5%af%86" title="浏览关于“加密”的文章" target="_blank" class="tag_link">加密</a><a href="http://www.cfei.net/archives/tag/%e8%a7%a3%e5%af%86" title="浏览关于“解密”的文章" target="_blank" class="tag_link">解密</a>   
 */    
public class BASE64     
{     
    
    /**    
     * BASE64解密   
   * @param key          
     * @return          
     * @throws Exception          
     */              
    public static byte[] decryptBASE64(String key) throws Exception {               
        return (new BASE64Decoder()).decodeBuffer(key);               
    }               
                  
    /**         
     * BASE64加密   
   * @param key          
     * @return          
     * @throws Exception          
     */              
    public static String encryptBASE64(byte[] key) throws Exception {               
        return (new BASE64Encoder()).encodeBuffer(key);               
    }       
         
    public static void main(String[] args) throws Exception     
    {     
        String para = &quot;{\&quot;IdList1\&quot;: 1,\&quot;IdList2\&quot;: [1,2,3,4,5,6,18],\&quot;IdList3\&quot;: [1,2]}&quot;;
        String data = BASE64.encryptBASE64(para.getBytes());     
        System.out.println(&quot;加密前:&quot;+data);     
             
        byte[] byteArray = BASE64.decryptBASE64(data);     
        System.out.println(&quot;解密后:&quot;+new String(byteArray));     
    }     
}

 

 

   使用Apache commons codec 类Base64

 

package test;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;

public class Base64Util {
    
    public static String encode(byte[] binaryData) throws UnsupportedEncodingException {
        return new String(Base64.encodeBase64(binaryData), &quot;UTF-8&quot;);
    }
    
    public static byte[] decode(String <a href="http://www.cfei.net/archives/tag/base64" title="浏览关于“base64”的文章" target="_blank" class="tag_link">base64</a>String) throws UnsupportedEncodingException {
        return Base64.decodeBase64(base64String.getBytes(&quot;UTF-8&quot;));
    }
    
    
    public static void main(String[] args) throws UnsupportedEncodingException {
        String para = &quot;{\&quot;IdList1\&quot;: 1,\&quot;IdList2\&quot;: [1,2,3,4,5,6,18],\&quot;IdList3\&quot;: [1,2]}&quot;;
        String data = Base64Util.encode(para.getBytes());   
        System.out.println(&quot;加密前:&quot;+data);     
             
        byte[] byteArray = Base64Util.decode(data);
        System.out.println(&quot;解密后:&quot;+new String(byteArray));  
    }

}

 

 

 

 

javaBase64加密解密

因为水平有限,难免有疏忽或者不准确的地方,希望大家能够直接指出来,我会及时改正。一切为了知识的分享。

后续会有更多的精彩的内容分享给大家。