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

时间:2021-11-16 18:27:28
public static void main(String[] args) throws Exception {
    RSAKeyPairGenerator rsaKeyPairGen = new RSAKeyPairGenerator();
    AsymmetricCipherKeyPair keyPair = rsaKeyPairGen.generateKeyPair();
}

the rsaKeyPairGen is not null, but the generateKeyPair() method is throwing NullPointerException. What may be wrong?

rsaKeyPairGen不为null,但generateKeyPair()方法抛出NullPointerException。可能有什么不对?

Error message:

错误信息:

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

1 个解决方案

#1


3  

You have to specify the bit length and the random number generator you want to use for the key (see the javadoc):

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

For generating a 2048 bit RSA key:

用于生成2048位RSA密钥:

rsaKeyPairGen.init( new KeyGenerationParameters( new SecureRandom(), 2048 ) );

#1


3  

You have to specify the bit length and the random number generator you want to use for the key (see the javadoc):

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

For generating a 2048 bit RSA key:

用于生成2048位RSA密钥:

rsaKeyPairGen.init( new KeyGenerationParameters( new SecureRandom(), 2048 ) );