package com.chenyang.nse.bussiness.controller.restfulapi.tools;

import com.chenyang.nse.bussiness.tools.string.AesTool;
import java.util.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64Tool {
   public static void main(String[] args) throws Exception {
      String str = "masking:21232F297A57A5A743894A0E4A801FC3";
      base64(str);
      enAndDeCode(str);
   }

   public static void base64(String str) {
      byte[] bytes = str.getBytes();
      String encoded = Base64.getEncoder().encodeToString(bytes);
      System.out.println("Base 64 加密后：" + encoded);
      byte[] decoded = Base64.getDecoder().decode(encoded);
      String decodeStr = new String(decoded);
      System.out.println("Base 64 解密后：" + decodeStr);
      System.out.println();
   }

   public static void enAndDeCode(String str) throws Exception {
      String data = encryptBASE64(str);
      System.out.println("sun.misc.BASE64 加密后：" + data);
      String byteArray = decryptBASE64(data);
      System.out.println("sun.misc.BASE64 解密后：" + byteArray);
   }

   public static String decryptBASE64(String key) throws Exception {
      return new String((new BASE64Decoder()).decodeBuffer(key));
   }

   public static String encryptBASE64(String key) throws Exception {
      return (new BASE64Encoder()).encodeBuffer(key.getBytes());
   }

   public static String decrypt(String content, String password) throws Exception {
      return AesTool.decrypt(content, password);
   }

   public static String encrypt(String content, String password) throws Exception {
      return AesTool.encrypt(content, password);
   }
}
