以编程方式获取密钥库的条目类型

huangapple go评论55阅读模式
英文:

Get the Entry type of a KeyStore programatically

问题

使用keytool命令,我们有以下信息:

密钥库类型:JKS
密钥库提供程序:SUN

您的密钥库包含1个条目

别名:myname
创建日期:2011年8月21日
条目类型:PrivateKeyEntry
证书链长度:1
...

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

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

With the keytool command, we have this kind of information:

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

In Java (programatically), how can I retrieve the "Entry type" value to know if it's a private certificate or a public? I'm using the KeyStore Java class this way:

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

答案1

得分: 2

你需要做的是检查KeyStore中给定别名的KeyEntryPrivateKeyEntry还是TrustedCertificateEntry

char[] password = "mypassword".toCharArray();

ProtectionParameter passwordProtection = new KeyStore.PasswordProtection(password);

KeyEntry entry = keystore.getEntry("myname", passwordProtection);

if (entry instanceof PrivateKeyEntry) {
    // 是私钥条目
}
英文:

What you need to do is check if the KeyEntry for the given alias in the KeyStore is a PrivateKeyEntry or a 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
}

huangapple
  • 本文由 发表于 2020年7月21日 22:28:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63016783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定