资源描述:
《pfx证书导入到usbkey(代码)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、查看文章 PFX证书导入到USBKEY(代码)2008-10-2417:295.完整代码#include"stdafx.h"#include#include#include//GlobalCspHandleHCRYPTPROVhCryptProv=NULL; //HandleforacryptographicvoidToSmartCard(HCERTSTOREhSystemStore){PCCERT_CONTEXTpCertContext=NULL;char pszNameString[25
2、6];DWORD dwKeySpec;HCRYPTKEY UserKey=0;DWORDdwBlobLen;BYTE*pbKeyBlob;HCRYPTKEYhPubKey=NULL;interror;//CryptAcquireCertificatePrivateKeyExportedHandleHCRYPTPROVhCrypt=NULL;//JustforTestProviderNameBYTE pbData[1000]; //1000willholdthelongest//keycontainername.DWORDcbData;cb
3、Data=1000;//-------------------------------------------------------------------//Findthecertificatesinthesystemstore.//Infact,justhaveone.while(pCertContext=CertEnumCertificatesInStore( hSystemStore, pCertContext))//onthefirstcalltothefunction, //thisparameterisNULL //onallsubsequentcalls,
4、//thisparameteristhelastpointer //returnedbythefunction{ //---------------------------------------------------------------- //Dowhateverisneededforacurrentcertificate. //... //-------------------------------------------------------------------- //Findandprintthenameofthesubjectofthecertifi
5、cate //justretrieved. if(CertGetNameString( pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, pszNameString, 128)) { printf("Certificatefor%shasbeenretrieved.",pszNameString); if(!(CryptAcquireCertificatePrivateKey( pCertContext, 0, NULL, &hCrypt,//
6、&hCryptProv, //注意此处hCrypt输出不该是hCryptProv,如果没有猜错是MicrosoftBase那个 &dwKeySpec, NULL))) { printf("CryptAcquireCertificatePrivateKey."); } //TesthCrypt,Trytogetitsname; //猜错了,不是MicrosoftBase...,而是下面这个 //MicrosoftEnhancedCryptographicProviderv1.0 if(CryptGetProvParam( hCry
7、pt, PP_NAME, pbData, &cbData, 0)) { printf("CryptGetProvParamsucceeded."); printf("Providername:%s",pbData); } if(!CryptGetUserKey( hCrypt, //nothCryptProv, AT_KEYEXCHANGE,//dwKeySpec,