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

📄 desdlg.cpp

📁 DES算法源代码
💻 CPP
字号:
// DESDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DES.h"
#include "DESDlg.h"

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

//全局变量
bool M[64];				//明文分组
bool X[8][8];			//明文分组经IP置换后的得到的矩阵
bool Y[8][8];			//该矩阵经IP逆置换后的得到密文分组
bool C[64];				//密文分组
bool L[4][8],R[4][8];	//存放每轮迭代后的结果
bool KEY[8][8];			//密钥

//初始置换函数
void Ip()
{
	for(int i=0;i<8;i++)
	{
		for(int j=0;j<8;j++)
		{
			X[i][j]=M[IP[i][j]-1];
		}
	}
}

//逆初始置换函数
void AdverseIp()
{
	int line,row;
	for(int i=0;i<8;i++)
	{
		for(int j=0;j<8;j++)
		{
			line=(ADVERSEIP[i][j]-1)/8;
			row=(ADVERSEIP[i][j]-1)%8;
			C[i*8+j]=Y[line][row];
		}
	}
}

void LeftMove(bool A[8][7],int times)
{
	int line,row;
	bool temp;
	
	for(int i=0;i<times;i++)
	{
		temp=A[0][0];
		for(int j=0;j<8;j++)
		{
			for(int k=0;k<7;k++)
			{
				line=(j*7+k+1)/7%8;
				row =(j*7+k+1)%7;
				A[j][k]=A[line][row];
			}
		}		
		A[7][6]=A[3][6];
		A[3][6]=temp;		
	}
}

void GetSubKey(bool SubKEY[16][8][7])
{
	bool PCKEY[8][7];	//存放密钥经置换1后的结果
	bool LoopKEY[8][8];	//存放每次迭代后的子密钥,用于下一次迭代的计算
	for(int i=0;i<8;i++)
	{
		for(int j=0;j<7;j++)
		{
			LoopKEY[i][j]=KEY[i][j];
		}
	}
	int line,row;
	for(int count=0;count<16;count++)
	{
		for(i=0;i<8;i++)
		{
			for(int j=0;j<7;j++)
			{
				line=(PC_1[i][j]-1)/8;
				row =(PC_1[i][j]-1)%8;
				PCKEY[i][j]=LoopKEY[line][row];
			}
		}

		//执行左移操作
		LeftMove(PCKEY,LMoveTimes[count]);
	
		//生成子密钥
		for(i=0;i<8;i++)
		{
			for(int j=0;j<6;j++)
			{
				line=(PC_2[i][j]-1)/7;
				row =(PC_2[i][j]-1)%7;
				SubKEY[count][i][j]=PCKEY[line][row];
			}
		}

		for(i=0;i<8;i++)
		{
			for(int j=0;j<7;j++)
			{
				LoopKEY[i][j]=PCKEY[i][j];
			}
		}
	}
}

void Loop(bool SubKEY[16][8][7],int count)
{
	int line,row;

	//拆分64位明文为L和R
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<8;j++)
		{
			//由于Li=Ri-1,所以此处便可求出Li
			L[i][j]=X[i+4][j];
		}
	}

	//扩充置换E
	bool ExtendR[8][6];
	for(i=0;i<8;i++)
	{
		for(int j=0;j<6;j++)
		{
			line=(E[i][j]-1)/8;
			row =(E[i][j]-1)%8;
			ExtendR[i][j]=L[line][row];
		}
	}
	
	//将ExtendR和SubKEY进行异或操作
	for(i=0;i<8;i++)
	{
		for(int j=0;j<6;j++)
		{
			if(ExtendR[i][j]==SubKEY[count][i][j])
			{
				ExtendR[i][j]=false;
			}
			else
			{
				ExtendR[i][j]=true;
			}
		}
	}

	//用S盒对ExtendR进行代换选择
	bool SR[4][8];	//用于存放S盒代换选择后的结果
	for(i=0;i<4;i++)
	{
		for(int j=0;j<8;j++)
		{
			SR[i][j]=0;
		}
	}
	int s_result;
	for(i=0;i<8;i++)
	{
		line=(int)ExtendR[i][0]*2+(int)ExtendR[i][5];
		row =(int)ExtendR[i][1]*8+(int)ExtendR[i][2]*4+(int)ExtendR[i][3]*2+(int)ExtendR[i][4];
		s_result=S[i][line][row];

		for(int j=3;s_result!=0&&j>=0;j--)
		{
			SR[i/2][i%2*4+j]=(bool)(s_result%2); 
			s_result/=2;
		}
	
	}

	//对S盒代换后的结果SR[4][8]进行P置换
	bool PR[4][8];
	for(i=0;i<4;i++)
	{
		for(int j=0;j<8;j++)
		{
			line=(P[i][j]-1)/8;
			row =(P[i][j]-1)%8;
			PR[i][j]=SR[line][row];
		}
	}


	//对Li-1和PR进行异或操作
	for(i=0;i<4;i++)
	{
		for(int j=0;j<8;j++)
		{
			if(X[i][j]==PR[i][j])
			{
				R[i][j]=false;
			}
			else
			{
				R[i][j]=true;
			}
		}
	}

	for(i=0;i<4;i++)
	{
		for(int j=0;j<8;j++)
		{
			X[i][j]  =L[i][j];
			X[i+4][j]=R[i][j];
		}
	}

}


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

