📄 signaturedlg.cpp
字号:
// SignatureDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Signature.h"
#include "SignatureDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <wincrypt.h>
/////////////////////////////////////////////////////////////////////////////
// CSignatureDlg dialog
/////////////////////////////////////////////////////////////////////////////
// CApiEncryptDlg dialog
#pragma comment( lib, "crypt32.lib")
#pragma comment( lib, "advapi32.lib")
CSignatureDlg::CSignatureDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSignatureDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSignatureDlg)
m_file = _T("c:\\aaa.jpg");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSignatureDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSignatureDlg)
DDX_Control(pDX, IDC_EData, m_data);
DDX_Text(pDX, IDC_EFile, m_file);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSignatureDlg, CDialog)
//{{AFX_MSG_MAP(CSignatureDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BSignature, OnBSignature)
ON_BN_CLICKED(IDC_BCmp, OnBCmp)
ON_EN_CHANGE(IDC_EData, OnChangeEData)
ON_EN_CHANGE(IDC_EFile, OnChangeEFile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSignatureDlg message handlers
BOOL CSignatureDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CSignatureDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSignatureDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSignatureDlg::OnBSignature()
{
//变量声明:
HCRYPTPROV hProv;// CSP的句柄
HCRYPTHASH hHash;//散列的句柄
const int BUFFER=4096;//缓冲区大小
int dReadLen;
BYTE pBuffer[BUFFER];
//存放读文件内容的缓冲区
BYTE pSignature[16];//存放签名的缓冲区
DWORD dSignatureLen=16;//签名的长度
FILE *fp; //一个文件流
fp=fopen(m_file,"rb");
if(!fp){
AfxMessageBox("没有该文件");
return;
}
memset(pSignature,0,256);
if(!CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,0))
//连接默认的CSP,接受它的句柄放入hProv
{//错误处理
AfxMessageBox("error1");
return;
}
if(!CryptCreateHash(hProv,CALG_MD5,0,0,&hHash))
//使用MD5算法创建一个散列对象,得到它的句柄放入hHash
{
//错误处理
AfxMessageBox("error2");
return;
}
do{
dReadLen=fread(pBuffer,1,BUFFER,fp);
if(!CryptHashData(hHash,pBuffer,dReadLen,0))
//根据文件的内容计算散列值
{
//错误处理
AfxMessageBox("error3");
return;
}
if(dReadLen<BUFFER)break;
}while(1);
if(FALSE==CryptSignHash(hHash,AT_SIGNATURE,NULL,0,pSignature,&dSignatureLen)){
//错误处理
// AfxMessageBox("error4");
return;
}
FILE *fp1=fopen("c:\\ahash","wb");
fwrite(pSignature,1,dSignatureLen,fp1);
fclose(fp1);
char dat[2048];
memset(dat,0,2048);
BYTE c;
char a,b;
for(int i=0;i<256;i++){
c=pSignature[i];
a=c&0xf0;
a=a>>4;
if(a>9)a=(char)('A'+a-10);
else a=(char)('0'+a);
b=c&0x0f;
if(b>9)a=(char)('A'+b-10);
else b=(char)('0'+b);
sprintf(dat+strlen(dat),"%c%c,",a,b);
}
m_data.SetWindowText(dat);
CryptDestroyHash(hHash);
CryptReleaseContext(hProv,0);
AfxMessageBox("在C:建立了签名文件ahash !");
}
void CSignatureDlg::OnBCmp()
{
Test();
}
void CSignatureDlg::OnChangeEData()
{
UpdateData();
}
void CSignatureDlg::OnChangeEFile()
{
UpdateData();
}
void CSignatureDlg::Test()
{
HCRYPTPROV hProv = 0;
#define BUFFER_SIZE 256
BYTE pbBuffer[BUFFER_SIZE];
HCRYPTHASH hHash = 0;
BYTE *pbSignature = NULL;
DWORD dwSigLen;
LPTSTR szDescription = TEXT("Test Data");
DWORD i;
char inf[256];
// Get a handle to the default provider.
if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
sprintf(inf,"Error %x during CryptAcquireContext!\n", GetLastError());
AfxMessageBox(inf);
goto done;
}
// Fill the buffer with test data.
for(i = 0 ; i < BUFFER_SIZE ; i++) {
pbBuffer[i] = (BYTE)i;
}
// Create a hash object.
if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
sprintf(inf,"Error %x during CryptCreateHash!\n", GetLastError());
AfxMessageBox(inf);
goto done;
}
// Compute the cryptographic hash of the buffer.
if(!CryptHashData(hHash, pbBuffer, BUFFER_SIZE, 0)) {
sprintf(inf,"Error %x during CryptHashData!\n", GetLastError());
AfxMessageBox(inf);
goto done;
}
// Determine the size of the signature and allocate memory.
dwSigLen= 0;
if(!CryptSignHash(hHash, AT_SIGNATURE, NULL, 0, NULL,&dwSigLen)) {
sprintf(inf,"Error0000 %x during CryptSignHash!\n", GetLastError());
AfxMessageBox(inf);
if(GetLastError()!=NTE_BAD_LEN) goto done;
}
if((pbSignature =(BYTE*)malloc(dwSigLen)) == NULL) {
printf("Out of memory!\n");
goto done;
}
// Sign the hash object.
if(!CryptSignHash(hHash, AT_SIGNATURE, szDescription, 0, pbSignature,
&dwSigLen)) {
sprintf(inf,"Error11111 %x during CryptSignHash!\n", GetLastError());
AfxMessageBox(inf);
goto done;
}
// Store or transmit the signature, test buffer, and description string.
done:
// Free memory to be used to store signature.
if(pbSignature != NULL) free(pbSignature);
// Destroy the hash object.
if(hHash != 0) CryptDestroyHash(hHash);
// Release the provider handle.
if(hProv != 0) CryptReleaseContext(hProv, 0);
AfxMessageBox("验证成功!");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -