Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.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生成RSA密钥时出现NullPointerException_Java_Rsa_Nullpointerexception_Bouncycastle - Fatal编程技术网

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

Java 使用BouncyCastle生成RSA密钥时出现NullPointerException,java,rsa,nullpointerexception,bouncycastle,Java,Rsa,Nullpointerexception,Bouncycastle,rsakepairgen不是null,但是generateKeyPair()方法正在抛出NullPointerException。可能出了什么问题 错误消息: public static void main(String[] args) throws Exception { RSAKeyPairGenerator rsaKeyPairGen = new RSAKeyPairGenerator(); AsymmetricCipherKeyPair keyPair = rsaKeyP

rsakepairgen
不是null,但是
generateKeyPair()
方法正在抛出NullPointerException。可能出了什么问题

错误消息:

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

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

要生成2048位RSA密钥,请执行以下操作:

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

几乎在这里,您的代码抛出:java.lang.ClassCastException:org.bouncycastle.crypto.KeyGenerationParameters不能强制转换为org.bouncycastle.crypto.params.RSAKeyGenerationParameters,并且有许多参数可以创建RSAKeyGenerationParameters。。我将阅读关于。它与KeyPairGenerator.getInstance(“RSA”、“BC”)一起工作;无论如何,谢谢。但是KeyPairGenerator不是一个有弹性的城堡职业。知道如何在不使用强制转换异常的情况下解决强制转换异常吗?
rsaKeyPairGen.init( new KeyGenerationParameters( new SecureRandom(), 2048 ) );