2012-03-19 37 views
0

我想通过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(); 

} 
+0

太模糊。没有人会为你重写你的代码。你有什么尝试? – EJP 2012-03-19 04:13:41

+0

我想知道如何从个人我的商店检索具有密码的特定证书。此代码是从我的商店获取所有证书,但我想获得我想要的特定证书。那么如何编写程序?不要紧,我的代码不一样。 – 2012-03-19 04:27:53

+0

我建议你检查Javadoc。除了遍历整个内容之外,还有其他一些从KeyStore中获取内容的方法。 – EJP 2012-03-19 05:17:50

回答

1

密码

访问证书不是特定的证书。密码是用于密钥的。它与密码是用于模式而不是单个表的数据库类似。

要回答在单个证书上检索的其他问题,为此您需要事先知道别名并使用该别名来检索证书。

在您的代码中它将是ks.getCertifcate("alias")

+0

单个密钥库条目可以有一个密码。 – EJP 2012-03-20 07:55:26

+0

你能发表一些关于这方面的链接吗? – Santosh 2012-03-20 09:08:18

+0

http://docs.oracle.com/javase/6/docs/api/java/security/KeyStore.html#getKey(java.lang.String,%20char []) – EJP 2012-03-20 09:15:50

相关问题