⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 authenticationdlg.cpp

📁 利用c++实现的一个数字认证的模拟程序。
💻 CPP
字号:
// authenticationDlg.cpp : implementation file
//

#include "stdafx.h"
#include "authentication.h"
#include "authenticationDlg.h"
#include <io.h>
#include "des.h"
#include "md5.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAuthenticationDlg dialog

CAuthenticationDlg::CAuthenticationDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAuthenticationDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAuthenticationDlg)
	m_messagepath = _T("");
	m_digestpath = _T("");
	m_password = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAuthenticationDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAuthenticationDlg)
	DDX_Text(pDX, IDC_EDIT1, m_messagepath);
	DDX_Text(pDX, IDC_EDIT2, m_digestpath);
	DDX_Text(pDX, IDC_EDIT3, m_password);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAuthenticationDlg, CDialog)
	//{{AFX_MSG_MAP(CAuthenticationDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_AUTHEN, OnAuthen)
	ON_BN_CLICKED(IDC_CREATE, OnCreate)
	ON_BN_CLICKED(IDC_OPENFILE, OnOpenfile)
	ON_BN_CLICKED(IDC_OPENMD, OnOpenmd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAuthenticationDlg message handlers

BOOL CAuthenticationDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
}

void CAuthenticationDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CAuthenticationDlg::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 CAuthenticationDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAuthenticationDlg::OnAuthen() 
{
	// TODO: Add your control notification handler code here
	UpdateData();

	if(m_messagepath.IsEmpty() || m_password.IsEmpty())
		return;
	CFile fSource;
	if(!fSource.Open(m_messagepath, CFile::modeRead))
	{
		MessageBox("不能打开消息文件!");
		return;
	}
	DWORD mesflen = fSource.GetLength();
    unsigned char *sText = new unsigned char[mesflen + 1];
	sText[mesflen] = 0;

	fSource.Read(sText, mesflen);		
	fSource.Close();	

	CString strResult = "";	
	unsigned char digest[16];
	MD5_CTX md5T;
	md5T.MD5Update (sText, mesflen);
	md5T.MD5Final (digest);	
	delete [] sText;

	CString tmp;
	for (int i=0; i<16; i++){
		tmp.Format("%x", digest[i]);		
		if (tmp.GetLength() == 1)
			tmp = "0" + tmp;
		strResult += tmp;
	}

	CFile fmsn;	
	if(!fmsn.Open(m_digestpath, CFile::modeRead))
	{
		MessageBox("不能打开摘要文件!");
		return;
	}
    DWORD msnlen = fmsn.GetLength();
    char *sdigest = new char[msnlen + 1];
	sdigest[msnlen] = 0;
	fmsn.Read(sdigest, msnlen);		
	fmsn.Close();
//	MessageBox(sdigest);
	char *strotherResult = new char [msnlen + 1];
	memset(strotherResult, 0, msnlen + 1);
	des_de(sdigest, msnlen, (char *)(LPCSTR)m_password, strotherResult);

	if(strResult.Compare(strotherResult) == 0)
		MessageBox("文件未被修改!");
	else
		MessageBox("文件被修改了!");


	UpdateData(false);
}

void CAuthenticationDlg::OnCreate() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
  
	if(m_messagepath.IsEmpty() || m_password.IsEmpty())
		return;
	CFile fSource;
	if(!fSource.Open(m_messagepath, CFile::modeRead))
	{
		MessageBox("不能打开消息文件!");
		return;
	}
	DWORD mesflen = fSource.GetLength();
    unsigned char *sText = new unsigned char[mesflen + 1];
	sText[mesflen] = 0;

	fSource.Read(sText, mesflen);		
	fSource.Close();	

	CString strResult = "";	
	unsigned char digest[16];
	MD5_CTX md5T;
	md5T.MD5Update (sText, mesflen);
	md5T.MD5Final (digest);	
	delete [] sText;

	CString tmp;
	for (int i=0; i<16; i++){
		tmp.Format("%x", digest[i]);		
		if (tmp.GetLength() == 1)
			tmp = "0" + tmp;
		strResult += tmp;
	}


	int outbuflen = (((strResult.GetLength()-1)+7)/8)*8;
	char *outbuf = new char[outbuflen];
	memset(outbuf, 0, outbuflen);
//	MessageBox(strResult);
	des_en((char*)(LPCSTR)strResult, strResult.GetLength(), (char *)(LPCSTR)m_password, outbuf);

	CStdioFile mfile;
	if(!mfile.Open("m.sn", CFile::modeCreate|CFile::modeWrite))
	{
		MessageBox("创建m.sn失败!");
		return;
	}
	mfile.Write(outbuf, outbuflen);
	mfile.Close();	
	
	MessageBox("已生成消息摘要!");
	UpdateData(false);
}


void CAuthenticationDlg::OnOpenfile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(true,NULL,NULL,OFN_OVERWRITEPROMPT,"allfile(*.*)|*.*||",NULL);
	if(dlg.DoModal()==IDOK)
	{	    
		if(_access(dlg.GetPathName(),0)==-1)
		{
			MessageBox("文件不存在");
			return;
		}
		m_messagepath=dlg.GetPathName();				
	}
	UpdateData(false);
}

void CAuthenticationDlg::OnOpenmd() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(true,NULL,NULL,OFN_OVERWRITEPROMPT,"m.sn(*.sn)|*.sn||",NULL);
	if(dlg.DoModal()==IDOK)
	{	    
		if(_access(dlg.GetPathName(),0)==-1)
		{
			MessageBox("文件不存在");
			return;
		}
		m_digestpath=dlg.GetPathName();				
	}
	UpdateData(false);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -