随着《电子签名法》的颁布,数字证书应用越来越广泛,在一般的应用中,我们都是在系统中安装pkcs12格式的证书。在访问应用(一般是网页、电子邮件等)时,选择合适的证书。我们也可以使用编程来直接读取证书文件。下面我们就介绍如何使用.net读取数字证书。
要读取pkcs12格式的证书,我们需要调用API,在WIN32类中,我们声明这些API的引用:
1
using System;
2
using System.Runtime.InteropServices;
3
4
namespace X509Cert
5
2
3
4
5
然后在Cert类中写一个Read方法读取其中的证书。注意:pfx文件有可能包含几个证书
1
using System;
2
using System.IO;
3
using System.Runtime.InteropServices;
4
using System.Security.Cryptography.X509Certificates;
5
6
namespace X509Cert
7
2
3
4
5
6
7