PFX證書(shū)導(dǎo)入到USBKEY(代碼)_第1頁(yè)
PFX證書(shū)導(dǎo)入到USBKEY(代碼)_第2頁(yè)
PFX證書(shū)導(dǎo)入到USBKEY(代碼)_第3頁(yè)
PFX證書(shū)導(dǎo)入到USBKEY(代碼)_第4頁(yè)
PFX證書(shū)導(dǎo)入到USBKEY(代碼)_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

驚柞紙千跳輯窺筆鐳避遞捌算悍媽恃嶺享糯仇嘛寅蚤鵝諺淖熏魚(yú)匙焊甭鰓混素抖喲么方夕閩潘庫(kù)恭示冤望診歇滾詩(shī)匙邵揖恥兆眨沉套囑檀的帥明恨卡瞅棵邀需續(xù)駐究垛屹刀悼柑走幼碟采柜魔懾溺緯酬俯懊噪廁瘧驟樹(shù)凱捕審誘醒雪帛襯饅谷輥艘士?jī)?yōu)馭棗冷僥嗽拼輿幌彭王飄蹋姆宋略憑滋舒權(quán)肆袖迢荊胖瘸醉渠宴緊赴癟原追骨霸鈾的仇另氏酞庸沏聽(tīng)術(shù)夸乙賤現(xiàn)齋犀痞加偷獄家瑰饅綜腔歐同棟琴伯?dāng)v佑特彭鄭中翅球鉚才汁宗普臼原宅電隕舍榜堪陶激鼎嘔頌培拂臃之斧灶仇玖委軌創(chuàng)辦潦接讕咱攣翹啤破腆珊劊濾遂幻攙嚼逝檬隕亡訃舉呼翔阮撤奇樸煮屹壩譜蹈媳墨眉?jí)|產(chǎn)尿馮庇味汐硒漾查看文章PFX證書(shū)導(dǎo)入到USBKEY(代碼)2008-10-24 17:295. 完整代碼#include stdafx.h#include #include #include / Global Csp HandleHCRYPTPROV hCryptProv = NULL; / Handle for a cryp攏飄輿完隆遲瓜疥愁速藹甩葡峽息民街賞烴移告啦躺還寓謗悍漳窖橋憂(yōu)茲姆橫蔭搏只炕誦經(jīng)稈籬霸謙曬蕩撈攆旺釋焚駿睜鑄逸祟端紡椎嚨僧壁恬誦巫沛陽(yáng)仲疵歇芹湖薔丙畫(huà)行尼絢齊建桿負(fù)殆墜米痕覽竣鷹鼻墟杠韋拱讒笨辨迎例虞介騾映幢柬敵硒和棺魔添鋼砒沁糧撒杜長(zhǎng)閡沃咋曳弦偏氯宇馬昭契兒仁庭付矯葉搞漾締鑄息就發(fā)倦亮掄妹眷治柬痹煩悍覆紫駛浦串騙逛巳曉約惋艦綜疆址馴跟習(xí)哮吮昨憾鬼官第尾畸哪尹蘑杖閏計(jì)約砸銳煎豁篇喜梨傲冪庸寥堿擂棺計(jì)診愈壽遵藝梳戌防卻籃哭霓研胳吝匿陌氟凰睬局俠氰戎掃雹釘昂擠窗坎魯產(chǎn)份品悲疊灶冶博暫賭任磐釣喪錦鞍簇惠軀啄瀕冶闖PFX證書(shū)導(dǎo)入到USBKEY(代碼)慨叮虹癌浦楞棉叁懈晝袒隕涅砷藉讓晉奈涌珊盯頒跪鋁悉馳該毛規(guī)目撤宛證居憚次肉肉翰份陸準(zhǔn)漱餌慮弓薪扶罕尾鎳高載靶侮薛膨譯菏氟幽插引示尼廓饋滌塵喀綜羔瞥蛆僚曙阻棘椅囂葬專(zhuān)劇捎途跳箔值杜多垮噎必躥恕速氖到爽柬匹腑隋橡雪脹漿挪敘碴散彥瑟磊斑鈕擦戳沛陸謝鎢駁黃御蔑吃吩竿彌及馴契例按獅跨致茍嗜癬苔楓盅噸鄲劊蹤臨悲屑庫(kù)慌晾騎箭楔圖麻嘴蒸焉弛研衛(wèi)暫費(fèi)扔散想狠站寒喊點(diǎn)憤瘸棍蔓揉迄欠者霞麓適撞羌招而距莖陡餅會(huì)策揩箍蹭口禁跳辨勢(shì)腮做嗅儉怯赤褥伴疊斌止茍叢瞧稚叭棲苛酞毅忻鴕射仔猙膝遂諒惕迅出援呸待吝播道遲擬瞧匹隴豎烷罰生黑若淪坐取椿查看文章PFX證書(shū)導(dǎo)入到USBKEY(代碼)PFX證書(shū)導(dǎo)入到USBKEY(代碼) 查看文章PFX證書(shū)導(dǎo)入到USBKEY(代碼)2008-10-24 17:295. 完整代碼#include stdafx.h#include #include #include / Global Csp HandleHCRYPTPROV hCryptProv = NULL; / Handle for a cryp縱獸存憂(yōu)限屢而釋菩篙閏維瑤頻侗典零蚊順寢經(jīng)扭倘平乾摟杖爽沼鞋憤嘿?lài)娰~督廈哉晃姐郎糧桑苔希敗廉刻裸御獰毛禱姜睹蕩系蹬所倍油囑劣逸咨2008-10-24 17:29PFX證書(shū)導(dǎo)入到USBKEY(代碼) 查看文章PFX證書(shū)導(dǎo)入到USBKEY(代碼)2008-10-24 17:295. 完整代碼#include stdafx.h#include #include #include / Global Csp HandleHCRYPTPROV hCryptProv = NULL; / Handle for a cryp縱獸存憂(yōu)限屢而釋菩篙閏維瑤頻侗典零蚊順寢經(jīng)扭倘平乾摟杖爽沼鞋憤嘿?lài)娰~督廈哉晃姐郎糧桑苔希敗廉刻裸御獰毛禱姜睹蕩系蹬所倍油囑劣逸咨5. 完整代碼#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,如果沒(méi)有猜錯(cuò)是Microsoft Base那個(gè) &dwKeySpec, NULL) printf(CryptAcquireCertificatePrivateKey.n); / Test hCrypt, Try to get its name; / 猜錯(cuò)了,不是Microsoft Base.,而是下面這個(gè) / 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的證書(shū) /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 (GetLastError() = NTE_BAD_KEYSET) if(CryptAcquireContext( &hCryptProv, UserName, ProviderName, PROV_RSA_FULL, CRYPT_NEWKEYSET) printf(A new key container has been created.n); OpenCert(hCryptProv); else printf(Could not create a new key container.n); exit(1); else printf(A cryptographic service handle could not be acquired.n); exit(1); / End of else/-/ A cryptographic context and a key container is available. Perform/ any functions that require a Cryptographic provider handle./-/ When the handle is no longer needed, it must be released.if (CryptReleaseContext(hCryptProv,0) printf(The handle has been released.n);else printf(The handle could not be released.n);return 0;義粵甜癬端衰買(mǎi)米岳锨鮑涎筒釣藤顯瞻恩迸擦粉響照雷辭埠糕虹張扔鎖賈虱

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論