【问题标题】:Loading a Certificate from a Keystore从密钥库加载证书
【发布时间】:2013-01-16 06:09:21
【问题描述】:

我有一个 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();

【问题讨论】:

    标签: java certificate keystore jks


    【解决方案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

    【讨论】:

    • 别名已更改,这有助于我了解这一点。
    【解决方案2】:

    您能否确认密钥库在类路径中?,

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

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

    谢谢

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        编辑这一行

        PublicKey pubKey = pubCert.getPublicKey();

        PublicKey pubKey = keyStore.getPublicKey();

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-16
          • 1970-01-01
          • 2017-02-05
          • 2011-08-17
          • 2011-01-04
          • 1970-01-01
          相关资源
          最近更新 更多