Search code examples
javabouncycastlekeystore

Read JKS with BouncyCastle


I have a Java KeyStore (JKS) and I need to read it with BouncyCastle.

I've added BC provider at the top of providers list:

Security.insertProviderAt(new BouncyCastleProvider(), 1);

If I create KeyStore this way:

final KeyStore keystore = KeyStore.getInstance("JKS", "BC");

I get an error:

java.security.KeyStoreException: JKS not found

If I don't specify a provider, the KeyStore will be created with Sun provider and keystore.aliases() will contain EmptyEnumeration.

As I saw in this topic, BouncyCastle can work with JKS

How can I read JKS with BouncyCastle?


Solution

  • Use BKS instead of JKS

     KeyStore keystore = KeyStore.getInstance("BKS", "BC");
    

    See section 6.4-Keystore of https://www.bouncycastle.org/specifications.html

    The Bouncy Castle package has three implementation of a keystore. The first "BKS" is a keystore that will work with the keytool in the same fashion as the Sun "JKS" keystore.

    The result will be the same as the Sun provider. If you get an empty list, check the JKS is not empty and you are reading It properly