gpt4 book ai didi

java - 使用 BouncyCaSTLe API 时出现 RSA key 生成错误

转载 作者:行者123 更新时间:2023-11-30 02:14:32 25 4
gpt4 key购买 nike

我有一个非常简单且简短的程序:-

Security.addProvider(new BouncyCastleProvider());
RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();
kpg.init(new KeyGenerationParameters(new SecureRandom(), 2048));
kpg.generateKeyPair();

根据 BoucnyCaSTLe API,我应该因此获得 RSA key 对。相反,我得到:-

Exception in thread "main" java.lang.ClassCastException: org.bouncycastle.crypto.KeyGenerationParameters cannot be cast to org.bouncycastle.crypto.params.RSAKeyGenerationParameters
at org.bouncycastle.crypto.generators.RSAKeyPairGenerator.init(Unknown Source)

有什么想法吗?谢谢。

最佳答案

您正在使用特定于提供者的生成器,因此您也必须使用特定于提供者的参数类。或者以独立于提供商的方式使用 JCE API:

KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", "BC");
gen.initialize(2048, new SecureRandom());
KeyPair kp = gen.generateKeyPair();

这仍然使用 BouncyCaSTLe,但所有提供商特定的内容都隐藏在 JCE API 后面。

关于java - 使用 BouncyCaSTLe API 时出现 RSA key 生成错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49011926/

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