package com.crunii.micro.gateway.utils;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.crunii.micro.common.utils.UUIDUtil;

import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gogo
 */
public class SymmetricCryptoUtil {
    /**
     * 16字节
     */
    private static final String ENCODE_KEY = "253140fdc9c44b0cb903c387b3e85d64";

    private static final  SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, ENCODE_KEY.getBytes(StandardCharsets.UTF_8));



    public static void main(String[] args) {
        Map<String,Object> str = new HashMap<>();
        str.put("t",System.currentTimeMillis()+200000);
        str.put("u",9999);
        str.put("r","/service-study-center/yjs/qzh/auth/pageSearchTestPaper");
        str.put("w",UUIDUtil.getUUID());
        System.out.println(aes.encryptBase64(JSONUtil.toJsonStr(str)));
    }

    public static String decryptFromString(String data) {
        String str;
        try {
            str = aes.decryptStr(data, StandardCharsets.UTF_8);
        } catch (Exception e) {
            return null;
        }
        return str;
    }

}

