作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想通过java编程从我的个人商店检索带有密码的证书。我找到了一些检索证书的代码,但它显示了所有证书。这些证书显示的数据不需要使用这些相关密码打开。我不想以这些方式展示证书。我想编写的代码格式类型是 - 选择我想要的证书,然后在浏览器上添加该证书的密码,然后显示该证书信息。
KeyStore ks = KeyStore.getInstance("Windows-MY");
ks.load(null, null) ;
Enumeration en = ks.aliases() ;
while (en.hasMoreElements()) {
String aliasKey = (String)en.nextElement() ;
Certificate c = ks.getCertificate(aliasKey) ;
System.out.println("---> alias : " + aliasKey) ;
if (ks.isKeyEntry(aliasKey)) {
Certificate[] chain = ks.getCertificateChain(aliasKey);
System.out.println("---> chain length: " + chain.length);
X509Certificate Cert = null;
for (Certificate cert: chain) {
System.out.println(cert);
}
}
}
如何修复此代码?我找到了一些用于访问证书的 C# 代码。我也想通过java程序像这样使用。如何将下面的C#代码转换为java代码?
通过C#访问证书
X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection col = store.Certificates.Find(X509FindType.FindBySerialNumber, "{serial number no space}", true);
//service is the webservice that need to //be authenticated using X509 certificate
TestWebService service = new TestWebService();
//Note, we should find the certificate from the the
//root certificate store on local machine if the
//certificate is imported correctly and the serial
//number is correct
if (col.Count == 1)
{
//all we need to do is to add the certificate
//after that we can use the webservice as usual
service.ClientCertificates.Add(col[0]);
service.Test();
}
最佳答案
密码不是特定于证书的。该密码用于 keystore 。它类似于数据库,其中密码适用于模式而不是单个表。
要回答检索单个证书的其他问题,您需要事先知道别名并使用该别名来检索证书。
在您的代码中,它将是 ks.getCertifcate("alias")
关于java - 如何从个人我的商店检索证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9764859/
我是一名优秀的程序员,十分优秀!