英文:
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中给定别名的KeyEntry
是PrivateKeyEntry
还是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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论