vc6中使用数字签名

vc 2005中使用数字签名十分方便,只要包含了WinTrust.h、SoftPub.h和Mscat.h,并添加导入库WinTrust.lib;就可以直接调api,但使用VC6的朋友们就麻烦了,需要参考MSDN上的函数及结构体声明,自已定义函数指针最后进行调用。

因为项目用的是VC6,没办法,只好硬着头皮把函数指针扣出来,同时发现还有问题:
1.数字签名需要新sdk的支持,不装新的sdk,就算函数指针都对也无法正确编译
2.函数指针所在的文件,不能加预编译头,否则与mfc的一些定义会有冲突,需要在工程设置中选择这个文件,并设置他的"Precompiled Headers",设成"Not using procompiled headers",当然不要忘了在函数定义时加上extern "c",可以用define包起来.

 

vc 2005中的使用可参考:
http://www.titilima.cn/readblog.php?id=108 

【前言】 工作或学习可能需要实现基于VC读\写Excel文件的功能,本人最近也遇到了该问题。间虽经波折,但是最终还是找到了解决问题的办法。 在此跟大家分享,希望对跟我同样迷茫过的同学们有所帮助。 1、程序功能 1)打开一个excel文件; 2)显示到CListCtrl上; 3)新建一个Excel文件。 以上均在对话框实现。 2、平台 VC++2010 3、实现方法 常用的Excel打开方式有两种 1)通过数据库打开; 2)OLE方式打开。 由于方式1)操作繁琐,经常出现莫名的错误,这里选用方式2). 4、准备步骤 首先新建一个Dialog窗体程序,添加list control和两个按钮 1)将ExcelLib文件夹拷贝到程序目录下; 2)将Export2Excel.h,Export2Excel.cpp两个文件添加到项目; 3)包含头文件,#include "ExcelLib/Export2Excel.h" 通过以上步骤在程序引入了可以读取Excle文件的CExport2Excel类; 5、打开excel文件 通过按钮点击打开 void CExcelTestDlg::OnBnClickedButtonOpenExcel() { //获取文件路径 CFileDialog* lpszOpenFile; CString szGetName; lpszOpenFile = new CFileDialog(TRUE,"","",OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,"Excel File(*.xlsx;*.xls)|*.xls;*.xlsx",NULL); if (lpszOpenFile->DoModal()==IDOK) { szGetName = lpszOpenFile->GetPathName(); SetWindowText(szGetName); delete lpszOpenFile; } else return; //打开文件 //文件包含多个sheet时,默认打开第一个sheet CExport2Excel Excel_example; Excel_example.OpenExcel(szGetName); //获取sheet个数 int iSheetNum = Excel_example.GetSheetsNumber(); //获取已使用表格行列数 int iRows = Excel_example.GetRowCount(); int iCols = Excel_example.GetColCount(); //获取单元格的内容 CString cs_temp = Excel_example.GetText(1,1); //AfxMessageBox(cs_temp); //List control上显示 //获取工作表列名(第一行) CStringArray m_HeadName; m_HeadName.Add(_T("ID")); for (int i=1;iGetItemCount()>0) { m_list.DeleteColumn(0); } //初始化ClistCtrl,加入列名 InitList(m_list,m_HeadName); //填入内容 //第一行是标题,所以从第2行开始 CString num; int pos; for (int row = 2;row<=iRows; row++) { pos = m_list.GetItemCount(); num.Format(_T("%d"),pos +1); m_list.InsertItem(pos,num); for (int colum=1;columDoModal()==IDOK) { szGetName = lpszOpenFile->GetPathName(); SetWindowText(szGetName); delete lpszOpenFile; } else return; //文件全名称 CString csFileName = szGetName; //需要添加的两个sheet的名称 CString csSheetName = "newSheet"; CString csSheetName2 = "newSheet2"; // 新建一个excel文件,自己写入文字 CExport2Excel Excel_example; //新建excel文件 Excel_example.CreateExcel(csFileName); //添加sheet,新加的sheet在前,也就是序号为1 Excel_example.CreateSheet(csSheetName); Excel_example.CreateSheet(csSheetName2); //操作最开始添加的sheet:(newSheet) Excel_example.SetSheet(2); //添加表头 Excel_example.WriteHeader(1,"第一列"); Excel_example.WriteHeader(2,"第二列"); //添加核心数据 Excel_example.WriteData(1,1,"数据1"); Excel_example.WriteData(1,2,"数据2"); //保存文件 Excel_example.Save(); //关闭文件 Excel_example.Close(); } 7、注意事项 1)一般单个Excel文件包含多个sheet,程序默认打开第一个; 2)指定操作sheet,使用Excel_example.SetSheet(2)函数; 3)打开文件时最左侧的sheet序号为1,新建excel时最新添加的sheet序号为1. 【后记】 本程序主要基于网络CSDN---“Excel封装库V2.0”---完成,下载地址是:http://download.csdn.net/detail/yeah2000/3576494,在此表示感谢!同时, 1)在其基础上作了小改动,改正了几个小错误,添加了几个小接口; 2)添加了如何使用的例子,原程序是没有的; 3)详细的注释 发现不足之处,还请大家多多指教!
实现数字签名的方式有很多种,下面是使用 VC6.0 实现 RSA 数字签名的示例代码: ```cpp #include <windows.h> #include <wincrypt.h> #include <stdio.h> #include <string.h> #pragma comment(lib, "Crypt32.lib") DWORD MyHandleError(char *s); int main() { HCRYPTPROV hProv = NULL; HCRYPTHASH hHash = NULL; HCRYPTKEY hKey = NULL; DWORD dwSigLen = 0; DWORD dwHashLen = 0; BYTE pbHash[1024] = { 0 }; BYTE pbSignature[1024] = { 0 }; CHAR szContainerName[] = "MyContainer"; CHAR szProviderName[] = "Microsoft Enhanced Cryptographic Provider v1.0"; CHAR szData[] = "This is a test data for signing"; // acquire a handle to the default key container if (!CryptAcquireContext(&hProv, szContainerName, szProviderName, PROV_RSA_FULL, 0)) { if (GetLastError() == NTE_BAD_KEYSET) { // create a new key container if (!CryptAcquireContext(&hProv, szContainerName, szProviderName, PROV_RSA_FULL, CRYPT_NEWKEYSET)) MyHandleError("Error in acquiring context"); } else { MyHandleError("Error in acquiring context"); } } // create a hash object if (!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash)) MyHandleError("Error in creating hash object"); // hash the data if (!CryptHashData(hHash, (BYTE*)szData, strlen(szData), 0)) MyHandleError("Error in hashing data"); // determine the size of the signature if (!CryptSignHash(hHash, AT_SIGNATURE, NULL, 0, NULL, &dwSigLen)) MyHandleError("Error in determining signature length"); // allocate memory for the signature if (!CryptSignHash(hHash, AT_SIGNATURE, NULL, 0, pbSignature, &dwSigLen)) MyHandleError("Error in signing hash"); // verify the signature if (!CryptGetUserKey(hProv, AT_SIGNATURE, &hKey)) MyHandleError("Error in getting user key"); if (!CryptVerifySignature(hHash, pbSignature, dwSigLen, hKey, NULL, 0)) MyHandleError("Error in verifying signature"); printf("Signature verified successfully\n"); // release resources if (hKey) CryptDestroyKey(hKey); if (hHash) CryptDestroyHash(hHash); if (hProv) CryptReleaseContext(hProv, 0); return 0; } DWORD MyHandleError(char *s) { fprintf(stderr, "%s\n", s); exit(EXIT_FAILURE); } ``` 这段代码会创建一个 RSA 密钥对,使用 SHA1 哈希算法对数据进行哈希,然后对哈希值进行数字签名,并验证签名的正确性。在实际使用,可以根据需要修改哈希算法和签名算法,以及相关的参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值