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

📄 seccommdlg.cpp

📁 AES加密算法对socket通信过程进行加密传输
💻 CPP
字号:
// SecCommDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SecComm.h"
#include "SecCommDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CSecCommDlg dialog

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

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

BEGIN_MESSAGE_MAP(CSecCommDlg, CDialog)
	//{{AFX_MSG_MAP(CSecCommDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_LISTEN, OnListen)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_BN_CLICKED(IDB_CLEAR, OnClear)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSecCommDlg message handlers

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

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

void CSecCommDlg::OnListen() 
{
	// TODO: Add your control notification handler code here
	if (isRunning)
		//如果正在运行,按下为停止
	{
		isRunning = false;
		mySock->Close();
		mySockNew->Close();
		GetDlgItem(IDC_LISTEN)->SetWindowText("运行");
		GetDlgItem(IDC_PORT)->EnableWindow(true);
		GetDlgItem(IDC_EDIT1)->EnableWindow(false);
		GetDlgItem(IDC_EDIT2)->EnableWindow(false);
		GetDlgItem(IDC_SEND)->EnableWindow(false);
		m_icon.SetIcon(AfxGetApp()->LoadIcon(IDI_DISCONNECTED));
	}
	else 
	{
		UpdateData(true);
		if (mySock->Create(m_port,SOCK_STREAM,0)==0)
		{
			AfxMessageBox("无法创建Socket,有可能是端口被占用");
			return;
		}
		mySock->Listen();
		isRunning = true;
		GetDlgItem(IDC_LISTEN)->SetWindowText("停止");
		GetDlgItem(IDC_PORT)->EnableWindow(false);
		m_icon.SetIcon(AfxGetApp()->LoadIcon(IDI_WAITING));
	}
}

bool CSecCommDlg::ServerInit()
{
	isRunning = false;
	m_port = 911;
	m_method = 0;
	m_passwd = "1234";
	UpdateData(false);

	mySock = new MySocket(this);
	mySockNew = new MySocket(this);

	GetDlgItem(IDC_EDIT1)->EnableWindow(false);
	GetDlgItem(IDC_EDIT2)->EnableWindow(false);
	GetDlgItem(IDC_SEND)->EnableWindow(false);
	m_icon.SetIcon(AfxGetApp()->LoadIcon(IDI_DISCONNECTED));

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

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

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

void CSecCommDlg::ServerAccept()
{
	mySock->Accept(*mySockNew);
	GetDlgItem(IDC_EDIT1)->EnableWindow(true);
	GetDlgItem(IDC_EDIT2)->EnableWindow(true);
	GetDlgItem(IDC_SEND)->EnableWindow(true);
	m_icon.SetIcon(AfxGetApp()->LoadIcon(IDI_CONNECTED));
}

void CSecCommDlg::ServerReceive()
{
	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;

	mySockNew->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(key,0,keysize);
	mySockNew->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 CSecCommDlg::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);

	mySockNew->Send(&size,sizeof(size));
	mySockNew->Send(ct,size);
}

void CSecCommDlg::ServerSocketClose()
{
	AfxMessageBox("客户端已断开连接");
	isRunning = false;
	mySock->Close();
	mySockNew->Close();
	GetDlgItem(IDC_LISTEN)->SetWindowText("运行");
	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(IDC_SEND)->EnableWindow(false);
	m_icon.SetIcon(AfxGetApp()->LoadIcon(IDI_DISCONNECTED));
}

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

⌨️ 快捷键说明

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