枚举证书和 CAPI读取证书总结

15 篇文章 0 订阅

枚举证书

以下几点介绍下面的示例代码的关键部分。

  • 调用以获取句柄 CertOpenSystemStore 函数

uint hCertStore = Crypt32.CertOpenSystemStore(0, "My");

用户的个人证书存储区。

  • 调用 CertEnumCertificatesInStore 函数,以获取证书上下文指针到

while (0 != (pCertContext = Crypt32.CertEnumCertificatesInStore(hCertStore, (uint)pCertContext)))

证书。

  • 从创建 x509 证书 对象

X509Certificate x509 = new X509Certificate((IntPtr)pCertContext);

证书上下文指针。

  • 调用 CertDuplicateCertificateContext 函数以增加引用计数在证书上下文指针,以便您可以稍后使用该值

CertDuplicateCertificateContext((IntPtr)pCertContext);

您可以使用下面的代码来枚举证书。

若要汇总该代码,您枚举 "my" 证书存储区中的证书、 填充 ListView 控件与每个的证书的主题和颁发者名称,然后附加到 ListView 的每个项的 Tag 属性的证书上下文的指针

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.CertEnumCertificatesInStore(hCertStore, (uint)0);

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.CertDuplicateCertificateContext(pCertContext);

pCertContext = Crypt32.CertEnumCertificatesInStore(hCertStore, pCertContext);

}

}

 

◆CAPI读取证书总结

系统证书库中存在的证书按下面的方法打开
1.CryptAcquireContext获取HCRYPTPROV
2.CertOpenStore得到证书库句柄HCERTSTORE
3.CertFindCertificateInStore得到PCCERT_CONTEXT
如果X509格式的文件证书
1.首先读入文件二进制byte
2.CertCreateCertificateContext获取PCCERT_CONTEXT

http://chinaitpower.com/A200507/2005-07-27/175806.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值