package com.ytpm.test;


import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.asymmetric.RSA;
import com.ytpm.util.EncryptUtils;
import org.junit.Test;

/**
 * @author hidewnd
 * @date 2026/1/13 10:27
 */

public class RsaTest {


    @Test
    public void generatorRsaKey() {
        RSA rsa = new RSA();
        //获得私钥
        System.out.println("private:");
        System.out.println(rsa.getPrivateKeyBase64());
        //获得公钥
        System.out.println("public:");
        System.out.println(rsa.getPublicKeyBase64());
    }

//    private static String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANyqac4YGjXSH+7cNTkYj1k4HRPab1DP6UBRxPPNgMonEIj6tFoC9At5HvdsI6Ff+U22bZNcYqZOHaqX6DvoMJb7W3QiyqUOiTesl5CCqpm2zFr1F+6bnawXNgYsGahP5bVuh8uOTWOiavofId1Xiq4xl8Ca/Ikg09fJb5t2EROxAgMBAAECgYAvIi1YariKf3f3rsylhfP0lDJNz84QHmDNrsmZY+9a/q4e/CHyygezvaraIZyYtyXKrilInSarLCtVCX5+8VETQUp5v2znDEvyLSiUWMp5hgbR4U/SGjti3rDnrxn+VUr2uieq0o6vVHULcCuEaiUOlIvUIDLONxQWVCvyy0CyWQJBAPPsWV90DuzRHJf8nYfCNby9SLaAXdDxe/5XhSf7JSA29v84n9rPHuUNxW1VnEO0m1uTj0j+BX2k7Qedo0xDv3UCQQDnl0gnpLPmgPgMbQ5eRvDygkkKhfvKukkrCsWFh1atj3QMKiEiyrjh0qa0gViqZi97VFgpriaOJ/N8F+buwFfNAkBeRb1w79kI2/TyJNb5biz4qitdJZWcuIgUj9zNy/nYToX9s/PMTHqDSmRAUnMXk8jrhsc9DbgvjDtz0xAW3KQ1AkEAvfIehCVaSBxPViG8PycASGcDcVYjBktz55GWH8a5GPEnxTJoqxiKOu2DPVk5V8kn+IddKBJmHQI3efhAuGGZoQJAIsHVfDgnhEt/APq5p0x4VMSg5nCD0LupDDJ4vA8Ort965T2o2KbHUlXBSd/U0iutSahqyrNhkORYSsyby7OyTg==";
//    private static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcqmnOGBo10h/u3DU5GI9ZOB0T2m9Qz+lAUcTzzYDKJxCI+rRaAvQLeR73bCOhX/lNtm2TXGKmTh2ql+g76DCW+1t0IsqlDok3rJeQgqqZtsxa9Rfum52sFzYGLBmoT+W1bofLjk1jomr6HyHdV4quMZfAmvyJINPXyW+bdhETsQIDAQAB";

    private static String privateKey = "";
    private static String publicKey = "";
    @Test
    public void decoryTest() {
        // 返回内容 解密逻辑
        // responseKey 从response header中获取"encrypt-key"
        String responseKey = "yRm7gaEyol6Az1zuA0JVbF7dUjTzgTgZnrryKnPoY8HdySf5OiXIRTmI5hclu+d/wSzNzBsA0lk/jaPlfFDHDFjtqQdMarIIbNmhmSo/M30rGCTbhk/Mv5bJkkIFH+8W6j+rYnqSt0qFElDt/jOQ1hHrzKY7U0D8vLHYkhxY06c=";
        String responseBody = "4yCh2rn2yG0dIKtuOGI9TWvRPl7XpoATwc3sAIJ7uj1o343V7qiN6mzObFHNNMjl";
        // rsa解码获取aes密钥
        String decryptByRsa = EncryptUtils.decryptByRsa(responseKey, privateKey);
        // base64解码获取真实ase密钥
        String aesPassword = EncryptUtils.decryptByBase64(decryptByRsa);
        // ase解码获取返回数据 运算模式:ECP  填充模式:pkcs7  初始向量:（IV）: null
        String decryptBody = EncryptUtils.decryptByAes(responseBody, aesPassword);
        System.out.println(decryptBody);
    }

    @Test
    public void encoryTest() {
        // 请求参数 加密逻辑
        // 随机生成32为密钥
        String aesPassword = RandomUtil.randomString(32);
        System.out.println("aesPassword:");
        System.out.println(aesPassword);
        // 对aes密钥进行base64加密
        String base64Str = EncryptUtils.encryptByBase64(aesPassword);
        // 对base64字符串进行rsa加密获取requestkey
        // reqeustKey 放入request header "encrypt-key"
        String requestKey = EncryptUtils.encryptByRsa(base64Str, publicKey);
        System.out.println("requestKey:");
        System.out.println(requestKey);
        // 对请求信息进行aes加密
        String requestBody = "{\"adSourceIndex\":\"0\",\"adSourceType\":2,\"appId\":71139,\"beginTime\":\"2026-01-06 11:26:12\",\"begintimestamp\":1767669972000,\"ecpm\":\"18.060000\",\"finishTime\":\"2026-01-06 11:34:01\",\"finishtimestamp\":1767670441000,\"iosId\":\"2CDDCF9E-BD86-49B3-8DC2-21993976ABD2\",\"networkFormId\":0,\"networkName\":\"gdt\",\"networkPlacementId\":2255033504285752,\"nickName\":\"Tourists3ce517e2\",\"placementId\":\"2255033504285752\",\"recordId\":\"190453\",\"revenue\":\"0.018060\",\"userId\":2610503644}";
        String encryptByRsa = EncryptUtils.encryptByAes(requestBody, aesPassword);
        System.out.println("requestBody:");
        System.out.println(encryptByRsa);
    }
}
