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

📄 secclientdlg.cpp

📁 AES加密算法
💻 CPP
字号:
// SecClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SecClient.h"
#include "SecClientDlg.h"
#include "MySocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include "tomcrypt.h"
#pragma comment(lib,"tomcrypt")
char method[3][5]={"des","3des","aes"};

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

/////////////////////////////////////////////////////////////////////////////
// CSecClientDlg dialog

CSecClientDlg::CSecClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSecClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSecClientDlg)
	m_recvstr = _T("");
	m_sendstr = _T("");
	m_passwd = _T("");
	m_port = 0;
	m_method = -1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSecClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSecClientDlg)
	DDX_Control(pDX, IDC_IPADDRESS, m_ipctl);
	DDX_Text(pDX, IDC_EDIT1, m_recvstr);
	DDX_Text(pDX, IDC_EDIT2, m_sendstr);
	DDX_Text(pDX, IDC_PASSWD, m_passwd);
	DDX_Text(pDX, IDC_PORT, m_port);
	DDV_MinMaxInt(pDX, m_port, 1, 65535);
	DDX_CBIndex(pDX, IDC_METHOD, m_method);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSecClientDlg, CDialog)
	//{{AFX_MSG_MAP(CSecClientDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDB_CONNECT, OnConnect)
	ON_BN_CLICKED(IDB_SEND, OnSend)
	ON_BN_CLICKED(IDB_CLEAR, OnClear)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSecClientDlg message handlers

BOOL CSecClientDlg::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
	ClientInit();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

bool CSecClientDlg::ClientInit()
{
	isRunning = false;
	m_ipctl.SetWindowText("127.0.0.1");
	m_port = 911;
	m_method = 0;
	m_passwd = "1234";
	UpdateData(false);

	mySock = new MySocket(this);

	GetDlgItem(IDC_EDIT1)->EnableWindow(false);
	GetDlgItem(IDC_EDIT2)->EnableWindow(false);
	GetDlgItem(IDB_SEND)->EnableWindow(false);

	register_cipher(&des_desc);
	register_cipher(&des3_desc);
	register_cipher(&aes_desc);
	return true;
}

void CSecClientDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	mySock->Close();

	unregister_cipher(&des_desc);
	unregister_cipher(&des3_desc);
	unregister_cipher(&aes_desc);
}

void CSecClientDlg::OnConnect() 
{
	// TODO: Add your control notification handler code here
	if (isRunning)
	{
		isRunning = false;
		mySock->Close();
		GetDlgItem(IDB_CONNECT)->SetWindowText("连接");
		GetDlgItem(IDC_IPADDRESS)->EnableWindow(true);
		GetDlgItem(IDC_PORT)->EnableWindow(true);
		GetDlgItem(IDC_EDIT1)->EnableWindow(false);
		GetDlgItem(IDC_EDIT2)->EnableWindow(false);
		GetDlgItem(IDB_SEND)->EnableWindow(false);
	}
	else
	{
		UpdateData(true);
		m_ipctl.GetWindowText(m_serverip);
		mySock->Create();
		if (mySock->Connect(m_serverip,m_port)==0)
		{
			AfxMessageBox("无法连接到服务器!");
			mySock->Close();
			return;
		}
		isRunning = true;
		GetDlgItem(IDB_CONNECT)->SetWindowText("断开");
		GetDlgItem(IDC_IPADDRESS)->EnableWindow(false);
		GetDlgItem(IDC_PORT)->EnableWindow(false);
		GetDlgItem(IDC_EDIT1)->EnableWindow(true);
		GetDlgItem(IDC_EDIT2)->EnableWindow(true);
		GetDlgItem(IDB_SEND)->EnableWindow(true);
	}
}

void CSecClientDlg::ClientReceive()
{
	int i, size, blocksize, keysize, desc_id;

	desc_id = find_cipher(method[m_method]);
	blocksize = cipher_descriptor[desc_id].block_length;
	keysize = cipher_descriptor[desc_id].max_key_length;

	unsigned char *pt,*ct,*key;
	symmetric_key skey;

	mySock->Receive(&size,sizeof(size));
	pt=(unsigned char*)malloc(size);
	ct=(unsigned char*)malloc(size+1);
	key=(unsigned char*)malloc(keysize);
	memset(pt,0,size);
	memset(ct,0,size+1);
	memset(pt,0,keysize);
	mySock->Receive(ct,size);

	strncpy((char*)key,LPCSTR(m_passwd),keysize);

	cipher_descriptor[desc_id].setup(key,keysize,0,&skey);
	for(i=0;i<size;i+=blocksize)
		cipher_descriptor[desc_id].ecb_decrypt(&ct[i],&pt[i],&skey);
	cipher_descriptor[desc_id].done(&skey);

	UpdateData(true);
	m_recvstr+=pt;
	m_recvstr+="\r\n";
	UpdateData(false);
}

void CSecClientDlg::ClientSocketClose()
{
	AfxMessageBox("服务端已断开连接");
	isRunning = false;
	mySock->Close();
	GetDlgItem(IDB_CONNECT)->SetWindowText("连接");
	GetDlgItem(IDC_IPADDRESS)->EnableWindow(true);
	GetDlgItem(IDC_PORT)->EnableWindow(true);
	GetDlgItem(IDC_METHOD)->EnableWindow(true);
	GetDlgItem(IDC_PASSWD)->EnableWindow(true);
	GetDlgItem(IDC_EDIT1)->EnableWindow(false);
	GetDlgItem(IDC_EDIT2)->EnableWindow(false);
	GetDlgItem(IDB_SEND)->EnableWindow(false);
}

void CSecClientDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);

	int i, size, blocksize, keysize, desc_id;
	int n = m_sendstr.GetLength();

	desc_id = find_cipher(method[m_method]);
	blocksize = cipher_descriptor[desc_id].block_length;
	keysize = cipher_descriptor[desc_id].max_key_length;
	size = n + blocksize - n % blocksize;

	unsigned char *pt,*ct,*key;
	symmetric_key skey;

	pt=(unsigned char*)malloc(size);
	ct=(unsigned char*)malloc(size);
	key=(unsigned char*)malloc(keysize);
	memset(pt,0,size);
	memset(ct,0,size);
	memset(key,0,keysize);

	strncpy((char*)pt,LPCSTR(m_sendstr),size);
	strncpy((char*)key,LPCSTR(m_passwd),keysize);

	cipher_descriptor[desc_id].setup(key,keysize,0,&skey);
	for(i=0;i<size;i+=blocksize)
		cipher_descriptor[desc_id].ecb_encrypt(&pt[i],&ct[i],&skey);
	cipher_descriptor[desc_id].done(&skey);

	mySock->Send(&size,sizeof(size));
	mySock->Send(ct,size);

	UpdateData(false);
}

void CSecClientDlg::OnClear() 
{
	// TODO: Add your control notification handler code here
	m_recvstr="";
	UpdateData(false);
}

⌨️ 快捷键说明

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