gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:29:13 25 4
gpt4 key购买 nike

我想通过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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com