package com.boman.web.core.utils;

import com.boman.web.core.domain.JsonRequest;
import org.apache.tomcat.util.buf.HexUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.HashMap;
import java.util.Map;

import static com.boman.web.core.utils.RSAUtil.getUUID32;

/**
 * @author tjf
 * @Date: 2022/03/15/11:04
 */
public class AESUtil {
    /**
     * 加密
     *
     * @param content 加密文本
     * @param key     加密密钥，appSecret的前16位
     * @param iv      初始化向量，appSecret的后16位
     * @return
     * @throws Exception
     */
    public static String encrypt(String content, String key, String iv) throws Exception {
        byte[] raw = key.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //"算法/模式/补码方式"
        IvParameterSpec ivParam = new IvParameterSpec(iv.getBytes()); //使用CBC模式，需要一个向量iv，可增加加密算法的强度
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParam);
        byte[] encrypted = cipher.doFinal(content.getBytes());

        return new BASE64Encoder().encode(encrypted);
    }

    //解密
    public static String decrypt(String content, String key, String iv) throws Exception {
        byte[] raw = key.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding "); //"算法/模式/补码方式"
        IvParameterSpec ivParam = new IvParameterSpec(iv.getBytes()); //使用CBC模式，需要一个向量iv，可增加加密算法的强度
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParam);
        byte[] encrypted = new BASE64Decoder().decodeBuffer(content); //先用base64解密
        byte[] original = cipher.doFinal(encrypted);
        return new String(original);
    }

/*    public static void main(String[] args) {
        Map<String, Object> businessParams = new HashMap<>();
        String aseKey = "6717c09c65fd487bb61e6710613faa20";
        businessParams.put("idCard", "110102196610122373");
        try {
            //参数加密
            String encrypt = encrypt(JacksonUtil.beanToJson(businessParams), aseKey.substring(0, 16), aseKey.substring(16));
            System.out.println(encrypt);
            //对ase进行公钥加密
            byte[] enStr = RSAUtil.encryptByPublicKey(aseKey.substring(0, 16), "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAg5xiCI0UAPy1IZtjSTIW3atbIdtp07or7HybI8oxtgBwaSlZrpJ2daOtLhy+qndWHHD5ck1XG6K/QrZc28L5ITkJxldBclIIFZK7hi2lugbHL6hsgZl8Rs4fylfxmBNwIpOTJnifAbEBy2lSQSl2dcgofUDlWCchZMEZdi/EW1o3q+bP2pC0BEEacHfnyhpwzNjLXSJM7EDgbbgKZpVkNSaxR50nJ/Ma4BsrLUwAKCo3795MZZWX97ycceFc0DMGc3cvm04nXbvrgp57TeGLq1bjaqFOIf15dElzycVi8uwpAqJxkzU9BQb/cQsQHsrnjFo9NHXKUDayFPGVYvLljQIDAQAB");
            //得到RSA公钥加密后的ase秘钥
            String aseKeyStr = HexUtils.toHexString(enStr);
            JsonRequest jsonRequest = new JsonRequest();
            String uuid32 = getUUID32();
            jsonRequest.setRequestId(uuid32);
            System.out.println("uuid:" + uuid32);
            jsonRequest.setAppId(aseKey);
            long l = System.currentTimeMillis();
            System.out.println(l);
            jsonRequest.setTimestamp(l);
            jsonRequest.setAseKey(aseKeyStr);
            jsonRequest.setBody(encrypt);
            System.out.println(aseKeyStr);
            Map<String, Object> paramMap = RSAUtil.bean2Map(jsonRequest);
            // 参数排序
            Map<String, Object> sortedMap = RSAUtil.sort(paramMap);
            // 拼接参数：key1Value1key2Value2
            String urlParams = RSAUtil.groupStringParam(sortedMap);
            byte[] bytes1 = urlParams.getBytes();
            String value = HexUtils.toHexString(bytes1);
            //私钥签名
            String sign = RSAUtil.sign(HexUtils.fromHexString(value), "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCDnGIIjRQA/LUhm2NJMhbdq1sh22nTuivsfJsjyjG2AHBpKVmuknZ1o60uHL6qd1YccPlyTVcbor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xTsiK+adO2XEk3xJWDFBkCT/GmkuoMvdANgX8F7ztrRJ9M7VWuy1BqfxNhmmDZol8cK6hvQhfP9ZAoGADe1ObgJww6c4oFderSAqVedQfPdMCUwEQTU7xW5bkVj5PYDalce5vREqNHI5RQ2JzAIKog8z9Rq7a5Yho8HJV+cCdZ27uKodDVZ98Fj+ZHQhrdpcZY5qsErt+Q2VjnYPE/PBdDj+a533aS+mmepztmTs1IqcTPnQEiwGkYvPjbkCgYEAyYajCNkwYLYiBNFP45pb82dAPedfitNBMKX7m51/mLjSnUefXkqROwWAn3Q1twifH3brw1mOb00nja7chYGYpvN7stKGZiZ5HuILJLUNYuKKLYkaespsVs7H9bRBC+ENdgWSUpKaDZc8eF2vC2e+drzKeevwSszDlNRTFaEBP9s=");
            System.out.println(sign);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}
