从密钥库加载证书

时间:2013-02-01 00:24:06

标签: java certificate keystore jks

我有一个Base-64编码的X.509(.CER)证书,它在下面的java代码中导致NullPointerException。引起它的行是PublicKey pubKey = pubCert.getPublicKey();

有没有办法验证myfile.keystore是否有“xyz”证书?

final FileInputStream keyFile = new FileInputStream(filePath
                    + "myfile.keystore");
final KeyStore keyStore = KeyStore.getInstance("JKS");
String storepass = "mypass";
keyStore.load(keyFile, storepass.toCharArray());
Certificate pubCert = keyStore.getCertificate("xyz");
PublicKey pubKey = pubCert.getPublicKey();

4 个答案:

答案 0 :(得分:1)

尝试应该在JRE或JDK bin目录中的keytool命令,请参阅下面的输出:

@raspbmc:~$ /opt/jdk1.8.0/bin/keytool  -list -help
keytool -list [OPTION]...

Lists entries in a keystore

Options:

 -rfc                            output in RFC style
 -alias <alias>                  alias name of the entry to process
 -keystore <keystore>            keystore name
 -storepass <arg>                keystore password
 -storetype <storetype>          keystore type
 -providername <providername>    provider name
 -providerclass <providerclass>  provider class name
 -providerarg <arg>              provider argument
 -providerpath <pathlist>        provider classpath
 -v                              verbose output
 -protected                      password through protected mechanism

Use "keytool -help" for all available commands

参考:

http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/keytool.html

答案 1 :(得分:1)

您能否确认密钥库位于类路径上?,

如上所述,如果您运行 keytool -list -v -keystore .jks ,您将看到密钥库中安装的证书。

如果您在密钥库中看到证书但仍然遇到错误,可能是因为证书没有正确链接,我以前遇到过这个问题。 如果您仍然遇到问题,请粘贴上述命令的输出,这将有助于我们解决您的问题

由于

答案 2 :(得分:0)

使用工具打开密钥库,例如eclipse插件或使用http://jxplorer.org/downloads/index.html

答案 3 :(得分:0)

编辑此行

PublicKey pubKey = pubCert.getPublicKey();

PublicKey pubKey = keyStore.getPublicKey();