package com.fdkankan.common.util;

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

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class AesUtil {

    public static final String ALMODE_CBC_NOPADDING = "AES/CBC/NoPadding";

    /**
     * @author miracle.qu
     * AES算法加密明文
     * @param data 明文
     * @param key 密钥，长度16
     * @param iv 偏移量，长度16
     * @return 密文
     */
    public static String encryptAES(String data,String key,String iv) throws Exception {
        return encryptCBC(data,key,iv,ALMODE_CBC_NOPADDING);
    }

    public static String encryptCBC(String data,String key,String iv,String alMode) throws Exception {
        Cipher cipher = Cipher.getInstance(alMode);
        int blockSize = cipher.getBlockSize();
        byte[] dataBytes = data.getBytes();
        int plaintextLength = dataBytes.length;

        if (plaintextLength % blockSize != 0) {
            plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
        }

        byte[] plaintext = new byte[plaintextLength];
        System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

        cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
        byte[] encrypted = cipher.doFinal(plaintext);

        return new Base64().encodeToString(encrypted).trim();
    }

    public static String decryptCBC(String data, String key, String iv, String alMode) throws Exception {
        byte[] encrypted1 = new Base64().decode(data);

        Cipher cipher = Cipher.getInstance(alMode);
        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

        cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); //使用密钥初始化，设置为解密模式

        byte[] original = cipher.doFinal(encrypted1);	//执行操作
        String originalString = new String(original);
        return originalString.trim();
    }

    public static String encryptECB(String data,String key,String alMode) throws Exception {
        byte[] raw = key.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance(alMode);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] byteEncode = data.getBytes(StandardCharsets.UTF_8);
        byte[] byteAes = cipher.doFinal(byteEncode, 0, byteEncode.length);

        return java.util.Base64.getEncoder().encodeToString(byteAes);
    }

    public static String decryptECB(String data,String key,String alMode) throws Exception {
        byte[] raw = key.getBytes(StandardCharsets.UTF_8);
        Cipher cipher = Cipher.getInstance(alMode);
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] encrypted1 = java.util.Base64.getDecoder().decode(data);
        byte[] original = cipher.doFinal(encrypted1);

        //字节转换字符串
        return new String(original, StandardCharsets.UTF_8).trim();
    }

    public static void main(String[] args) throws Exception {
//        final String encode = cn.hutool.core.codec.Base64.encode("Aa1234567".getBytes());
//        System.out.println(encode);

        String data = "linjunbo@192.168.0.30@1695872675";
        String s = encryptECB(data, "3d8904474ebbdbbd81c5952524dad646", "AES/ECB/PKCS5Padding");
        System.out.println(s);
    }



}
