despedlg.cpp

来自「使用VC实现DES加密中的EP置换。是一份很好的网络安全学习材料。」· C++ 代码 · 共 391 行

CPP
391
字号
// desPEDlg.cpp : implementation file
//

#include "stdafx.h"
#include "desPE.h"
#include "desPEDlg.h"
#include "math.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()

/////////////////////////////////////////////////////////////////////////////
// CDesPEDlg dialog

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

void CDesPEDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDesPEDlg)
	DDX_Text(pDX, IDC_minwen, m_minwen);
	DDX_Text(pDX, IDC_miwen, m_miwen);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDesPEDlg, CDialog)
	//{{AFX_MSG_MAP(CDesPEDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_E, OnE)
	ON_BN_CLICKED(IDC_P, OnP)
	ON_BN_CLICKED(IDC_change, Onchange)
	ON_EN_CHANGE(IDC_minwen, OnChangeminwen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDesPEDlg message handlers

BOOL CDesPEDlg::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
CButton *but=(CButton *)GetDlgItem(IDC_E);
	but->SetCheck(1);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CDesPEDlg::OnOK() 
{
	// TODO: Add extra validation here
	this->UpdateData(TRUE);

	//获得历史明文
	int mw[33];
FILE *fps;
fps=fopen("明文二进制表示.txt","r");
for(int i=1;i<=32;i++)    
      fscanf(fps,"%d\n",&mw[i]);
	fclose(fps);
	int ch=0;
		m_minwen=""; 
		for(i=0;i<=3;i++)
		{
			for(int j=8;j>=1;j--)
			{
				ch=ch+mw[i*8+j]*(int)pow(2,8-j);
			}
			m_minwen=m_minwen+(char)ch;
			ch=0;
		}


//获得密文
	m_miwen="";
	CString str;
	this->GetDlgItem(IDC_change)->GetWindowText(str);
	if(str=="进行E置换")
	{
		int mw[49];
FILE *fp;
fp=fopen("E密文二进制表示.txt","r");
for(int i=1;i<=48;i++)    
      fscanf(fp,"%d\n",&mw[i]);
fclose(fp);
for(i=1;i<=48;i++)
	{
		m_miwen=m_miwen+(char)(48+mw[i]);
		if(i%8==0)
          m_miwen=m_miwen+" ";
	}
	}
	else
	{
		int mw[33];
FILE *fp;
fp=fopen("P密文二进制表示.txt","r");
for(int i=1;i<=32;i++)    
      fscanf(fp,"%d\n",&mw[i]);
fclose(fp);
for(i=1;i<=32;i++)
	{
		m_miwen=m_miwen+(char)(48+mw[i]);
		if(i%8==0)
          m_miwen=m_miwen+" ";
	}

	}
	this->UpdateData(FALSE);
	//CDialog::OnOK();
}

void CDesPEDlg::OnE() 
{
	// TODO: Add your control notification handler code here
	this->GetDlgItem(IDC_STATICMinwen)->SetWindowText("E置换明文");
	this->GetDlgItem(IDC_STATICMIwen)->SetWindowText("E置换密文");
	this->GetDlgItem(IDC_change)->SetWindowText("进行E置换");
}

void CDesPEDlg::OnP() 
{
	// TODO: Add your control notification handler code here
	this->GetDlgItem(IDC_STATICMinwen)->SetWindowText("P置换明文");
	this->GetDlgItem(IDC_STATICMIwen)->SetWindowText("P置换密文");
	this->GetDlgItem(IDC_change)->SetWindowText("进行P置换");
}

void CDesPEDlg::Onchange() 
{
	// TODO: Add your control notification handler code here
	this->UpdateData(TRUE);
	int mw[33];
FILE *fp;
fp=fopen("明文二进制表示.txt","r");
for(int i=1;i<=32;i++)    
      fscanf(fp,"%d\n",&mw[i]);
	fclose(fp);
	m_miwen="";
	CString str;
	this->GetDlgItem(IDC_change)->GetWindowText(str);
ChangeMinwenTo2();

	
	if(str=="进行E置换")
	{
		int EM[49];
		int E[49]={32,1,2,3,4,5,
	           4,5,6,7,8,9,
			   8,9,10,11,12,13,
			   12,13,14,15,16,17,
			   16,17,18,19,20,21,
			   20,21,22,23,24,25,
			   24,25,26,27,28,29,
			   28,29,30,31,32,1};

		for(int j=1;j<=48;j++)
			EM[j]=mw[E[j-1]];

		fp=fopen("E密文二进制表示.txt","w");
		for(int i=1;i<=48;i++)
		{
			fprintf(fp,"%d\n",EM[i]);
		}
		fclose(fp);
		int ch=0;
		m_miwen=""; 
		for(i=0;i<=3;i++)
		{
			for(j=8;j>=1;j--)
			{
				ch=ch+EM[i*8+j]*(int)pow(2,8-j);
			}
			m_miwen=m_miwen+(char)ch;
			ch=0;
		}
		
	}



	else
	{
		int PM[33];
		int P[33]={16,7,20,21,
			29,12,28,17,
			1,15,23,26,
			5,18,31,10,
			2,8,24,14,
			32,27,3,9,
			19,13,30,6,
			22,11,4,25};
		for(int j=1;j<=32;j++)
			PM[j]=mw[P[j-1]];
		
		fp=fopen("P密文二进制表示.txt","w");
		for(int i=1;i<=48;i++)
		{
			fprintf(fp,"%d\n",PM[i]);
		}
		fclose(fp);
					int ch=0;
    m_miwen=""; 
	for(i=0;i<=3;i++)
	{
      for(j=8;j>=1;j--)
	  {
         ch=ch+PM[i*8+j]*(int)pow(2,8-j);
	  }
	  m_miwen=m_miwen+(char)ch;
	  ch=0;
	}
		
	}


this->UpdateData(FALSE);
}

void CDesPEDlg::ChangeMinwenTo2()
{
	FILE *fp;
	char ch;
	static int i,j,str[8];
	this->UpdateData(TRUE);

	fp=fopen("明文二进制表示.txt","w");
	for(i=0;i<4;i++)
	{
	  ch=m_minwen.GetAt(i);
	  for(j=0;j<8;j++)
	  {
         str[j]=ch%2;
		 ch=ch/2;
	  }
	  for(j=7;j>=0;j--)
	    fprintf(fp,"%d\n",str[j]);
	}
	fclose(fp);

}

void CDesPEDlg::OnChangeminwen() 
{
	// 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.
	this->UpdateData(TRUE);
	if(m_minwen.GetLength()>4)	
	{
		m_minwen=m_minwen.Left(4);
		CEdit *edit=(CEdit *)this->GetDlgItem(IDC_minwen);
		edit->SetSel(0,-1,TRUE);
	this->UpdateData(FALSE);
	}
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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