📄 c.txt
字号:
1using System;
2using System.Runtime.InteropServices;
3
4namespace X509Cert
5{
6
7 public class WIN32
8 {
9 public const uint CRYPT_USER_KEYSET = 0x00001000;
10 public const uint CERT_KEY_PROV_INFO_PROP_ID = 0x00000002;
11 public const uint CRYPT_DELETEKEYSET = 0x00000010;
12
13 [DllImport("crypt32.dll", SetLastError=true)]
14 public static extern IntPtr PFXImportCertStore(ref CRYPT_DATA_BLOB pPfx,[MarshalAs(UnmanagedType.LPWStr)] String szPassword,uint dwFlags);
15
16 [DllImport("CRYPT32.DLL", EntryPoint="CertEnumCertificatesInStore", CharSet=CharSet.Auto, SetLastError=true)]
17 public static extern IntPtr CertEnumCertificatesInStore( IntPtr storeProvider, IntPtr prevCertContext);
18
19 [DllImport("CRYPT32.DLL",CharSet=CharSet.Auto, SetLastError=true)]
20 public static extern bool CertGetCertificateContextProperty(IntPtr pCertContext,uint dwPropId,IntPtr pvData,ref uint pcbData);
21
22 [DllImport("advapi32.dll",EntryPoint="CryptAcquireContext",CharSet=CharSet.Auto, SetLastError=true)]
23 public static extern bool CryptAcquireContext(ref IntPtr phProv,string szContainer,string szProvider,uint dwProvType,uint dwFlags);
24
25 [StructLayout(LayoutKind.Sequential)]
26 public struct CRYPT_DATA_BLOB {
27 public int cbData;
28 public IntPtr pbData;
29 }
30
31 [StructLayout(LayoutKind.Sequential)]
32 public struct CRYPT_KEY_PROV_INFO {
33
34 [MarshalAs(UnmanagedType.LPWStr)]
35 public String ContainerName;
36
37 [MarshalAs(UnmanagedType.LPWStr)]
38 public String ProvName;
39
40 public uint ProvType;
41
42 public uint Flags;
43
44 public uint ProvParam;
45
46 public IntPtr rgProvParam;
47
48 public uint KeySpec;
49
50 }
51
52 public WIN32()
53 {
54 //
55 // TODO: 在此处添加构造函数逻辑
56 //
57 }
58 }
59}
60
然后在Cert类中写一个Read方法读取其中的证书。注意:pfx文件有可能包含几个证书
1using System;
2using System.IO;
3using System.Runtime.InteropServices;
4using System.Security.Cryptography.X509Certificates;
5
6namespace X509Cert
7{
8 /**//// <summary>
9 /// Cert 的摘要说明。
10 /// </summary>
11 public class Cert
12 {
13 public Cert()
14 {
15 //
16 // TODO: 在此处添加构造函数逻辑
17 //
18 }
19 public static System.Security.Cryptography.X509Certificates.X509Certificate[] Read(string filename,string password) {
20
21 //打开证书文件,并读到一个字节数组中。
22 FileStream stream = new FileStream(filename,FileMode.Open);
23 byte[] buffer = new byte[stream.Length];
24 stream.Read(buffer,0,buffer.Length);
25 stream.Close();
26
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -