如何阅读pkcs12文件内容?

时间:2018-02-14 00:16:45

标签: java bouncycastle keystore pkcs#12

我正在尝试从pkcs12(.p12)文件加载KeyStore,但是当我调用KeyStore.load(流,密码)时,JVM会抛出RuntimeExcepction:创建Key时出错,我可以在pkcs12上写入我甚至可以在导航器上导入它,但我无法在我的代码中读取它。

将KeyStore保存在磁盘上的代码:

public static void gerarPkcs12(PrivateKey chavePrivada, X509Certificate certificado, String caminhoPkcs12,
        String alias, char[] senha) {
        try {
    KeyStore repositorio = KeyStore.getInstance("PKCS12", "BC");
            repositorio.load(null, senha);
            Certificate[] chain = {certificado};
            repositorio.setKeyEntry(alias, chavePrivada, senha, chain);
            FileOutputStream fos = new FileOutputStream(caminhoPkcs12);
            repositorio.store(fos, senha);
        } catch(KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException | NoSuchProviderException ex) {
            System.out.println(ex.getMessage());
        }
}

尝试从文件中读取它的代码

 80. KeyStore ks = KeyStore.getInstance("PKCS12", "BC");
 81. FileInputStream in = new FileInputStream(caminhoPkcs12Usuario);
 82. BufferedInputStream bis = new BufferedInputStream(in);
 83. ks.load(bis, "1234".toCharArray());

StackTrace

Exception in thread "main" java.lang.RuntimeException: error creating key
at org.bouncycastle.jcajce.provider.keystore.pkcs12.PKCS12KeyStoreSpi.createSubjectKeyId(Unknown Source)
at org.bouncycastle.jcajce.provider.keystore.pkcs12.PKCS12KeyStoreSpi.access$100(Unknown Source)
at org.bouncycastle.jcajce.provider.keystore.pkcs12.PKCS12KeyStoreSpi$CertId.<init>(Unknown Source)
at org.bouncycastle.jcajce.provider.keystore.pkcs12.PKCS12KeyStoreSpi.engineLoad(Unknown Source)
at java.security.KeyStore.load(KeyStore.java:1445)
at etapas.QuartaEtapa.main(QuartaEtapa.java:83)

非常感谢。

0 个答案:

没有答案