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

📄 des_cryptiondlg.cpp

📁 自己编的DES加密算法
💻 CPP
字号:
// DES_CryptionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DES_Cryption.h"
#include "DES_CryptionDlg.h"
#include "string.h"

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

#define     lMax	10000
#define		lMes	64
#define		lKey	56


	int X_X0[lMes+1] = {  0,58,50,42,34,26,18,10,2,		//明文初始置换IP
							60,52,44,36,28,20,12,4,
							62,54,46,38,30,22,14,6,
							64,56,48,40,32,24,16,8,
							57,49,41,33,25,17,9,1,
							59,51,43,35,27,19,11,3,
							61,53,45,37,29,21,13,5,
							63,55,47,39,31,23,15,7  } ;
					 

	int PC_PC0[lKey+1] = {0,57,49,41,33,25,17,9,		//秘钥压缩换位64-56
							1,58,50,42,34,26,18,
							10,2,59,51,43,35,27,
							19,11,3,60,52,44,36,
							63,55,47,39,31,23,15,
							7,62,54,46,38,30,22,
							14,6,61,53,45,37,29,
							21,13,5,28,20,12,4	};
	int PC2_PC0[49] = {   0,14,17,11,24,1,5,			//秘钥压缩变换56-48
							3,28,15,6,21,10,
							23,19,12,4,26,8,
							16,7,27,20,13,2,
							41,52,31,37,47,55,
							30,40,51,45,33,48,
							44,49,39,56,34,53,
							46,42,50,36,29,32  } ;

	int E_E0[49] = {  0,32,1,2,3,4,5,					//E盒扩展置换 32-48
						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  } ;

	int C_C0[65] = {  0,40,8,48,16,56,24,64,32,			//逆初始置换IP-1
						39,7,47,15,55,23,63,31,
						38,6,46,14,54,22,62,30,
						37,5,45,13,53,21,61,29,
						36,4,44,12,52,20,60,28,
						35,3,43,11,51,19,59,27,
						34,2,42,10,50,18,58,26,
						33,1,41,9,49,17,57,25  };

	int S1[4][16] = {	14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,         //S盒      
						0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
						4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
						15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13  };

	int S2[4][16] = {	15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
						3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
						0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
						13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9  };

	int S3[4][16] = {	10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
						13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
						13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
						1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12  };

	int S4[4][16] = {	7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
						13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
						10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
						3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14  };

	int S5[4][16] = {	2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
						14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
						4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
						11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3  };

	int S6[4][16] = {	12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
						10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
						9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
						4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13  };

	int S7[4][16] = {	4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
						13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
						1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
						6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12  };

	int S8[4][16] = {	13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
						1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
						7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
						2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11  };


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

/////////////////////////////////////////////////////////////////////////////
// CDES_CryptionDlg dialog

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

void CDES_CryptionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDES_CryptionDlg)
	DDX_Control(pDX, IDC_EDIT_CRYPT, m_crypt);
	DDX_Text(pDX, IDC_EDIT_PATH, m_path);
	DDX_Text(pDX, IDC_EDIT_MESSAGE, m_message);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDES_CryptionDlg, CDialog)
	//{{AFX_MSG_MAP(CDES_CryptionDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_BROWER, OnButtonBrower)
	ON_BN_CLICKED(IDC_BUTTON_ENCRYPTION, OnButtonEncryption)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDES_CryptionDlg message handlers

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

void CDES_CryptionDlg::OnButtonBrower() //
{
	// TODO: Add your control notification handler code here


}



void CDES_CryptionDlg::Dec_0X(int x,int a[4])
{
	int i=3;
	memset(a,0,sizeof(a));
	while(x>0)
	{
		a[i] = x%2;
		x = x / 2 ;
		i--;
	}
}

void CDES_CryptionDlg::LeftMove(int a[28],int y)	//循环左移y个 ( y=1或2 )
{
	int i,k1,k2;
	k1=a[0];k2=a[1];
	if(y==1)
	{
		for(i=0;i<27;i++)
			a[i]=a[i+1];
		a[27] = k1;
	}
	else
	{
		for(i=0;i<26;i++)
		a[i]=a[i+2];
		a[26]=k1;
		a[27]=k2;
	}
}

