我想通过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();
}
太模糊。没有人会为你重写你的代码。你有什么尝试? – EJP 2012-03-19 04:13:41
我想知道如何从个人我的商店检索具有密码的特定证书。此代码是从我的商店获取所有证书,但我想获得我想要的特定证书。那么如何编写程序?不要紧,我的代码不一样。 – 2012-03-19 04:27:53
我建议你检查Javadoc。除了遍历整个内容之外,还有其他一些从KeyStore中获取内容的方法。 – EJP 2012-03-19 05:17:50