Reputation: 361
I want to retrieve certificate with password from personal my store by java programming. I found some code of retrieving certificate but it shows all certificates. These certificates shown data didn't need to open with these related password. I do not want to these style of showing certificate. I want to write the code format type is- choose certificate I want and I add password of this certificate on the browser and then show of this certificate information.
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);
}
}
}
How to repair this code? And I found some C# code for accessing certificate. I wanna also use just like this by java program. How to convert the following C# code to java code?
Access certificate by 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();
}
Upvotes: 0
Views: 3117
Reputation: 17903
The password is not certificate specific. The password is for the keyestore. Its similar to the database where in the password is for a schema and not individual tables.
To answer other question of retrieving on a single certificate, for that you would need to know the alias beforehand and use that alias to retrieve the certificate.
in your code it would be ks.getCertifcate("alias")
Upvotes: 1