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

📄 des_vcdlg.cpp

📁 VC++6.0环境下的DES加密解密算法测试程序
💻 CPP
字号:
// Des_VCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Des_VC.h"
#include "Des_VCDlg.h"
#include "MySocket.h"
#include "d3des.c"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
int m_iType,m_iPort;
CString m_strName;

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()
private:
	CMySocket m_sConnectSocket;
	CMySocket m_sListenSocket;
};

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

/////////////////////////////////////////////////////////////////////////////
// CDes_VCDlg dialog

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

void CDes_VCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDes_VCDlg)
	DDX_Text(pDX, IDC_EDIT_DECRYPT, m_sDecrypt);
	DDX_Text(pDX, IDC_EDIT_ENCRYPT, m_sEncrypt);
	DDX_Text(pDX, IDC_EDIT_PLAIN, m_sPlain);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDes_VCDlg, CDialog)
	//{{AFX_MSG_MAP(CDes_VCDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDes_VCDlg message handlers

BOOL CDes_VCDlg::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_iType = 0;
	m_strName ="loopback";
	m_iPort = 4000;
	UpdateData(FALSE);
	m_sConnectSocket.SetParent(this);
	m_sListenSocket.SetParent(this);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CDes_VCDlg::OnOK() 
{
	// TODO: Add extra validation here
	BYTE PlainMsg[1024];
	BYTE EncryptMsg[1024];
	BYTE DecryptMsg[1024];
	BYTE fromMsg[8];
	BYTE intoMsg[8];
	BYTE Key[16]={0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xA,0xB,0xC,0xD,0xE,0xF};
	int i,nLen,j;
	CDes_VCDlg *pDlg = (CDes_VCDlg *)this;
	CEdit * pEdit;
	pEdit = (CEdit*)pDlg->GetDlgItem(IDC_EDIT_PLAIN);
	pEdit->GetWindowText(m_sPlain);
	memset(PlainMsg,0,1024);
	memset(EncryptMsg,0,1024);
	memset(DecryptMsg,0,1024);
	
	nLen=m_sPlain.GetLength();
	for(i=0;i<nLen;i++)
		PlainMsg[i]=m_sPlain[i];
	PlainMsg[nLen]=0;
	des2key(Key,EN0);
	for(i=0;i<nLen;i+=8)
	{
		memset(fromMsg,0,8);
		memset(intoMsg,0,8);
		memcpy(fromMsg,PlainMsg+i,8);
		Ddes(fromMsg,intoMsg);
		memcpy(EncryptMsg+i,intoMsg,8);
	}
	m_sEncrypt=(char*)EncryptMsg;
	pEdit = (CEdit*)pDlg->GetDlgItem(IDC_EDIT_ENCRYPT);
	pEdit->SetWindowText(m_sEncrypt);
	des2key(Key,DE1);
	for(i=0;i<nLen;i+=8)
	{
		memset(fromMsg,0,8);
		memset(intoMsg,0,8);
		memcpy(fromMsg,EncryptMsg+i,8);
		Ddes(fromMsg,intoMsg);
		memcpy(DecryptMsg+i,intoMsg,8);
	}
	m_sDecrypt=(char*)DecryptMsg;
	pEdit = (CEdit*)pDlg->GetDlgItem(IDC_EDIT_DECRYPT);
	pEdit->SetWindowText(m_sDecrypt);
	//CDialog::OnOK();
}

void CDes_VCDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
		OnClose();
	CDialog::OnCancel();
}

void CDes_VCDlg::OnClose()
{
	//Close the connected socket
	m_sConnectSocket.Close();
}

void CDes_VCDlg::OnAccept()
{
	//Accept the connection request
	m_sListenSocket.Accept(m_sConnectSocket);
	//Enable the text and message controls 
	//GetDlgItem(IDC_EMSG)->EnableWindow(	TRUE);
	//GetDlgItem(IDC_BSEND)->EnableWindow(TRUE);
	//GetDlgItem(IDC_STATICMSG)->EnableWindow(TRUE);
}

void CDes_VCDlg::OnConnect()
{
	//Enable the text and message controls
	//GetDlgItem(IDC_EMSG)->EnableWindow(	TRUE);
	//GetDlgItem(IDC_BSEND)->EnableWindow(TRUE);
	//GetDlgItem(IDC_STATICMSG)->EnableWindow(TRUE);
	//GetDlgItem(IDC_BCLOSE)->EnableWindow(TRUE);
}

void CDes_VCDlg::OnReceive()
{
	char *pBuf = new char[1025];
	int   iBufSize = 1024;
	int   iRcvd,iSent;
	int   Flag_Rcv;
	CString strRecvd;
	
	//Receive  the message
	iRcvd = m_sConnectSocket.Receive(pBuf,iBufSize);
	//Did we receive anything?
	if(iRcvd == SOCKET_ERROR)
	{
	}
	else
	{
		//Truncate the end of the message
		pBuf[iRcvd] = NULL;
		Flag_Rcv=1;
		//Copy the message to a CString
		strRecvd = pBuf;
		//Add the message to the received list box
		//m_ctlRecvd.AddString(strRecvd);
		if(Flag_Rcv){
			Flag_Rcv=0;
			iSent = m_sConnectSocket.Send(LPCSTR(strRecvd),iRcvd);
		}
		//Sync the variable with controls
		UpdateData(FALSE);
	}
}

⌨️ 快捷键说明

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