2016-11-29 171 views
0

我有一个Java KeyStore(JKS),我需要阅读它与BouncyCastle。阅读JKS与BouncyCastle

我添加BC供应商在供应商列表的顶部:

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

如果我创建密钥库是这样的:

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

我得到一个错误:

java.security.KeyStoreException: JKS not found

如果我没有指定供应商,将使用Sun供应商创建KeyStore,并且keystore.aliases()将包含EmptyEnumeration

正如我在this话题看到,BouncyCastle的可JKS

工作,我怎样才能读取BouncyCastle的JKS?

+1

你是否在程序的顶部添加了以下行:'Security.addProvider(new BouncyCastleProvider());'?参考[6.0节](https://www.bouncycastle.org/specifications.html)。 –

+0

@ Mr.Polywhirl是的。我编辑了说明 – Kirill

+1

您很困惑“使用”和“实施”。您通常不应该在JCE的任何'getInstance()'方法中指定提供者。只需使用'Security.addProvider()'添加提供程序,让JCE找到任何提供程序的实现。只有Oracle提供者实现了JKS密钥库,但是你的'KeyStore.getInstance(“JKS”,“BC”);'强制JCE只查看BouncyCastle的JKS实现,它没有。另外,除非你真的知道你在做什么,否则不要在特定位置添加BouncyCastle提供程序。 –

回答

2

使用BKS而不是JKS

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

见节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.

结果6.4密钥库将是一样的Sun提供者。如果你得到一个空的列表,检查JKS不是空的,你正在正确地阅读它

+0

我试过这个,在这种情况下'keystore.aliases()'返回'EmptyEnumeration' – Kirill

+1

您的密钥库是否有证书?使用'keytool -list -v -keystore keystore.jks'检查你是否以这种方式读取密钥仓库?:'keystore.load(inputStream,password);' – pedrofb

+0

是的,我可以使用keytool以相同的参数看到此证书 – Kirill