簡體   English   中英

使用BouncyCastle API的RSA密鑰生成錯誤

[英]RSA key generation error using BouncyCastle API

我有一個非常簡單而簡短的程序:

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

根據BoucnyCastle API,因此,我應該獲得一個RSA密鑰對。 相反,我得到:

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的后面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM