使用 BouncyCastle 生成 RSA 密钥时出现 NullPointerException

发布于 2024-09-02 21:02:29 字数 522 浏览 2 评论 0原文

public static void main(String[] args) throws Exception {
    RSAKeyPairGenerator rsaKeyPairGen = new RSAKeyPairGenerator();
    AsymmetricCipherKeyPair keyPair = rsaKeyPairGen.generateKeyPair();
}

rsaKeyPairGen 不为 null,但 generateKeyPair() 方法抛出 NullPointerException。可能出了什么问题?

错误信息:

java.lang.NullPointerException
at org.bouncycastle.crypto.generators.RSAKeyPairGenerator.generateKeyPair(Unknown Source)
at pkg.main(Main.java:154)
public static void main(String[] args) throws Exception {
    RSAKeyPairGenerator rsaKeyPairGen = new RSAKeyPairGenerator();
    AsymmetricCipherKeyPair keyPair = rsaKeyPairGen.generateKeyPair();
}

the rsaKeyPairGen is not null, but the generateKeyPair() method is throwing NullPointerException. What may be wrong?

Error message:

java.lang.NullPointerException
at org.bouncycastle.crypto.generators.RSAKeyPairGenerator.generateKeyPair(Unknown Source)
at pkg.main(Main.java:154)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2024-09-09 21:02:29

您必须指定要用于密钥的位长度和随机数生成器(请参阅 javadoc):

用于生成 2048 位 RSA 密钥:

rsaKeyPairGen.init( new KeyGenerationParameters( new SecureRandom(), 2048 ) );

You have to specify the bit length and the random number generator you want to use for the key (see the javadoc):

For generating a 2048 bit RSA key:

rsaKeyPairGen.init( new KeyGenerationParameters( new SecureRandom(), 2048 ) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文