![](/img/trans.png)
[英]How do I RSA encrypt a string with a plaintext key using Java BouncyCastle API on Android
[英]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.