枚举证书
以下几点介绍下面的示例代码的关键部分。
- 调用以获取句柄
CertOpenSystemStore 函数
uint hCertStore = Crypt32.CertOpenSystemStore(0, "My");
用户的个人证书存储区。
- 调用
CertEnumCertificatesInSt ore 函数,以获取证书上下文指针到
while (0 != (pCertContext = Crypt32.CertEnumCertificatesInSt
证书。
- 从创建
x509 证书 对象
X509Certificate x509 = new X509Certificate((IntPtr)pCertContext);
证书上下文指针。
- 调用
CertDuplicateCertificate Context 函数以增加引用计数在证书上下文指针,以便您可以稍后使用该值
CertDuplicateCertificate
您可以使用下面的代码来枚举证书。
若要汇总该代码,您枚举
private void button1_Click(object sender, System.EventArgs e)
{
listView1.Items.Clear();
uint hCertStore = Crypt32.CertOpenSystemStore(0, "My");
if (hCertStore == 0)
{
MessageBox.Show("CertOpenSystemStore failed: " + Marshal.GetLastWin32Error().ToString());
return;
}
uint pCertContext = Crypt32.CertEnumCertificatesInSt
while (pCertContext != 0)
{
X509Certificate x509 = new X509Certificate((IntPtr)pCertContext);
ListViewItem NewItem = listView1.Items.Add(x509.GetName());
NewItem.SubItems.Add(x509.GetIssuerName());
// Increment the reference count so that you can use this value later.
NewItem.Tag = Crypt32.CertDuplicateCertificate
pCertContext = Crypt32.CertEnumCertificatesInSt
}
}
◆CAPI读取证书总结
系统证书库中存在的证书按下面的方法打开
1.CryptAcquireContext获取HCRYPTPROV
2.CertOpenStore得到证书库句柄HCERTSTORE
3.CertFindCertificateInSto
如果X509格式的文件证书
1.首先读入文件二进制byte
2.CertCreateCertificateCon
http://chinaitpower.com/A200507/2005-07-27/175806.html