package com.hs.admin.common.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;

public class AESUtil {

    public static final String AES_KEY = "edFabJn3ZA==7JVk";//密钥自定义16位

    /**
     * AES加密+Base64转码
     *
     * @param data 明文（16进制）
     * @return
     */
    public static String encrypt(String data) {
        byte[] keyb = null;
        try {
            keyb = AES_KEY.getBytes("utf-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } // 明文
        SecretKeySpec sKeySpec = new SecretKeySpec(keyb, "AES");
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        byte[] bjiamihou = null;
        String miwen = "";
        try {
            bjiamihou = cipher.doFinal(data.getBytes("utf-8"));
            // byte加密后
            miwen = Base64.encodeBase64String(bjiamihou);// 密文用base64加密
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }

        return miwen;
    }

    /**
     * Base64解码 + AES解码
     *
     * @param data 密文 （16进制）
     * @return
     */
    public static String decrypt(String data){
        byte[] keyb = null;
        try {
            keyb = AES_KEY.getBytes("utf-8");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        byte[] miwen = Base64.decodeBase64(data);
        SecretKeySpec sKeySpec = new SecretKeySpec(keyb, "AES");
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            cipher.init(Cipher.DECRYPT_MODE, sKeySpec);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        byte[] bjiemihou = null;
        String mingwen = "";
        try {
            bjiemihou = cipher.doFinal(miwen);
            // byte加密后
            mingwen = new String(bjiemihou,"utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mingwen;
    }

    public static void main(String[] args) throws Exception {
        // 测试加密工具类
        String data = "{\"appCode\":\"portal\",\"createtime\":1592981430336,\"msgContent\":\"这里是通知的正文\",\"msgSignature\":\"中心（Janson）\",\"msgStatus\":0,\"msgTitle\":\"测试通知任务\",\"msgType\":1,\"objectCreateTime\":1592981430322,\"objectId\":\"123123123Id\",\"permission\":0,\"receiveUsers\":\"450503\",\"sendObject\":1,\"status\":0,\"userType\":1}";//明文
        String miwen = encrypt(" ");// 加密
        System.out.println(miwen);
        System.out.println(decrypt(miwen));// 解密

    }

}