package top.continew.admin.common.config.properties;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class KeyGenerator {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048);
        KeyPair pair = keyGen.generateKeyPair();
        X509EncodedKeySpec pub = new X509EncodedKeySpec(pair.getPublic().getEncoded());
        PKCS8EncodedKeySpec priv = new PKCS8EncodedKeySpec(pair.getPrivate().getEncoded());

        // 将密钥转换为Base64编码的字符串以便配置文件使用
        String publicKeyStr = Base64.getEncoder().encodeToString(pair.getPublic().getEncoded());
        String privateKeyStr = Base64.getEncoder().encodeToString(pair.getPrivate().getEncoded());

        // 保存到文件或配置文件中
        System.out.println("Public Key: " + publicKeyStr);
        System.out.println("Private Key: " + privateKeyStr);
    }
}
