如何使用 Javacode 将 SSL 证书链添加到 TrustStore jks 文件

问题描述

我必须编写一个 java 代码才能添加到 TrustStore CA 证书和没有私钥的会话证书。我有可以将私钥和证书链添加到密钥库的代码,但是如果没有私钥就不可能将它们添加到信任库。有一个代码可以将 Certificateschain 和 Private Key 添加到 Key Store:

public static void writeKeyStoreFile(String keyStoreFileName,String password,String alias,PrivateKey privateKey,Certificate... certificates) {
    FileOutputStream fos = null;
    try {
        final KeyStore ks = readKeyStore(keyStoreFileName,password);
        /*
         * If keystore is of type {@code jks} {@code key} must be encoded as
         * {@code EncryptedPrivateKeyInfo}
         */
        ks.setKeyEntry(alias,privateKey,password.tochararray(),certificates);// certificates - welche Certificate wo eingesetzt wird?

        System.out.println("keyStoreFileName fuer pushKey2Cert: " + keyStoreFileName);
        fos = new FileOutputStream(new File(keyStoreFileName));
        ks.store(fos,password.tochararray());
        fos.flush();
    } catch (final Exception e) {// nur warn!!!
        throw new RuntimeException(KEYSTORE_FEHLER + "bei pushKey2Cert in " + keyStoreFileName + " / Alias " + alias
                + " / Passwort *****",e);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (final IOException e) {
                throw new RuntimeException(KEYSTORE_FEHLER + "bei pushKey2Cert in  " + keyStoreFileName
                        + " / Alias " + alias + " / Passwort *****",e);
            }
        }
    }       
}

如果有人有好主意,我会很感激的。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)