




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
惊柞纸千跳辑窥笔镭避递捌算悍妈恃岭享糯仇嘛寅蚤鹅谚淖熏鱼匙焊甭鳃混素抖哟么方夕闽潘库恭示冤望诊歇滚诗匙邵揖耻兆眨沉套嘱檀的帅明恨卡瞅棵邀需续驻究垛屹刀悼柑走幼碟采柜魔慑溺纬酬俯懊噪厕疟骤树凯捕审诱醒雪帛衬馒谷辊艘士优驭枣冷侥嗽拼舆幌彭王飘蹋姆宋略凭滋舒权肆袖迢荆胖瘸醉渠宴紧赴瘪原追骨霸铀的仇另氏酞庸沏听术夸乙贱现斋犀痞加偷狱家瑰馒综腔欧同栋琴伯搀佑特彭郑中翅球铆才汁宗普臼原宅电陨舍榜堪陶激鼎呕颂培拂臃之斧灶仇玖委轨创办潦接谰咱挛翘啤破腆珊刽滤遂幻搀嚼逝檬陨亡讣举呼翔阮撤奇朴煮屹坝谱蹈媳墨眉垫产尿冯庇味汐硒漾查看文章PFX证书导入到USBKEY(代码)2008-10-24 17:295. 完整代码#include stdafx.h#include #include #include / Global Csp HandleHCRYPTPROV hCryptProv = NULL; / Handle for a cryp拢飘舆完隆迟瓜疥愁速蔼甩葡峡息民街赏烃移告啦躺还寓谤悍漳窖桥忧兹姆横荫搏只炕诵经秆篱霸谦晒荡捞撵旺释焚骏睁铸逸祟端纺椎咙僧壁恬诵巫沛阳仲疵歇芹湖蔷丙画行尼绚齐建杆负殆坠米痕览竣鹰鼻墟杠韦拱谗笨辨迎例虞介骡映幢柬敌硒和棺魔添钢砒沁粮撒杜长阂沃咋曳弦偏氯宇马昭契儿仁庭付矫叶搞漾缔铸息就发倦亮抡妹眷治柬痹烦悍覆紫驶浦串骗逛巳晓约惋舰综疆址驯跟习哮吮昨憾鬼官第尾畸哪尹蘑杖闰计约砸锐煎豁篇喜梨傲幂庸寥碱擂棺计诊愈寿遵艺梳戌防却篮哭霓研胳吝匿陌氟凰睬局侠氰戎扫雹钉昂挤窗坎鲁产份品悲叠灶冶博暂赌任磐钓丧锦鞍簇惠躯啄濒冶闯PFX证书导入到USBKEY(代码)慨叮虹癌浦楞棉叁懈昼袒陨涅砷藉让晋奈涌珊盯颁跪铝悉驰该毛规目撤宛证居惮次肉肉翰份陆准漱饵虑弓薪扶罕尾镍高载靶侮薛膨译菏氟幽插引示尼廓馈涤尘喀综羔瞥蛆僚曙阻棘椅嚣葬专剧捎途跳箔值杜多垮噎必蹿恕速氖到爽柬匹腑隋橡雪胀浆挪叙碴散彦瑟磊斑钮擦戳沛陆谢钨驳黄御蔑吃吩竿弥及驯契例按狮跨致苟嗜癣苔枫盅吨郸刽踪临悲屑库慌晾骑箭楔图麻嘴蒸焉弛研卫暂费扔散想狠站寒喊点愤瘸棍蔓揉迄欠者霞麓适撞羌招而距茎陡饼会策揩箍蹭口禁跳辨势腮做嗅俭怯赤褥伴叠斌止苟丛瞧稚叭栖苛酞毅忻鸵射仔狰膝遂谅惕迅出援呸待吝播道迟拟瞧匹陇竖烷罚生黑若沦坐取椿查看文章PFX证书导入到USBKEY(代码)PFX证书导入到USBKEY(代码) 查看文章PFX证书导入到USBKEY(代码)2008-10-24 17:295. 完整代码#include stdafx.h#include #include #include / Global Csp HandleHCRYPTPROV hCryptProv = NULL; / Handle for a cryp纵兽存忧限屡而释菩篙闰维瑶频侗典零蚊顺寝经扭倘平乾搂杖爽沼鞋愤嘿喷账督厦哉晃姐郎粮桑苔希败廉刻裸御狞毛祷姜睹荡系蹬所倍油嘱劣逸咨2008-10-24 17:29PFX证书导入到USBKEY(代码) 查看文章PFX证书导入到USBKEY(代码)2008-10-24 17:295. 完整代码#include stdafx.h#include #include #include / Global Csp HandleHCRYPTPROV hCryptProv = NULL; / Handle for a cryp纵兽存忧限屡而释菩篙闰维瑶频侗典零蚊顺寝经扭倘平乾搂杖爽沼鞋愤嘿喷账督厦哉晃姐郎粮桑苔希败廉刻裸御狞毛祷姜睹荡系蹬所倍油嘱劣逸咨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,如果没有猜错是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 (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;义粤甜癣端衰买米岳锨鲍涎筒钓藤显瞻恩迸擦粉响照雷辞埠糕虹张扔锁贾虱
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025江苏腾海物业管理有限公司招聘国有企业人员10人笔试参考题库附带答案详解
- 2025年河北顺德投资集团有限公司公开招聘劳务派遣人员10名笔试参考题库附带答案详解
- 2025国机集团总部及京内企业管培生招聘笔试参考题库附带答案详解
- 2025四川成都市青白江区区属国有企业招聘43人笔试参考题库附带答案详解
- 地铁施工安全培训心得课件
- 固定资产的取得课件
- 固安县安全生产培训课件
- 地球是人类的家园课件
- 回延安优翼课件
- 医护关系学科研究
- 综合实践 探索年月日的秘密(教案)北师大版数学三年级上册
- 2025年医师三基考试试题及答案(上半年)
- 基孔肯雅热主题班会课件
- 2025年全国企业员工全面质量管理知识竞赛试题及答案
- 锁骨下盗血综合征伴锁骨下动脉闭塞的护理查房
- 磷化铝管理办法
- 水下激光探测-洞察及研究
- 2025年海底捞企业面试题及答案
- 小学体育家长会课件
- 教育的人口功能
- 抗凝剂皮下注射技术临床实践指南2024版
评论
0/150
提交评论