package com.jiayue.insu.inclientqn.service;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.AlgorithmParameters;
import java.security.Security;
import java.util.Arrays;

/**
 * TODO
 *
 * @author xsl
 * @version 3.0
 */
public class Test {
    public static void main(String[] args) {
        String result = "";
        // 被加密的数据
        byte[] dataByte = Base64.decode("EH5P41Kv25xvV2XDwhux6m6NIs88LuuANDc8XUC18RUdaV6LbkEpECL+9TrKy2M2D+RJc0ByEG61GhPjncbL0AM8bxsT7AaPADyKpYfzuxup1hOX6h17J3jfIUySDtYM7KK4KK7DXYp7JxhjH0Z9ik6ZXdK5gt5ptuvqXlV4igKwimlBJ9JFmC8MKyHDZYEFrQOgCtuMzXPY5Gwj76yEDH+ni6b7blBVxpx7E/gcwbxnesnZc9AgALK26CfcOOrFh0VVWtn9A9wCKn5Nvn74B6cKN2dBEb5ZFnuXTCqsqANDHyHllDDwECmhAahHTIDeUYxRtf9qUEMimFFW+HEtskgiT6Gn+yzLAiLyLEVGJyw0ZKdPYzgMfDYdqwrXGwMZ4V7FmTsuQ9iAcLuUnat1Mpp32nT9TweF32uXdZ9eP5Y=");
        // 加密秘钥
        byte[] keyByte = Base64.decode("wMPVnmd7rOUTDrlbKPKgtA==");
        // 偏移量
        byte[] ivByte = Base64.decode("8xCEBpnmeo5NeGPymlfKWA==");
        try {
            // 如果密钥不足16位，那么就补足. 这个if 中的内容很重要
            int base = 16;
            if (keyByte.length % base != 0) {
                int groups = keyByte.length / base
                        + (keyByte.length % base != 0 ? 1 : 0);
                byte[] temp = new byte[groups * base];
                Arrays.fill(temp, (byte) 0);
                System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
                keyByte = temp;
            }
            // 初始化
            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
            SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
            AlgorithmParameters parameters = AlgorithmParameters
                    .getInstance("AES");
            parameters.init(new IvParameterSpec(ivByte));
            // 初始化
            cipher.init(Cipher.DECRYPT_MODE, spec, parameters);
            byte[] resultByte = cipher.doFinal(dataByte);
            if (null != resultByte && resultByte.length > 0) {
                result = new String(resultByte, "UTF-8");
                System.out.println(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
