Search code examples
javacertificatex509certificatekeystoretruststore

Delete Certificate from Truststore programmatical


I try to delete a certificate from a Truststore programmatical in JAVA.

public static void deleteCertificate(final File trustStore, final String password, final String alias) {
    try (final FileInputStream fis = new FileInputStream(trustStore)) {
        final KeyStore keystore = KeyStore.getInstance("UBER");
        keystore.load(fis, password.toCharArray());
        if (keystore.containsAlias(alias)) {
            keystore.deleteEntry(alias);
        }
        else {
            throw new IllegalStateException("Alias " + alias + " not found in trust store");
        }
    }
    catch (final Exception e) {
        throw new IllegalStateException("Error occures while deleting certificate.", e);

The Programm pass the line keystore.deleteEntry(alias), but the certificate is still in the trust store. No Error occures, the application runs through without any problems.

What do I wrong?

Thanks in advance. :-)


Solution

  • Try to store the result

     OutputStream writeStream = new FileOutputStream(filePathToStore);
     keystore.store(writeStream, password);
     writeStream.close();