【问题标题】:Could not parse certificate from .cer to import it in keystore: Java无法从 .cer 解析证书以将其导入密钥库:Java
【发布时间】:2017-10-25 14:00:01
【问题描述】:

我想将导出的证书 (.cer) 导入我的应用程序。导出以 PEM、DER 转换类型完成。我已经在在线证书解码器工具上测试了我导出的数据,它显示了很好的结果,但是当我想在我的应用程序中导入它时,我得到了:

java.security.cert.CertificateException:无法解析证书:java.io.IOException:空输入

这是我的来源:

Path fileLocation = Paths.get(file.getAbsolutePath());
byte[] data = Files.readAllBytes(fileLocation);
ByteArrayInputStream bIn = new ByteArrayInputStream(data);
X509Certificate cert = (X509Certificate) 

exception -->> CertificateFactory.getInstance("X.509").generateCertificate(bIn);

keystore.setCertificateEntry(alias, cert);

编辑: 方法适用于 PEM 格式证书,但无法导入 DER。

【问题讨论】:

  • 所以文件是空的。
  • @EJP 其实不是,我已经在 ByteArrayInputStream 上放了断点和数据,它的 buf 是 843 字节长度。

标签: java certificate x509


【解决方案1】:

我发现错误与导出 .DER 类型的证书有关。

这里是固定的.DER导出方法:

FileOutputStream fos = new FileOutputStream(file.getAbsolutePath() + ".cer");
DEROutputStream dos = new DEROutputStream(fos);
ASN1InputStream asn1inputStream = new ASN1InputStream(new ByteArrayInputStream(chain[0].getEncoded())); // this is Certificate
dos.writeObject(asn1inputStream.readObject());
dos.flush();
fos.flush();
dos.close();
fos.close();

上述导入方法适用于 .DER 和 .PEM 证书格式。

【讨论】:

    猜你喜欢
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 2014-02-12
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    相关资源
    最近更新 更多