【问题标题】:NullPointerException when generating RSA keys with BouncyCastle使用 BouncyCastle 生成 RSA 密钥时出现 NullPointerException
【发布时间】:2010-05-20 19:15:49
【问题描述】:
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)

【问题讨论】:

    标签: java rsa nullpointerexception bouncycastle


    【解决方案1】:

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

    用于生成 2048 位 RSA 密钥:

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

    【讨论】:

    • 差不多了,你的代码正在抛出:java.lang.ClassCastException: org.bouncycastle.crypto.KeyGenerationParameters cannot be cast to org.bouncycastle.crypto.params.RSAKeyGenerationParameters
    • 创建 RSAKeyGenerationParameters 有很多参数。我会读一下。
    • 它与 KeyPairGenerator.getInstance("RSA", "BC");还是谢谢。
    • 但是 KeyPairGenerator 不是充气城堡类。知道如何在不使用它的情况下解决强制转换异常吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 2015-06-18
    • 1970-01-01
    • 2012-03-20
    • 2011-01-07
    • 1970-01-01
    • 2021-10-24
    相关资源
    最近更新 更多