1.从磁盘上的证书文件中读取证书数据

unsigned char* pbP12Data; // 证书数据

unsigned long ulP12DataLen; // 证书数据长度

2.让用户输入证书密码

char* szPwd; // 证书密码

3.将证书密码转换成UNICODE格式(最好采用二次调用方式)

LPWSTR pWideChar = NULL;

int nWideChar = 0;

nWideChar = MultiByteToWideChar(CP_ACP, 0, szPwd, -1, pWideChar, nWideChar);

pWideChar = new WCHAR[nWideChar];

memset(pWideChar, 0, sizeof(WCHAR)*nWideChar);

MultiByteToWideChar(CP_ACP, 0, szPwd, -1, pWideChar, nWideChar);

4.将证书数据导入临时store

CRYPT_DATA_BLOB blob;

memset(&blob, 0, sizeof(blob));

blob.pbData = pbP12Data;

blob.cbData = ulP12DataLen;

HCERTSTORE hCertStore = NULL;

hCertStore = PFXImportCertStore(&blob, pWideChar, CRYPT_EXPORTABLE);

5.在store中查找证书,获取CertContext

PCCERT_CONTEXT pCertContext = CertFindCertificateInStore(hCertStore, X509_ASN_ENCODING|PKCS_7_ASN_ENCODING, 0, CERT_FIND_ANY, NULL, NULL);

6.获取证书信息

CRYPT_INTEGER_BLOB snBlob = pCertContext->pCertInfo->SerialNumber; // 证书SN

pCertContext->pbCertEncoded; // X509格式证书数据

pCertContext->cbCertEncoded; // X509格式证书数据长度

7.获取CSP句柄

HCRYPTPROV hProv = NULL;

DWORD dwKeySpec = 0;

BOOL bCallerFreeProv = FALSE;

CryptAcquireCertificatePrivateKey(pCertContext, 0, NULL, &hProv, &dwKeySpec, &bCallerFreeProv);

8.获取密钥句柄

HCRYPTKEY hKey = NULL;

CryptGetUserKey(hProv, dwKeySpec, &hKey);

9.导出私钥(最好采用二次调用方式)

BYTE* pbData = NULL;

DWORD dwDataLen = 0;

CryptExportKey(hKey, NULL, PRIVATEKEYBLOB, 0, pbData, &dwDataLen);

pbData = new BYTE[dwDataLen];

memset(pbData, 0, dwDataLen);

CryptExportKey(hKey, NULL, PRIVATEKEYBLOB, 0, pbData, &dwDataLen);

10.获取公私钥信息

BYTE *p = pbData+ sizeof(PUBLICKEYSTRUC); 

(*(RSAPUBKEY*)p).bitlen; // 公私钥模长(以bit为单位)

(*(RSAPUBKEY*)p).pubexp; // 公钥的e(注意字节顺序)

p += sizeof(RSAPUBKEY); // 公私钥的n(注意字节顺序)

p += ((*(RSAPUBKEY*)p).bitlen)/8; // 私钥的p(注意字节顺序)

p += ((*(RSAPUBKEY*)p).bitlen)/16; // 私钥的q(注意字节顺序)

p += ((*(RSAPUBKEY*)p).bitlen)/16; // 私钥的dp(注意字节顺序)

p += ((*(RSAPUBKEY*)p).bitlen)/16; // 私钥的dq(注意字节顺序)

p += ((*(RSAPUBKEY*)p).bitlen)/16; // 私钥的qu(注意字节顺序)

p += ((*(RSAPUBKEY*)p).bitlen)/16; // 私钥的d(注意字节顺序)

11.清理工作

delete[] pbData;

pbData = NULL;

CryptDestroyKey(hKey);

CryptReleaseContext(hProv, 0);

CertFreeCertificateContext(pCertContext);

CertCloseStore(hCertStore, CERT_CLOSE_STORE_FORCE_FLAG);

delete[] pWideChar;

pWideChar = NULL;