package com.hx.util; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * BASE64加密解密 */ public class BASE64 { /** * BASE64解密 * @param key * @return * @throws Exception */ public static byte[] decryptBASE64(String key) { byte[] t = new byte[]{}; try{ t = (new BASE64Decoder()).decodeBuffer(key); }catch (Exception e) { e.printStackTrace(); } return t; } /** * BASE64加密 * @param key * @return * @throws Exception */ public static String encryptBASE64(byte[] key){ String s = ""; try{ s = (new BASE64Encoder()).encodeBuffer(key); }catch (Exception e) { e.printStackTrace(); } return s; } public static void main(String[] args) throws Exception { String data = BASE64.encryptBASE64("123456789".getBytes()); System.out.println("加密前:"+data); byte[] byteArray = BASE64.decryptBASE64(data); System.out.println("解密后:"+new String(byteArray)); } }