Original article: http://hi.baidu.com/lidhcn/item/5476d292a0710eda1a49df60
1. Read certificate data from the certificate file on the disk
Unsigned char * pbx509data; // certificate data
Unsigned long ulx509datalen; // Certificate Data Length
2. Get certcontext
Pccert_context pcertcontext = certcreatecertificatecontext (x509_asn_encoding, pbx509data, ulx509datalen );
3. Obtain certificate information
Pcertcontext-> pcertinfo-> dwversion; // certificate version
Crypt_integer_blob snblob = pcertcontext-> pcertinfo-> serialnumber; // certificate Sn
Cert_name_blob issuerblob = pcertcontext-> pcertinfo-> issuer; // Certificate Issuer
Cert_name_blob subjectblob = pcertcontext-> pcertinfo-> subject; // certificate topic
// Certificate validity Start Date
Systemtime receivime;
Memset (& systime, 0, sizeof (systime ));
Filetimetosystemtime (& pcertcontext-> pcertinfo-> notbefore, & policime );
Char sztime [128] = {0 };
Sprintf_s (sztime, 128, "% d: % d", systime. wyear, too ime. wmonth, too ime. wday, too ime. whour, mongoime. wminute, policime. wsecond );
// Valid end date of Certificate
Memset (& systime, 0, sizeof (systime ));
Filetimetosystemtime (& pcertcontext-> pcertinfo-> notafter, & systime );
Memset (sztime, 0, sizeof (sztime ));
Sprintf_s (sztime, 128, "% d: % d", systime. wyear, too ime. wmonth, too ime. wday, too ime. whour, mongoime. wminute, policime. wsecond );
4. Create a temporary key container
Hcryptprov htmpprov = NULL;
Cryptacquirecontext (& htmpprov, "my_temporary_container", null, prov_rsa_aes, 0); // null indicates using the system default CSP
5. Import the public key to the container to obtain the public key handle.
Hcryptkey hkey = NULL;
Cert_public_key_info certpubkeyinfo = pcertcontext-> pcertinfo-> subjectpublickeyinfo;
Cryptimppubpublickeyinfo (htmpprov, x509_asn_encoding | pkcs_7_asn_encoding, & certpubkeyinfo, & hkey );
6. Export the Public Key (the second call method is recommended)
Unsigned char * pbuf = NULL;
Unsigned long ulbuflen = 0;
Cryptexportkey (hkey, 0, publickeyblob, 0, pbuf, & ulbuflen );
Pbuf = new unsigned char [ulbuflen];
Memset (pbuf, 0, ulbuflen );
Cryptexportkey (hkey, 0, publickeyblob, 0, pbuf, & ulbuflen );
7. obtain public key information
Unsigned char * P = pbuf + sizeof (publickeystruc );
(* (Rsapubkey *) P). bitlen; // public key module length (in BIT)
(* (Rsapubkey *) P). pubexp; // E of the public key (note the byte order)
P + = sizeof (rsapubkey); // n of the public key (note the byte order)
8. Cleanup
Delete [] pbuf;
Pbuf = NULL;
Cryptdestroykey (hkey );
Cryptreleasecontext (htmpprov, 0 );
Certfreecertificatecontext (pcertcontext );