PFX证书导入到USBKEY(代码).doc_第1页
PFX证书导入到USBKEY(代码).doc_第2页
PFX证书导入到USBKEY(代码).doc_第3页
PFX证书导入到USBKEY(代码).doc_第4页
PFX证书导入到USBKEY(代码).doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

查看文章PFX证书导入到USBKEY(代码)2008-10-24 17:295. 完整代码#include stdafx.h#include #include #include / Global Csp HandleHCRYPTPROV hCryptProv = NULL; / Handle for a cryptographicvoid ToSmartCard(HCERTSTORE hSystemStore)PCCERT_CONTEXT pCertContext = NULL; char pszNameString256; DWORD dwKeySpec;HCRYPTKEY UserKey = 0;DWORD dwBlobLen;BYTE *pbKeyBlob;HCRYPTKEY hPubKey = NULL;int error;/ CryptAcquireCertificatePrivateKey Exported HandleHCRYPTPROV hCrypt = NULL;/ Just for Test Provider NameBYTE pbData1000; / 1000 will hold the longest/ key container name.DWORD cbData;cbData = 1000;/-/ Find the certificates in the system store./ In fact, just have one.while(pCertContext= CertEnumCertificatesInStore( hSystemStore, pCertContext) / on the first call to the function, / this parameter is NULL / on all subsequent calls, / this parameter is the last pointer / returned by the function /- / Do whatever is needed for a current certificate. / . /- / Find and print the name of the subject of the certificate / just retrieved. if(CertGetNameString( pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, pszNameString, 128) printf(Certificate for %s has been retrieved.n,pszNameString); if(!( CryptAcquireCertificatePrivateKey( pCertContext, 0, NULL, &hCrypt, /&hCryptProv, / 注意此处 hCrypt 输出不该是hCryptProv,如果没有猜错是Microsoft Base那个 &dwKeySpec, NULL) printf(CryptAcquireCertificatePrivateKey.n); / Test hCrypt, Try to get its name; / 猜错了,不是Microsoft Base.,而是下面这个 / Microsoft Enhanced Cryptographic Provider v1.0 if(CryptGetProvParam( hCrypt, PP_NAME, pbData, &cbData, 0) printf(CryptGetProvParam succeeded.n); printf(Provider name: %sn, pbData); if( ! CryptGetUserKey( hCrypt, /not hCryptProv, AT_KEYEXCHANGE, /dwKeySpec, &UserKey ) ) printf(CryptGetUserKey Error.n); /- / Export the public/private key-pair. if(CryptExportKey( UserKey, NULL, PRIVATEKEYBLOB, 0, NULL, &dwBlobLen) printf(Size of the BLOB for the public/private key pair determined. n); else printf(Error computing BLOB length.n); exit(1); /- / Allocate memory for the pbKeyBlob. if(pbKeyBlob = (BYTE*)malloc(dwBlobLen) printf(Memory has been allocated for the BLOB. n); else printf(Out of memory. n); exit(1); /- / Do the actual exporting into the key BLOB. if(CryptExportKey( UserKey, NULL, PRIVATEKEYBLOB, 0, pbKeyBlob, &dwBlobLen) printf(Contents have been written to the BLOB. n); else printf(Error exporting key.n); exit(1); if(CryptImportKey( hCryptProv, pbKeyBlob, dwBlobLen, 0, 0, &hPubKey) printf(The key has been imported.n); else printf(Public key import failed.n); exit(1); / Not this one /if (!CryptSetKeyParam(UserKey, KP_CERTIFICATE, pCertContext-pbCertEncoded, 0) if (!CryptSetKeyParam(hPubKey, KP_CERTIFICATE, pCertContext-pbCertEncoded, 0) error = GetLastError(); printf(CryptSetKeyParam 0x%xn, error); if (error != NTE_BAD_TYPE) / If error is bad_type then we just cant set the property. / Likely means our provider isnt a smart card. / If there was another error, we should report it. printf(CryptSetKeyParam Failed (0x80090020 usually means no room on card) 0x%xn,error); if(hPubKey) CryptDestroyKey(hPubKey); /- / Allocate memory for the pbKeyBlob. if(pbKeyBlob) free(pbKeyBlob); else printf(CertGetName failed. n); CertFreeCertificateContext(pCertContext); / End of whilevoid InstallPfx(CHAR *filename, CHAR *password)HANDLE hFile;BOOL bResult;BYTE inBuffer10000;DWORD nBytesToRead = 0;DWORD nBytesRead;WCHAR wszpassword20;HCERTSTORE pfxcert = NULL;hFile = CreateFile(filename, / open MYFILE.TXT GENERIC_READ, / open for reading FILE_SHARE_READ, / share for reading NULL, / no security OPEN_EXISTING, / existing file only FILE_ATTRIBUTE_NORMAL, / normal file NULL); / no attr. templateif (hFile = INVALID_HANDLE_VALUE) printf(Could not open file.n); / process error return;nBytesToRead = GetFileSize (hFile, NULL);printf(File Size is %dn, nBytesToRead);bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead, NULL) ; if (bResult) / Reading is OK. CRYPT_DATA_BLOB pfxblob; pfxblob.cbData = nBytesRead; pfxblob.pbData = inBuffer; if( PFXIsPFXBlob( &pfxblob) ) printf(Its a Pfx Certificate.n); MultiByteToWideChar( CP_ACP, 0, password, strlen(password)+1, wszpassword, sizeof(wszpassword)/sizeof(wszpassword0) ); if( NULL != PFXImportCertStore( &pfxblob, wszpassword, 0x21 /CRYPT_USER_PROTECTED ) ) printf(Its a Pfx imPORT OK.n); pfxcert = PFXImportCertStore( &pfxblob, wszpassword, 0x21 /CRYPT_USER_PROTECTED ); ToSmartCard(pfxcert); / 列出所有My的证书 /ToSmartCard(hSystemStore); CloseHandle(hFile);void OpenCert(HCRYPTPROV hprov)/-/ Declare and initialize variables.HCERTSTORE hSystemStore; / system store handle/-/ Open the MY system certificate store. The same call can be/ used with the name of a different system store, such as My or Root,/ as the second parameter.if(hSystemStore = CertOpenSystemStore( hprov, MY) printf(The MY system store is open. Continue.n);else printf(The MY system store did not open.n); exit(1);/ Use the store as needed./ .InstallPfx(d:exported.pfx, 123456);/ When done using the store, close it.if(!CertCloseStore(hSystemStore,CERT_CLOSE_STORE_CHECK_FLAG) printf(Unable to close the MY system store.n); exit(1);int main(int argc, CHAR* argv)/-/ Declare and initialize variables./ provider context.LPCSTR UserName = MyKeyContainer; / Name of the key containerLPCSTR ProviderName = eSafe Cryptographic Service Provider v2.0;/ to be used./-/ Attempt to acquire a context and a key/ container. The context will use the default CSP/ for the RSA_FULL provider type. DwFlags is set to 0/ to attempt to open an existing key container.if(CryptAcquireContext( &hCryptProv, / Handle to the CSP UserName, / Container name ProviderName, / Use the default provider PROV_RSA_FULL, / Provider type 0) / Flag values printf(A crypto context with the %s key container n, UserName); printf(has been acquired.nn); OpenCert(hCryptProv);else /- / An error occurred in acquiring the context. This could mean / that the key container requested does not exist. In this case, / the function can be called again to attempt to create a new key / container. Error codes are defined in winerror.h. if (

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论