base64加密解密


簡介

  base64加密解密算法是我們編程中常用的,有很多第三方開源jar包提供base64加密解密算法。

比如apache的commons-codec的jar包,還有sun jdk自帶的sun.misc.BASE64Decoder。

加密解密:

 1 import sun.misc.BASE64Decoder;
 2 import sun.misc.BASE64Encoder;
 3 
 4 @SuppressWarnings("restriction")
 5 public class Base64Util {
 6 /**
 7      * BASE64解密
 8      * 
 9      * @param key
10      * @return
11      * @throws Exception
12      */
13     public static byte[] decryptBASE64(String key) throws Exception {
14         return (new BASE64Decoder()).decodeBuffer(key);
15     }
16 
17     /**
18      * BASE64加密
19      * 
20      * @param key
21      * @return
22      * @throws Exception
23      */
24     public static String encryptBASE64(byte[] key) {
25         return (new BASE64Encoder()).encodeBuffer(key);
26     }
27 }

用法

1 String origValue = "cGFzaWVy"; //pasier
2 String k1 = new String(Base64Util.decryptBASE64(origValue), "UTF-8");
3 System.out.println("解密后:"+k1);
4 
5 String k2 ="pasiecsa";
6 //將k2進行加密
7 String s2 = Base64Util.encryptBASE64(k2.getBytes());
8 System.out.println("加密后:"+s2 );  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM