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

📄 homework2dlg.cpp

📁 加密作业,实现简单的DES加密,完全实现DES加密的工作流程
💻 CPP
字号:
// homework2Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "homework2.h"
#include "homework2Dlg.h"
#include "MyEdit.h"
#include "About.h"
#include <math.h>

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

int ip[8]={2,6,3,1,4,8,5,7};
int ip_1[8]={4,1,3,5,7,2,8,6};
int ep[8]={4,1,2,3,2,3,4,1};
int s0[4][4]={1,0,3, 2 , 3,2,1,0,0,2,1,3,3,1,3,2};
int s1[4][4]={0,1,2,3,2,0,1,3,3,0,1,0,2,1,0,3};
int p4[4]={2,4,3,1};
int	p8[8]={6,3,7,4,8,5,10,9};
int p10[10]={3,5,2,7,4,10,1,9,8,6};
int k1,k2;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CHomework2Dlg::CHomework2Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHomework2Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEncryptDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}


class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTUS };
	//}}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 void OnPaint();
	//}}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)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHomework2Dlg dialog



void CHomework2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHomework2Dlg)
	DDX_Control(pDX, IDC_CLEAN, m_Clean);
	DDX_Control(pDX, IDC_PLAINEDIT, m_Plaintext);
	DDX_Control(pDX, IDC_OPHEREDIT, m_Ophertext);
	DDX_Control(pDX, IDC_CRYPTOEDIT, m_Cryptograph);
	DDX_Control(pDX, IDC_PLAINTEXT, m_PlainStatic);
	DDX_Control(pDX, IDC_OPHERTEXT, m_OpherStatic);
	DDX_Control(pDX, IDC_CRYPTOGRAPH, m_CryptoStatic);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHomework2Dlg, CDialog)
	//{{AFX_MSG_MAP(CHomework2Dlg)
	ON_WM_PAINT()
	ON_EN_CHANGE(IDC_PLAINEDIT, OnChangePlainedit)
	ON_EN_CHANGE(IDC_OPHEREDIT, OnChangeOpheredit)
	ON_EN_CHANGE(IDC_CRYPTOEDIT, OnChangeCryptoedit)
	ON_BN_CLICKED(IDC_ENCRYPT, OnEncrypt)
	ON_BN_CLICKED(IDC_DECRYPT, OnDecrypt)
	ON_BN_CLICKED(IDC_ABOUT, OnAbout)
	ON_BN_CLICKED(IDC_CLEAN, OnClean)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_CRYPTOGRAPH, OnCryptograph)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHomework2Dlg message handlers

BOOL CHomework2Dlg::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_Plaintext.SetLimitText(8);
	m_Ophertext.SetLimitText(10);
	m_Cryptograph.SetLimitText(8);

	pWndPt=GetDlgItem(IDC_ENCRYPT);
	pWndOt=GetDlgItem(IDC_DECRYPT);
	pWndPt->EnableWindow(false);
	pWndOt->EnableWindow(false);

	return TRUE;  // return TRUE  unless you set the focus to a control
}


