如何用javacode向truststore jks文件添加ssl证书链

cgfeq70w  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(290)

我必须写一个java代码,以便添加到信任库ca证书和会话证书没有私钥。我有代码可以将私钥和证书链添加到密钥存储中,但是如果没有私钥,就不可能将它们添加到信任存储中。有一个将证书链和私钥添加到密钥存储的代码:

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);
            }
        }
    }       
}

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题