【问题标题】:Get the Entry type of a KeyStore programatically以编程方式获取 KeyStore 的 Entry 类型
【发布时间】:2020-07-21 23:49:19
【问题描述】:

使用 keytool 命令,我们有这样的信息:

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

Alias name: myname
Creation date: 21-Aug-2011
Entry type: PrivateKeyEntry
Certificate chain length: 1
...

在 Java 中(以编程方式),我如何检索“条目类型”值以了解它是私有证书还是公共证书?我以这种方式使用 KeyStore Java 类:

File file = new File(filePath);
String password = password.toCharArray();
KeyStore keyStore = KeyStore.getInstance(format);
keyStore.load(new FileInputStream(file), password);

【问题讨论】:

    标签: java keystore keytool jks


    【解决方案1】:

    您需要做的是检查 KeyStore 中给定别名的 KeyEntry 是 PrivateKeyEntry 还是 TrustedCertificateEntry。

    char[] password = "mypassword";
    
    ProtectionParameter passwordProtection = new KeyStore.PasswordProtection(password.toCharArray());
    
    KeyEntry entry = keystore.getEntry("myname", passwordProtection);
    
    if (entry instanceof PrivateKeyEntry) {
        // is a private key entry
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-12
      • 1970-01-01
      • 2020-03-19
      • 2015-06-21
      • 2020-10-26
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      • 2020-11-07
      相关资源
      最近更新 更多