package com.example.demo;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class KeyPairTest {
    
    public static void main(String[] args) throws Exception {
        // 生产环境应用私钥（新的）
        String privateKeyStr = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC2OS65zDqk7/Z6izOrq+y2S6vojWvfibm7I3MYnh03UaS1zrZqCnxVaAOt4iw3s1ucWDsD/aDRZf4uYVeGrFokfaab0V7x011JjaMM5CFG2rKDeEvGttp0QKE4G+1qNcsZcl760M0LGh9JmK8o7gZhppV4ouszrJFzLLR5VzTXRdIm4AHAd3BLqEfW4tMU+JOmOt+36hrXpVAygPfUIlx43oHenL8E8cPXqtnZyBwR7zsO2MQpxFxIHnzj8aWYBVDyuXaI5+FPQJgkBya2EbEEoqvSlCh4rPd1Aao+zOpXwbym9NVRQqHwf0nQWDsf8F3McBkIMRnhVub2GnDqiBB/AgMBAAECggEAIpM5BXH10qPhXaEZ/cHSWUiEZsymojSMtDBmv04I9x0bpo+BVx+ENeRVhmG6yBrVEBZBpGE0aWbz0rMPm3MKa5AX08rnO/VB5xnjzSdgFQScCIwDvMGnM5WrwWyzIIrybXKhCPAjZ67eLuW0noyDU3X7OHeZLyXYN0VNPRTJ5up6+GEw+7znFkRMu1bMbNTlHrktWjt2iro1F0+SIHpkM2Uq0mjM6+NiOsLVuDwxmKbc8cmHJ3ud8bxDebUPJPcLS4KNr3v/l5MOIwd2XIRP4G1OV+D3ooMG+LGMdF7FuBoI+mMcWSGTsT/wutC4ZT4d91tmmqh6kyTFzhBccBCTKQKBgQD04BVa+RIYcXio4W2i0zScWpM5mnCiqDVt1SqrLd4Sr6wL9V/dekr6wdUzAJkDfDZmP7vvYok93C+fZ3uaaB4HpPcN3usP85V6nx2WPwxRgwWBIT0nEMxVqptW+BIyWd4t2ln8I0RnAETe98e1mVWFHmvB0+jBoD/rxDcNl5nUzQKBgQC+gHPenMhz7fZTwwIxr11HXc55eVZ0nqyO2lP8EhyVGQawI9YaiHFBuPHIy4QVNjWOU3+MGxDtWvxTJEvdJ2rvaMXSgFNtC9GJZMN9phb3hscLqguv/qS2tns31d3RW4thvv64ur+RHo5+zMJGseOOOiqChKcaJqDVTCdltAoaewKBgF+t2spD/Z0NYS7jfhob2jepcFMWlCIKBW5X1ycxcc7tUxUNGBsKuJdH+0zFVAQ3mW3UQ1Nites0yGrJXVNUkT89ZsbFriT6cRKLb10QX3jN7+2nliRYfO6QDcgqf7mqwZQ69+P7x7NTOJXCTQcY1YCBBWujqBNX0QIHFde6v4GVAoGADBhZwvuPWpT0O9M403C/6mURU/MZQyRD7nn8NRftSqYhH5PW5y5cioC0kTwisboUYmn4wiuBwqAxPxIh4vO6vWKYlMnjAhxq/F8ybkraUHL1Nk9gmJcBXDxvzFa+06kNS3J198KboRogj/puJ1DqEsCsrEbB2U40jfZc1f89WPsCgYEAwH3i/1dxKrlT1d3de5WgKpMjEYcoaz63tZnC2IdWZCEEPtwPa0uqflN5w2hHlb46g4o9d/f7oOaGiRKdYbaDQKbjKMM4ucGe+hTn76J8yo5bLUcvOMPjAUKpYriOe7/MnyzDPgDTw1wLOvIHsSFwv9zDX3PpISiWQ70tqRO0Rk0=";
        
        // 生产环境应用公钥（新的，用于验证密钥对是否匹配）
        String publicKeyStr = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtjkuucw6pO/2eoszq6vstkur6I1r34m5uyNzGJ4dN1Gktc62agp8VWgDreIsN7NbnFg7A/2g0WX+LmFXhqxaJH2mm9Fe8dNdSY2jDOQhRtqyg3hLxrbadEChOBvtajXLGXJe+tDNCxofSZivKO4GYaaVeKLrM6yRcyy0eVc010XSJuABwHdwS6hH1uLTFPiTpjrft+oa16VQMoD31CJceN6B3py/BPHD16rZ2cgcEe87DtjEKcRcSB584/GlmAVQ8rl2iOfhT0CYJAcmthGxBKKr0pQoeKz3dQGqPszqV8G8pvTVUUKh8H9J0Fg7H/BdzHAZCDEZ4Vbm9hpw6ogQfwIDAQAB";
        
        System.out.println("=== 测试生产环境密钥 ===");
        System.out.println("私钥长度: " + privateKeyStr.length());
        System.out.println("公钥长度: " + publicKeyStr.length());
        
        // 解析私钥
        byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyStr);
        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
        System.out.println("✅ 私钥解析成功");
        
        // 解析公钥
        byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyStr);
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
        PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
        System.out.println("✅ 公钥解析成功");
        
        // 测试签名和验签
        String testData = "test data for signature verification";
        
        // 用私钥签名
        Signature signer = Signature.getInstance("SHA256withRSA");
        signer.initSign(privateKey);
        signer.update(testData.getBytes());
        byte[] signature = signer.sign();
        System.out.println("✅ 签名成功");
        
        // 用公钥验签
        Signature verifier = Signature.getInstance("SHA256withRSA");
        verifier.initVerify(publicKey);
        verifier.update(testData.getBytes());
        boolean verified = verifier.verify(signature);
        
        System.out.println("\n=== 密钥对验证结果 ===");
        System.out.println("私钥和公钥是否匹配: " + verified);
        
        if (verified) {
            System.out.println("✅ 密钥对匹配！");
        } else {
            System.out.println("❌ 密钥对不匹配！");
            System.out.println("注意：支付宝公钥是支付宝返回的，不是你的应用公钥");
            System.out.println("支付宝公钥用于验证支付宝的响应签名，不是用于验证你的请求签名");
        }
    }
}
