如何从我的个人商店检索证书

时间:2012-03-19 03:58:12

标签: java servlets x509certificate

我想通过java编程从个人商店检索带密码的证书。 我找到了一些检索证书的代码,但它显示了所有证书。这些证书显示的数据不需要打开这些相关的密码。 我不想这些风格的证书。我想编写代码格式类型is-选择我想要的证书,然后在浏览器中添加此证书的密码,然后显示此证书信息。

 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();

 }

1 个答案:

答案 0 :(得分:1)

密码不是特定于证书的。密码用于密钥库。它类似于数据库,其中密码是针对模式而不是单独的表。

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

代码中的

将是ks.getCertifcate("alias")