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

📄 clientdlg.cpp

📁 实现通信过程中的数据加密(AES, DES, SHA-256,SHA-384,SHA512, RSA)。 测试加解密效率等
💻 CPP
字号:
// ClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Client.h"
#include "ClientDlg.h"
#include "Message.h"
#include "Crypt.h"

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

typedef struct
{
	byte sha[64];
	string des_key;
	string ciper_text;
}MESSAGE, *LPMESSAGE;

DWORD start_time;
DWORD end_time;
DWORD encrypt_time;

DWORD encrypt_key_start;
DWORD encrypt_key_end;
DWORD encrypt_key_time;

DWORD encrypt_message_start;
DWORD encrypt_message_end;
DWORD encrypt_message_time;

DWORD cal_hash_start;
DWORD cal_hash_end;
DWORD cal_hash_time;

UINT  encrypt_type;

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog

CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CClientDlg)
	m_strServ = _T("");
	m_nServPort = 0;
	m_strMessage = _T("");
	m_strDesKey = _T("");
	m_nNumSend = 0;
	m_strRsaFilepath = _T("");
	m_nNumSendSuc = 0;
	m_strSendTime = _T("");
	m_strEncryptTime = _T("");
	m_nRsaLength = 0;
	m_nPLength = 0;
	m_nCLength = 0;
	m_nHash = 0;
	m_nEncryptType = 0;
	m_nEncrypt = 0;
	m_strEncryptKeyTime = _T("");
	m_strEncryptMessageTime = _T("");
	m_strCalHashTime = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClientDlg)
	DDX_Text(pDX, IDC_EDIT1, m_strServ);
	DDX_Text(pDX, IDC_EDIT2, m_nServPort);
	DDX_Text(pDX, IDC_EDIT3, m_strMessage);
	DDX_Text(pDX, IDC_EDIT4, m_strDesKey);
	DDX_Text(pDX, IDC_EDIT6, m_nNumSend);
	DDX_Text(pDX, IDC_SSDATIC, m_strRsaFilepath);
	DDX_Text(pDX, IDC_EDIT5, m_nNumSendSuc);
	DDX_Text(pDX, IDC_EDIT7, m_strSendTime);
	DDX_Text(pDX, IDC_EDIT8, m_strEncryptTime);
	DDX_Text(pDX, IDC_EDIT9, m_nRsaLength);
	DDX_Text(pDX, IDC_EDIT10, m_nPLength);
	DDX_Text(pDX, IDC_EDIT11, m_nCLength);
	DDX_Radio(pDX, IDC_RADIO6, m_nHash);
	DDX_Radio(pDX, IDC_RADIO1, m_nEncryptType);
	DDX_Radio(pDX, IDC_RADIO21, m_nEncrypt);
	DDX_Text(pDX, IDC_EDIT12, m_strEncryptKeyTime);
	DDX_Text(pDX, IDC_EDIT13, m_strEncryptMessageTime);
	DDX_Text(pDX, IDC_EDIT14, m_strCalHashTime);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
	//{{AFX_MSG_MAP(CClientDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers

BOOL CClientDlg::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
    m_strServ = "192.168.1.67";
	m_nServPort = 1234;
	m_strMessage = "google-vs-bai";
	m_strDesKey = "test" ;
	m_nNumSend = 100;
	m_strRsaFilepath = "pub.txt";
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CClientDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CFileDialog dlg(TRUE, NULL);
	if(dlg.DoModal() == IDOK)
	{
		m_strRsaFilepath = dlg.GetPathName();
	}
	UpdateData(FALSE);
}

void CClientDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	this->GetDlgItem(IDC_BUTTON5)->EnableWindow(FALSE);
	if(m_nServPort <= 0 || m_nNumSend <= 0 || m_strMessage == "" || 
		m_strDesKey == "" || m_strRsaFilepath == "" || m_strServ == "")
	{
		AfxMessageBox("信息不完整,或者不正确!");
		this->GetDlgItem(IDC_BUTTON5)->EnableWindow(TRUE);
		return;
	}
	
	CCrypt crypt;
	if(m_strDesKey.GetLength() > 8)
		m_strDesKey = m_strDesKey.GetBufferSetLength(8);
	crypt.setDesKey(m_strDesKey.GetBuffer(0));
	crypt.setRsaKeyLength(512);
	crypt.setRsaPubFilename(m_strRsaFilepath.GetBuffer(0));
	crypt.setRsaEncryptor();
	crypt.setRsaEncryptSeed("123456");
	crypt.setAesKey(m_strDesKey.GetBuffer(0));
	
	MESSAGE msg;
	string  tmp;
	UINT    len;
	UINT    sizeSend = 0;
	DWORD   st;
	DWORD   et;
	char    strSend[SEND_BUFSIZ];

	encrypt_type = m_nEncryptType;

	CSocket* m_pSocket = new CSocket;
	if(!m_pSocket->Create())
	{
		delete m_pSocket;
		AfxMessageBox("创建SOCKET失败!");
		exit(0);
	}
		
	while(!m_pSocket->Connect(m_strServ,m_nServPort))
	{
		if(AfxMessageBox("连接失败,继续?",MB_YESNO) == IDNO)
		{
			this->GetDlgItem(IDC_BUTTON5)->EnableWindow(TRUE);
			return;
		}
	}
	encrypt_time = 0;
	encrypt_key_time = 0;
	encrypt_message_time = 0;
	cal_hash_time = 0;
	start_time = GetTickCount();
	if(m_nEncrypt == 0)
	{
		for(int i = 0; i < m_nNumSend + 2; i++)
		{
			if(i == 0)
			{
				strcpy(strSend, "begin");
				len = strlen(strSend);
				sizeSend += len + 1;
				strSend[len] = '\0';
			}
			
			else if(i == m_nNumSend + 1)
			{
				strcpy(strSend, "end");
				len = strlen(strSend);
				strSend[len] = '\0';
			}
			
			else
			{
				st = GetTickCount();
				cal_hash_start = st;
				if(m_nHash == 0)
					crypt.shaEncrypt((char*)m_strMessage.GetBuffer(0),msg.sha);
				else if(m_nHash == 1)
					crypt.sha256Encrypt((char*)m_strMessage.GetBuffer(0),msg.sha);
				else if(m_nHash == 2)
					crypt.sha512Encrypt((char*)m_strMessage.GetBuffer(0),msg.sha);
				else
					crypt.sha384Encrypt((char*)m_strMessage.GetBuffer(0),msg.sha);
				cal_hash_end = GetTickCount();
				cal_hash_time += cal_hash_end - cal_hash_start;

				encrypt_message_start = cal_hash_end;
				if(encrypt_type == 0)
					tmp = crypt.desEncrypt(m_strMessage.GetBuffer(0));
				else
					tmp = crypt.aesEncrypt(m_strMessage.GetBuffer(0));
				encrypt_message_end = GetTickCount();
				encrypt_message_time += encrypt_message_end - encrypt_message_start;

				encrypt_key_start = encrypt_message_end;
				msg.ciper_text = tmp;
				tmp = crypt.rsaEncrypt(m_strDesKey.GetBuffer(0));
				msg.des_key = tmp;
				et = GetTickCount();
				encrypt_key_end = et;
				encrypt_key_time += encrypt_key_end - encrypt_key_end;
				encrypt_time += et - st;
				
				strcpy(strSend, msg.ciper_text.c_str());
				len = strlen(strSend);
				sizeSend += len;
				strSend[sizeSend] = '\0';
				
				strcpy(strSend + sizeSend + 1, msg.des_key.c_str());
				len = strlen(strSend + sizeSend + 1);
				strSend[sizeSend + len + 1] = '\0';
				sizeSend += len + 1;
				
				if(m_nHash == 0) len = 16;
				else if(m_nHash == 1) len = 32;
				else if(m_nHash == 2) len = 64;
				else len = 48;
				memcpy(strSend + sizeSend + 1, (char*)msg.sha, len);
				sizeSend += len + 1;
				strSend[sizeSend] = '\0';
				
				if(i == 1)
				{
					//send(*m_pSocket, (char*)&sizeSend, sizeof(sizeSend), 0);
					m_pSocket->Send((char*)&sizeSend, sizeof(sizeSend), 0);
				}
			}
			
			//send(*m_pSocket, strSend, sizeSend, 0);
			m_pSocket->Send(strSend, sizeSend, 0);
			if(i < m_nNumSend) sizeSend = 0;
		}
	}
	else
	{
		UINT len;
		for(int i = 0; i < m_nNumSend + 2; i++)
		{
			if(i == 0)
			{
				strcpy(strSend, "begin");
				len = strlen(strSend);
				strSend[len] = '\0';
				m_pSocket->Send(strSend, len+1, 0);
			}
			
			else if(i == m_nNumSend + 1)
			{
				strcpy(strSend, "end");
				//len = strlen(strSend);
				strSend[3] = '\0';
				m_pSocket->Send(strSend, len+1, 0);
			}
			else
			{
				if(i == 1)
				{
					len = m_strMessage.GetLength() + 1;
					m_pSocket->Send(&len, sizeof(len), 0);
				}
				strcpy(strSend, m_strMessage.GetBuffer(0));
				len = m_strMessage.GetLength();
				strSend[len] = '\0';
				m_pSocket->Send(strSend, len+1, 0);
			}
		}
	}
	end_time = GetTickCount();
	AfxMessageBox("发送完成!");
	m_pSocket->Close();
    delete m_pSocket;
	m_nNumSendSuc = m_nNumSend;
	m_nPLength = m_strMessage.GetLength();

	if(m_nEncrypt == 0)
	{
		CFile file;
		file.Open(m_strRsaFilepath, CFile::modeRead);
		m_nRsaLength = file.GetLength();
		file.Close();
		m_nCLength = sizeSend;
		m_strSendTime.Format("%d ms",end_time - start_time - encrypt_time);
		m_strEncryptTime.Format("%d ms", encrypt_time);
		m_strEncryptKeyTime.Format("%d ms", encrypt_key_time);
		m_strEncryptMessageTime.Format("%d ms",encrypt_message_time);
		m_strCalHashTime.Format("%d ms",cal_hash_time);
	}
	else
	{
		m_strSendTime.Format("%d ms",end_time - start_time);
	}

	UpdateData(FALSE);
	this->GetDlgItem(IDC_BUTTON5)->EnableWindow(TRUE);
}	
	

⌨️ 快捷键说明

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