void CDES_CryptionDlg::Map48_32(int E0[49],int P0[33],int K[49])
{
	int i,j,k;
	int E_K[49];
	for(i=1;i<=48;i++)
	{
		if(E0[i] == K[i])	E_K[i] = 0;
		else				E_K[i] = 1; //扩展后的L与压缩后的秘钥K异或运算变成E_K--------48位
	}
	
	int block_6[8][6];
	k = 1;
	for(i=0;i<8;i++)
	for(j=0;j<6;j++)
	{
		block_6[i][j] = E_K[k];		//48位E_K分成8块
		k++;
	}
	int rep,ir,ic;
	int block_4[8][4];
	for(i=0;i<8;i++)
	{//6位 ---> 4位
		ir = block_6[i][0] * 2 + block_6[i][5] ;
		ic = block_6[i][1] * 8 + block_6[i][2] * 4 + block_6[i][3] * 2 + block_6[i][4] ;
		if(i==0)			rep = S1[ir][ic];
		else if(i==1)		rep = S1[ir][ic];
		else if(i==2)		rep = S1[ir][ic];
		else if(i==3)		rep = S1[ir][ic];
		else if(i==4)		rep = S1[ir][ic];
		else if(i==5)		rep = S1[ir][ic];
		else if(i==6)		rep = S1[ir][ic];
		else				rep = S1[ir][ic];
		Dec_0X(rep,block_4[i]);		//十进制转化成二进制		
	}

	int P[33];
	for(i=0,j=1;i<8;i++,j+=4)
	{//8个4位块组合成32位P
		P[j]   = block_4[i][0];
		P[j+1] = block_4[i][1];
		P[j+2] = block_4[i][2];
		P[j+3] = block_4[i][3];
	}
	int P_P0[33] = {  0,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(i=1;i<=32;i++)
		P0[i] = P[P_P0[i]];
}


void CDES_CryptionDlg::F(int L0[32],int R0[32],int K[49])
{
	//F变换
	int i;
	int E0[49];
	for(i=1;i<=48;i++)
		E0[i] = L0[E_E0[i]-1];		//扩展成48位E0
	for(i=0;i<32;i++)				//L[i] = R[i-1]
		L0[i] = R0[i];

	int P0[33];
	Map48_32(E0,P0,K);				//48位E0 --> 32位P0

	for(i=0;i<32;i++)
	{
		if(P0[i+1] == R0[i])	R0[i]=1;
		else					R0[i]=0;	//P0与R0异或
	}
}

void CDES_CryptionDlg::OnButtonEncryption() 
{
	// TODO: Add your control notification handler code here
	UpdateData();

	if(m_path.GetLength() >= 19)
	{
		AfxMessageBox("密匙长度大于19,无法加密!\n\t请重新输入 ...");
		return ;
	}

	char *ch2;
	int len,i;
	unsigned __int64 sum = 0;
	ch2 = m_path.GetBuffer(m_path.GetLength());
	
	len = strlen(ch2);
	for(i=0;i<len;i++)
		sum = 10 * sum + ch2[i] - 48 ;
	memset(PC,0,sizeof(PC));
	dec2bin(PC,sum);	


	m_crypt.SetSel(0,-1);
	m_crypt.Clear();
	if(m_message.IsEmpty())
	{
		AfxMessageBox("原文是空的,无法加密!\n\t请输入 ...");
		return ;
	}
	if(m_message.GetLength() >= 19)
	{
		AfxMessageBox("原文长度大于19,无法加密!\n\t请重新输入 ...");
		return ;
	}

	int X[lMes+1] ,X0[lMes+1] ;				//明文X和置换后的明文X0-----------64位
	int L0[lMes/2],R0[lMes/2];
	int	PC0[lKey+1];		//置换后的秘钥PC0----------56位
	int th[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
	int PC2[16][49]={0};	


	int len1 = strlen(m_message);
	char ch0[lMax];
	memcpy(ch0,m_message,len1);
	sum =0;
	for(i=0;i<len1;i++)
		sum = 10 * sum + ch0[i] - 48;
	
	memset(X,0,sizeof(X));
	dec2bin(X,sum);

		for(i=1;i<=lMes;i++)	
		X0[i] = X [ X_X0[i] ] ;
	
	
		for(i=0;i<lMes;i++)
		{
			if(i<lMes/2)	L0[i] = X0[i+1];		//明文前32位L0
			else			R0[i-lMes/2] = X0[i+1];	//明文后32位R0
		}
		

		///////////////////////////////////////////////////////////////---------读取一段64位二进制秘钥
		
		
		for(i=1;i<=lKey;i++)
			PC0[i] = PC[ PC_PC0[i] ];
		
		int C0[lKey/2],D0[lKey/2];
		for(i=1;i<=lKey;i++)
		{
			if(i<lKey/2)	C0[i] =PC0[i];			//秘钥前28位C0
			else			D0[i-lKey/2]=PC0[i];	//秘钥后28位D0
		}
		

		int j,k;
		for(int ix=0;ix<16;ix++)	//得到16个子秘钥PC2
		{
			LeftMove(C0,th[i]);		
			LeftMove(D0,th[i]);		//分别循环左移th[i]个

			//重新合并成56位PC0				
			for(j=1;j<=lKey/2;j++)
				PC0[j] = C0[j-1];
			for(k=1;k<=lKey/2;k++,j++)
				PC0[j]=D0[k-1];		
			for(j=1;j<=48;j++)
				PC2[ix][j] = PC0[ PC2_PC0[j] ];		//再次置换压缩得到48位子秘钥PC2	
		}

		
		////////////////////////////////////////////////////////////////-----------进行16轮加密
		for(int iy=0;iy<16;iy++)	
		{
			F(L0,R0,PC2[ix]);
		}

		int C[65],last_C0[65];
		j=1;
		for(i=0;i<32;i++,j++)
			C[j] =R0[i];
		for(i=0;i<32;i++,j++)
			C[j] = L0[i];

		for(i=1;i<=64;i++)		//逆置换IP-1
			last_C0[i] = C[C_C0[i]];
		
		unsigned __int64 x;
		sum = 0;
		x   = 1;
		for(i=64;i>=2;i--)
		{
			sum += last_C0[i] * x;
			x *= 2;
		}
		CString dec;
		dec.Format("%I64d",sum);
		char last_cryption[65];
		for(i=0;i<64;i++)
		{
			last_cryption[i] = last_C0[i+1] + 48 ;
		}
		last_cryption[64] = '\0';

		CString str;
		str.Format("二进制:%s",last_cryption);
		str += "\r\n十进制:\r\n";
		str += dec;
		int nlength = m_crypt.GetWindowTextLength();
		m_crypt.GetSel(nlength,nlength);
		m_crypt.ReplaceSel(str);
	


	UpdateData(FALSE);
}

void CDES_CryptionDlg::OnButtonExit() 
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void CDES_CryptionDlg::dec2bin(int PC[65], unsigned __int64 sum)
{
	int i = 64;
	while(sum > 0)
	{
		PC[i] = int(sum % 2);
		sum /= 2;
		i--;
	}
}

⌨️ 快捷键说明

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