package org.lingniu.idp.utils.jwt;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;

public class RsaKeyGenerator {
    public static void main(String[] args) throws Exception {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048); // 密钥长度
        KeyPair keyPair = keyGen.generateKeyPair();
        
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();
        
        // 输出Base64编码的密钥
        System.out.println("=== 私钥（用于签名） ===");
        System.out.println(Base64.getEncoder().encodeToString(privateKey.getEncoded()));
        
        System.out.println("\n=== 公钥（用于验证） ===");
        System.out.println(Base64.getEncoder().encodeToString(publicKey.getEncoded()));
        
        // 也可以输出PEM格式
        System.out.println("\n=== PEM格式私钥 ===");
        System.out.println("-----BEGIN PRIVATE KEY-----");
        System.out.println(Base64.getEncoder().encodeToString(privateKey.getEncoded()));
        System.out.println("-----END PRIVATE KEY-----");
    }
}