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

时间:2010-05-20 19:15:50

标签: java rsa nullpointerexception bouncycastle

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

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

错误讯息:

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

1 个答案:

答案 0 :(得分:3)

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

用于生成2048位RSA密钥:

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