// 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 CHomework2Dlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CRect rect;
	GetClientRect(&rect);
	CBrush brush(RGB(20,20,150));
	dc.FillRect(&rect,&brush);
	if (IsIconic())
	{
		
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		
		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.

void CHomework2Dlg::OnChangePlainedit() 
{
	CString pt,ot,ct;
	m_Plaintext.GetWindowText(pt);
	m_Ophertext.GetWindowText(ot);
	m_Cryptograph.GetWindowText(ct);
	pWndPt=GetDlgItem(IDC_ENCRYPT);
	pWndOt=GetDlgItem(IDC_DECRYPT);
	if(ot.GetLength()==10)
	{
		if(pt.GetLength()==8)
		{
			pWndPt->EnableWindow(true);
		}
		else
		{
			pWndOt->EnableWindow(true);
		}
	}
	else
	{
		pWndPt->EnableWindow(false);
		pWndOt->EnableWindow(false);
	}

	
}

void CHomework2Dlg::OnChangeOpheredit() 
{
// 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
	CString pt,ot,ct;
	m_Plaintext.GetWindowText(pt);
	m_Ophertext.GetWindowText(ot);
	m_Cryptograph.GetWindowText(ct);
	pWndPt=GetDlgItem(IDC_ENCRYPT);
	pWndOt=GetDlgItem(IDC_DECRYPT);
	if(ot.GetLength()==10)
	{
		if(pt.GetLength()==8)
		{
			pWndPt->EnableWindow(true);
		}
		else
		{
			pWndOt->EnableWindow(true);
		}
	}
	else
	{
		pWndPt->EnableWindow(false);
		pWndOt->EnableWindow(false);
	}
	
}

void CHomework2Dlg::OnChangeCryptoedit() 
{
	// 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
	CString pt,ot,ct;
	m_Plaintext.GetWindowText(pt);
	m_Ophertext.GetWindowText(ot);
	m_Cryptograph.GetWindowText(ct);
	pWndPt=GetDlgItem(IDC_ENCRYPT);
	pWndOt=GetDlgItem(IDC_DECRYPT);
	if(ot.GetLength()==10)
	{
		if(pt.GetLength()==8)
		{
			pWndPt->EnableWindow(true);
		}
		else
		{
			pWndOt->EnableWindow(true);
		}
	}
	else
	{
		pWndPt->EnableWindow(false);
		pWndOt->EnableWindow(false);
	}
}

void CHomework2Dlg::OnEncrypt() 
{
	// TODO: Add your control notification handler code here
	Encrypt();
	
}

void CHomework2Dlg::OnDecrypt() 
{
	// TODO: Add your control notification handler code here
	Decrypt();
	
}

void CHomework2Dlg::OnAbout() 
{
	// TODO: Add your control notification handler code here
//	CEncryptAbout EDlg;
//	EDlg.DoModal();
	CAboutDlg Dlg;
	Dlg.DoModal();
}

void CHomework2Dlg::OnClean() 
{
	// TODO: Add your control notification handler code here
	m_Cryptograph.SetWindowText("");
	m_Ophertext.SetWindowText("");
	m_Plaintext.SetWindowText("");
	
}

void CHomework2Dlg::OnExit() 
{
	// TODO: Add your control notification handler code here
		CDialog::OnOK();
	
}


void CHomework2Dlg::OnCryptograph() 
{
	// TODO: Add your control notification handler code here
	
}
int CHomework2Dlg::CharToDec(CString ls)
{
	int ta,tb=0;
	int n=ls.GetLength();
	CString temp;
	for(int i=0;i<n;i++)
	{
		temp=ls.GetAt(i);
		ta=atoi(temp);
		ta=ta<<(n-i-1);
		tb=tb | ta;
	}
	return tb;
}
void CHomework2Dlg::Decrypt()
{
	CString cryptograph,ophertext;
	int temp,tempo;
	m_Cryptograph.GetWindowText(cryptograph);
	m_Ophertext.GetWindowText(ophertext);
	temp=CharToDec(cryptograph);
	tempo=CharToDec(ophertext);
	load_k(tempo);
	temp=IP(temp);
	temp=FK(temp,k2);
	temp=SW(temp);
	temp=FK(temp,k1);
	temp=IP_1(temp);
	CString ls,rs;
	for(int i=0;i<8;i++)
	{
		int t,r=1;
		t=1;
		t=(temp & (r<<(7-i)))>>(7-i);;
		rs.Format("%d",t);
		ls+=rs;
	}
	m_Plaintext.SetWindowText(ls);	
}
void CHomework2Dlg::Encrypt()
{
	CString plaintext,ophertext;
	int temp,tempo;
	m_Plaintext.GetWindowText(plaintext);
	m_Ophertext.GetWindowText(ophertext);
	temp=CharToDec(plaintext);
	tempo=CharToDec(ophertext);
	load_k(tempo);
	temp=IP(temp);
	temp=FK(temp,k1);
	temp=SW(temp);
	temp=FK(temp,k2);
	temp=IP_1(temp);
	CString ls,rs;
	for(int i=0;i<8;i++)
	{
		int t,r=1;
		t=1;
		t=(temp&(r<<(7-i)))>>(7-i);;
		rs.Format("%d",t);
		ls+=rs;
	}
	m_Cryptograph.SetWindowText(ls);	
}

int CHomework2Dlg::EP(int ori)
{
	int i,temp=0;
	for(i=0;i<8;i++)
	{
		temp+=(((ori>>(4-ep[i]))%2)<<(7-i));
	}
	return temp;
}

int CHomework2Dlg::IP(int p) 
{
	int i,temp=0;
	for(i=0;i<8;i++)
	{
		temp+=(((p>>(8-ip[i]))%2)<<(7-i));
	}		
	return temp;
}

int CHomework2Dlg::IP_1(int p)
{	
	int i,temp=0;
	for(i=0;i<8;i++)
	{
		temp+=((p>>(8-ip_1[i]))%2)<<(7-i);
	}
	return temp;
}

int CHomework2Dlg::SW(int a)//实现八位二进制数左右四位交换
{
	int left,right;
	left=a/16;
	right=a%16;
	return (left+right*16);
}
void CHomework2Dlg::load_k(int key)
{
	int temp,temp1,left,right,left1,right1;
	temp=P10(key);
	left=temp/32;
	right=temp%32;
	left1=rleft_shift5(left,1);
	right1=rleft_shift5(right,1);
	temp=(left1<<5)+right1;
	temp=P8(temp);
	k1=temp;
	left=rleft_shift5(left1,2);
	right=rleft_shift5(right1,2);
	temp1=(left<<5)+right;
	temp1=P8(temp1);
	k2=temp1;
}

int CHomework2Dlg::S0(int a)
{
	int row,line;
	row=(a/8)*2+a%2;
	line=((a>>2)%2)*2+(a>>1)%2;
	return s0[row][line];
}
int CHomework2Dlg::S1(int a)
{
	int row,line;
	row=(a/8)*2+a%2;
	line=((a>>2)%2)*2+(a>>1)%2;
	return s1[row][line];
}
int CHomework2Dlg::P4(int a)
{
	int i,temp=0;
	for(i=0;i<4;i++)
	{
		temp+=((a>>(4-p4[i]))%2)<<(3-i);
	}
	return temp;
}
int CHomework2Dlg::P8(int a)
{
	int i,temp=0;
	for(i=0;i<8;i++)
	{
		temp+=(((a>>(10-p8[i]))%2)<<(7-i));
	}
	return temp;
}
int CHomework2Dlg::P10(int a)
{
	int i,temp=0;
	for(i=0;i<10;i++)
	{
		temp+=(((a>>(10-p10[i]))%2)<<(9-i));
	}
	return temp;
}
int CHomework2Dlg::rleft_shift5(int at,int num)   //五位的循环左移函数
{
	int left,right,ar;
	int temp=1;
	temp=(temp<<(5-num));
	right=at%temp;
	left=at/temp;	
	ar=(right<<num)+left;
	return ar;
}
int CHomework2Dlg::FK(int fk,int k)
{
	int ileft,iright,temp;
	int left,right;
	ileft=fk/16;
	iright=fk%16;
	temp=iright;
	temp=EP(temp);
	temp=temp^k;
	left=temp/16;
	right=temp%16;
	left=S0(left);
	right=S1(right);
	temp=left*4+right;
	temp=P4(temp);
	ileft=ileft^temp;
	temp=ileft*16+iright;
	return temp;
}	

void CHomework2Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	
}

void CAboutDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CRect rect;
	GetClientRect(&rect);
	CBrush brush(RGB(250,80,80));
	dc.FillRect(&rect,&brush);		

	// TODO: Add your message handler code here
	
	// Do not call CDialog::OnPaint() for painting messages
}

⌨️ 快捷键说明

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