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 );  
原文地址:https://www.cnblogs.com/enshrineZither/p/3441952.html