package com.hfkj.common.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Calendar;

/**
 * @className: AESTool
 * @author: HuRui
 * @date: 2022/9/27
 **/
public class AESTool {

    public static String Encrypt(String sSrc, String sKey)
    {
        try
        {
            return toHex(encryptBytes(sSrc.getBytes("utf-8"), sKey));
        } catch (Exception ex) {
            throw new RuntimeException(ex.toString());
        }
    }

    public static byte[] encrypt(byte[] sSrc, String sKey) {
        try {
            return encryptBytes(sSrc, sKey);
        } catch (Exception ex) {
            throw new RuntimeException(ex.toString());
        }
    }

    public static byte[] encryptBytes(byte[] sSrc, String sKey) throws Exception
    {
        if (sKey == null) {
            throw new Exception("Key为空null");
        }

        if (sKey.length() != 16) {
            throw new Exception("Key长度不是16位");
        }
        byte[] raw = sKey.getBytes("utf-8");

        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(1, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc);
        return encrypted;
    }

    public static String Decrypt(String sSrc, String sKey)
    {
        try {
            return new String(decryptBytes(toBytes(sSrc), sKey), "utf-8");
        } catch (Exception ex) {
            throw new RuntimeException(ex.toString());
        }
    }

    public static byte[] decrypt(byte[] sSrc, String sKey) {
        try {
            return decryptBytes(sSrc, sKey);
        } catch (Exception ex) {
            throw new RuntimeException(ex.toString());
        }
    }


    public static byte[] decryptBytes(byte[] sSrc, String sKey)
            throws Exception
    {
        if (sKey == null) {
            throw new Exception("Key为空null");
        }

        if (sKey.length() != 16) {
            throw new Exception("Key长度不是16位");
        }
        byte[] raw = sKey.getBytes("utf-8");

        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(2, skeySpec);
        byte[] original = cipher.doFinal(sSrc);
        return original;
    }




    public static String toHex(byte[] buf)
    {
        if (buf == null)
            return "";
        StringBuffer result = new StringBuffer(2 * buf.length);
        for (int i = 0; i < buf.length; i++) {
            appendHex(result, buf[i]);
        }
        return result.toString();
    }

    private static void appendHex(StringBuffer sb, byte b) {
        sb.append("0123456789ABCDEF".charAt(b >> 4 & 0xF)).append("0123456789ABCDEF".charAt(b & 0xF));
    }



    public static byte[] toBytes(String hexString)
    {
        int len = hexString.length() / 2;
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++)
            result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
                    16).byteValue();
        return result;
    }

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        cal.add(5, 10);
        String str = "1,13439676580," + cal.getTimeInMillis();
        String key = "0123456789ABCDEF";
        String token = Encrypt(str, key);
        System.out.println(token);
        System.out.println(Decrypt(token, key));
    }
}
