java非对称加密算法原理及应用

使用JDK的类 BASE64Decoder  BASE64Encoder

 

Java代码   收藏代码
  1. package test;  
  2.   
  3. import sun.misc.BASE64Decoder;       
  4. import sun.misc.BASE64Encoder;       
  5.       
  6. /**    
  7.  * BASE64加密解密    
  8.  */      
  9. public class BASE64       
  10. {       
  11.       
  12.     /**     
  13.      * BASE64解密    
  14.    * @param key           
  15.      * @return           
  16.      * @throws Exception           
  17.      */                
  18.     public static byte[] decryptBASE64(String key) throws Exception {                 
  19.         return (new BASE64Decoder()).decodeBuffer(key);                 
  20.     }                 
  21.                     
  22.     /**          
  23.      * BASE64加密    
  24.    * @param key           
  25.      * @return           
  26.      * @throws Exception           
  27.      */                
  28.     public static String encryptBASE64(byte[] key) throws Exception {                 
  29.         return (new BASE64Encoder()).encodeBuffer(key);                 
  30.     }         
  31.            
  32.     public static void main(String[] args) throws Exception       
  33.     {       
  34.         String para = "{\"IdList1\": 1,\"IdList2\": [1,2,3,4,5,6,18],\"IdList3\": [1,2]}";  
  35.         String data = BASE64.encryptBASE64(para.getBytes());       
  36.         System.out.println("加密前:"+data);       
  37.                
  38.         byte[] byteArray = BASE64.decryptBASE64(data);       
  39.         System.out.println("解密后:"+new String(byteArray));       
  40.     }       
  41. }      

 

 

   使用Apache commons codec 类Base64获取【下载地址】  

 

Java代码   收藏代码
  1. package test;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4.   
  5. import org.apache.commons.codec.binary.Base64;  
  6.   
  7.   
  8.   
  9. public class Base64Util {  
  10.       
  11.     public static String encode(byte[] binaryData) throws UnsupportedEncodingException {  
  12.         return new String(Base64.encodeBase64(binaryData), "UTF-8");  
  13.     }  
  14.       
  15.     public static byte[] decode(String base64String) throws UnsupportedEncodingException {  
  16.         return Base64.decodeBase64(base64String.getBytes("UTF-8"));  
  17.     }  
  18.       
  19.       
  20.     public static void main(String[] args) throws UnsupportedEncodingException {  
  21.         String para = "{\"IdList1\": 1,\"IdList2\": [1,2,3,4,5,6,18],\"IdList3\": [1,2]}";  
  22.         String data = Base64Util.encode(para.getBytes());     
  23.         System.out.println("加密前:"+data);       
  24.                
  25.         byte[] byteArray = Base64Util.decode(data);  
  26.         System.out.println("解密后:"+new String(byteArray));    
  27.     }  
  28.   
  29.   
  30. }  

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值