/////////////////////////////////////////////////////////////////////////////
// CDESDlg dialog

CDESDlg::CDESDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDESDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDESDlg)
		// 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 CDESDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDESDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDESDlg, CDialog)
	//{{AFX_MSG_MAP(CDESDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ENCRIPT, OnEncript)
	ON_BN_CLICKED(IDC_DECRIPT, OnDecript)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDESDlg message handlers

BOOL CDESDlg::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
	CString str="0000000000000000000000000000000000000000000000000000000000000000";
	SetDlgItemText(IDC_EN_M,str);
	str="01001010011001011011010011001011100100100101010101100100";
	SetDlgItemText(IDC_KEY,str);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CDESDlg::OnEncript() 
{
	// TODO: Add your control notification handler code here

	//获取明文
	CString m;
	GetDlgItemText(IDC_EN_M,m);
	int len;
	len=m.GetLength();
	if(len!=64)
	{
		MessageBox("明文长度不正确,请检查!");
		return ;
	}
	CString str="明文输入不规范,请检查!";
	
	for(int i=0;i<64;i++)
	{
		if(m[i]=='1')
		{
			M[i]=true;
		}
		else if(m[i]=='0')
		{
			M[i]=false;
		}
		else
		{
			MessageBox(str);
		}
	}

	//获取密钥
	CString key;
	GetDlgItemText(IDC_EN_KEY,key);
	len=key.GetLength();
	if(len!=56)
	{
		MessageBox("密钥长度不正确,请检查!");
		return ;
	}
	str="密钥输入不规范,请检查!";
	for(i=0;i<56;i++)
	{
		if(key[i]=='1')
		{
			KEY[i/7][i%7]=true;
		}
		else if(key[i]=='0')
		{
			KEY[i/7][i%7]=false;
		}
		else
		{
			MessageBox(str);
		}
	}

	//产生16个子密钥
	bool SubKEY[16][8][7];
	GetSubKey(SubKEY);

	//初始置换
	Ip();

	//16轮代换
	for(i=0;i<16;i++)
	{
		Loop(SubKEY,i);
		
	}

	//给Y[8][8]赋值
	for(i=0;i<4;i++)
	{
		for(int j=0;j<8;j++)
		{
			Y[i][j]  =R[i][j];
			Y[i+4][j]=L[i][j];
		}
	}

	//逆初始置换,得到密文
	AdverseIp();

	//密文输出
	CString c="";
	for(i=0;i<64;i++)
	{
		if(C[i])
		{
			c+="1";
		}
		else
		{
			c+="0";
		}		
	}

	SetDlgItemText(IDC_EN_C,c);

}



void CDESDlg::OnDecript() 
{
	// TODO: Add your control notification handler code here
	//获取明文
	CString m;
	GetDlgItemText(IDC_DE_C,m);
	int len;
	len=m.GetLength();
	if(len!=64)
	{
		MessageBox("密文长度不正确,请检查!");
		return ;
	}
	CString str="密文输入不规范,请检查!";
	
	for(int i=0;i<64;i++)
	{
		if(m[i]=='1')
		{
			M[i]=true;
		}
		else if(m[i]=='0')
		{
			M[i]=false;
		}
		else
		{
			MessageBox(str);
		}
	}

	//获取密钥
	CString key;
	GetDlgItemText(IDC_DE_KEY,key);
	len=key.GetLength();
	if(len!=56)
	{
		MessageBox("密钥长度不正确,请检查!");
		return ;
	}
	str="密钥输入不规范,请检查!";
	for(i=0;i<56;i++)
	{
		if(key[i]=='1')
		{
			KEY[i/7][i%7]=true;
		}
		else if(key[i]=='0')
		{
			KEY[i/7][i%7]=false;
		}
		else
		{
			MessageBox(str);
		}
	}

	//产生16个子密钥
	bool SubKEY[16][8][7];
	GetSubKey(SubKEY);
	bool TKEY[16][8][7];
	for(i=0;i<16;i++)
	{
		for(int j=0;j<8;j++)
		{
			for(int k=0;k<7;k++)
			{
				TKEY[15-i][j][k]=SubKEY[i][j][k];
			}
		}
	}

	//初始置换
	Ip();

	//16轮代换
	for(i=0;i<16;i++)
	{
		Loop(TKEY,i);		
	}

	//给Y[8][8]赋值
	for(i=0;i<4;i++)
	{
		for(int j=0;j<8;j++)
		{
			Y[i][j]  =R[i][j];
			Y[i+4][j]=L[i][j];
		}
	}

	//逆初始置换,得到密文
	AdverseIp();

	//密文输出
	CString c="";
	for(i=0;i<64;i++)
	{
		if(C[i])
		{
			c+="1";
		}
		else
		{
			c+="0";
		}		
	}

	SetDlgItemText(IDC_DE_M,c);
}

⌨️ 快捷键说明

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