views:

131

answers:

1
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?

Error message:

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

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

For generating a 2048 bit RSA key:

rsaKeyPairGen.init( new KeyGenerationParameters( new SecureRandom(), 2048 ) );
tangens
almost there, your code is throwing:java.lang.ClassCastException: org.bouncycastle.crypto.KeyGenerationParameters cannot be cast to org.bouncycastle.crypto.params.RSAKeyGenerationParameters
Tom Brito
and there is a lot of arguments to create a RSAKeyGenerationParameters.. I'll read about.
Tom Brito
it works with KeyPairGenerator.getInstance("RSA", "BC"); Thanks anyway.
Tom Brito