Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用BouncyCastle API生成RSA密钥时出错_Java_Bouncycastle - Fatal编程技术网

Java 使用BouncyCastle API生成RSA密钥时出错

Java 使用BouncyCastle API生成RSA密钥时出错,java,bouncycastle,Java,Bouncycastle,我有一个非常简单和简短的程序:- Security.addProvider(new BouncyCastleProvider()); RSAKeyPairGenerator kpg = new RSAKeyPairGenerator(); kpg.init(new KeyGenerationParameters(new SecureRandom(), 2048)); kpg.generateKeyPair(); 根据BoucnyCastleAPI,我应该得到一个RSA密钥对。相反,我得到的是:

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

Security.addProvider(new BouncyCastleProvider());
RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();
kpg.init(new KeyGenerationParameters(new SecureRandom(), 2048));
kpg.generateKeyPair();
根据BoucnyCastleAPI,我应该得到一个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后面。

异常在“init”方法中引发。
KeyGenerationParameters
可能应该是抽象类或接口。假设它是,将
RSAKeyGenerationParameters
的实例提供给
kpg.init()
方法。