gpt4 book ai didi

Java 从 KeyPair 对象中检索公钥的实际值

转载 作者:行者123 更新时间:2023-12-02 00:48:05 30 4
gpt4 key购买 nike

我想问如何从 KeyPair 对象中检索私钥和公钥的实际值,因为我需要导出它们并保存在数据库中。

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair keyPair = kpg.genKeyPair();

System.out.println("Public key " + keyPair.getPublic());
System.out.println("Private key " + keyPair.getPrivate());

输出为:

Public key Sun RSA public key, 1024 bits
modulus: 105712092415375085805423498639048173422142354311030811647243014925610093650322108853068042919471115278002432342007597147610508132502035047888382465733153739247741208519707861808073276783311634229563965825609200080862631487160732889423591650215084096832366499080850540875321197564283324922935557797293830551071
public exponent: 65537
Private key sun.security.rsa.RSAPrivateCrtKeyImpl@35e71

最佳答案

您可以简单地向下转换为 RSAPrivateKeyRSAPublicKey。完成后,您可以访问这些类的方法。

RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic());
BigInteger modulus = pubKey.getModulus();

等等

关于Java 从 KeyPair 对象中检索公钥的实际值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19818550/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com