KeyStore.load方法抛出java.io.IOException:给DerValue构造函数的额外数据

时间:2016-05-27 14:57:08

标签: java security x509certificate x509

我一直在寻找解决方案,但我找不到任何解决方案。 我试图简单地将一个新密钥添加到现有的KeyStore文件中,但它似乎无法添加多个密钥,导致在第二次调用函数时发生此异常,以便在main方法中添加新密钥并在粗体KeyStore.load方法。 这是我的方法:

    String keyPass="pass"; 

    KeyStore keyStore = KeyStore.getInstance("jks");
    char[] storePass = this.keyStorePass.toCharArray();
    FileInputStream is = new FileInputStream(this.keyStoreName);
    **keyStore.load(is,storePass);** 
    is.close();

    Certificate[] certChain = new Certificate[1];  
    certChain[0] = this.certificate;  
    keyStore.setKeyEntry(key, this.pair.getPrivate(), keyPass.toCharArray(), certChain); 

    FileOutputStream fos = new FileOutputStream(this.keyStoreName);
    keyStore.store(fos, storePass);
    fos.close();

以下是例外:

   `java.security.cert.CertificateParsingException: java.io.IOException:
    extra data given to DerValue constructor
   at sun.security.x509.X509CertInfo.<init>(Unknown Source)
   at sun.security.x509.X509CertImpl.parse(Unknown Source)
   at sun.security.x509.X509CertImpl.<init>(Unknown Source)
   at sun.security.provider.X509Factory.engineGenerateCertificate
   at java.security.cert.CertificateFactory.generateCertificate
   at sun.security.provider.JavaKeyStore.engineLoad
   at sun.security.provider.JavaKeyStore$JKS.engineLoad(Unknown Source)
   at java.security.KeyStore.load(Unknown Source)
   at zp2016.StorageClass.addToMyKeyStore(StorageClass.java:76)
   at zp2016.MainProgram.main(MainProgram.java:76)

`

证书链有1个元素,因为应用程序是关于自签名的X509证书。谢谢。

0 个答案:

没有答案