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

📄 serverdlg.cpp

📁 基于挑战—应答模式的服务器认证程序
💻 CPP
字号:
// ServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"
#include "rand.h"
#include "md5.h"
#include "des.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()

/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialog

CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CServerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CServerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CServerDlg)
	DDX_Control(pDX, IDC_EDIT_INFO, m_EditInfo);
	DDX_Control(pDX, IDC_EDIT_SendData, m_editSendData);
	DDX_Control(pDX, IDC_EDIT_REVData, m_editRecvData);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
	//{{AFX_MSG_MAP(CServerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(Listen, OnListen)
	ON_BN_CLICKED(CreatRan, OnCreatRan)
	ON_BN_CLICKED(CreatKey, OnCreatKey)
	ON_BN_CLICKED(IDC_BUTTON_RECV_DATA, OnButtonRecvData)
	ON_BN_CLICKED(SendData, OnSendData)
	ON_WM_DESTROY()
	ON_BN_CLICKED(DeCry, OnDeCry)
	ON_EN_CHANGE(IDC_EDIT_INFO, OnChangeEditInfo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CServerDlg message handlers

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

void CServerDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

void CServerDlg::OnListen() 
{
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2,2),&wsaData);

	m_sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	SOCKADDR_IN servaddr;
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(3344);
	servaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	
	bind(m_sock,(sockaddr*)&servaddr,sizeof(servaddr));
	listen(m_sock, 2);
	


		SOCKADDR_IN clientaddr;
		int addrlen = sizeof(clientaddr);
		m_sNew = accept(m_sock,(sockaddr*)&clientaddr,&addrlen);
		
		char buf[32];
    	ZeroMemory(buf,32);
		recv(m_sNew,buf,32,0);
		m_editRecvData.SetWindowText(buf);
		m_value1 = (ULONG)atol(buf);
}

void CServerDlg::OnCreatRan() 
{
	// TODO: Add your control notification handler code here
	my_srand((unsigned)(time(NULL)));
	m_value2 = my_rand();
	CString data;
	data.Format("%d",m_value2);
	send(m_sNew,data.GetBuffer(0),data.GetLength(),0);
	m_EditInfo.SetWindowText(data);

}

void CServerDlg::OnCreatKey() 
{
	// TODO: Add your control notification handler code here
	ULONG uValue = m_value2 + m_value1;
	CString temp;
	temp.Format("%d",uValue);
	m_key = MD5String(temp.GetBuffer(0));
	m_EditInfo.SetWindowText(m_key);

}

void CServerDlg::OnButtonRecvData() 
{
	// TODO: Add your control notification handler code here
	char buf[1024];
	ZeroMemory(buf,1024);
	recv(m_sNew,buf,1000,0);
	m_editRecvData.SetWindowText(buf);
}

void CServerDlg::OnSendData() 
{
	// TODO: Add your control notification handler code here
	CString senddata;
	m_editSendData.GetWindowText(senddata);
	send(m_sNew,senddata.GetBuffer(0),senddata.GetLength(),0);
}

void CServerDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	closesocket(m_sock);
	closesocket(m_sNew);
}

void CServerDlg::OnDeCry() 
{
	// TODO: Add your control notification handler code here
	CString RecvData;
	m_editRecvData.GetWindowText(RecvData);
	 

	char nowkey[32];
	char inbuff[32], outbuff[32];
	memcpy(nowkey, (LPCTSTR)m_key, 32);
	memcpy(inbuff, (LPCTSTR)RecvData, 32);

    DES jm;
    jm.Des_Go(outbuff, inbuff, sizeof(inbuff),nowkey, sizeof(nowkey),  DECRYPT);
    for(int i=0;i<8;i++)
       RecvData +=outbuff[i];
    UpdateData(FALSE);	
	m_editRecvData.SetWindowText(RecvData);
}

void CServerDlg::OnChangeEditInfo() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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