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

📄 des.cpp

📁 密码学实验中的DES算法
💻 CPP
字号:
// DES.cpp : Defines the class behaviors for the application.
//

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

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

/////////////////////////////////////////////////////////////////////////////
// CDESApp

BEGIN_MESSAGE_MAP(CDESApp, CWinApp)
	//{{AFX_MSG_MAP(CDESApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDESApp construction

CDESApp::CDESApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CDESApp object

CDESApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CDESApp initialization

BOOL CDESApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CDESDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

void CDESDlg::charToBit(CString in,int flag)//获得密钥/明文/密文的64位
{
	int i,j;
	char a;
	int key_64[64],DATA[64];
	if(!flag)
	{
		for(i=0;i<8;i++)
		{
			a=in[i];
			for(j=0;j<8;j++)
			{
				key_64[8*i+7-j]=a&0x01;
				a=(char)((int)a>>1);
			}
		}
		key_64Tokey_56AndkeyT1(key_64);
	}
	if(flag)
	{
		for(i=0;i<8;i++)
		{
			a=in[i];
			for(j=0;j<8;j++)
			{
				DATA[8*i+7-j]=a&0x01;
				a=(char)((int)a>>1);
			}
		}
		IpT(DATA);
	}
}
void CDESDlg::key_64Tokey_56AndkeyT1(int key_64[64])//将64位的密钥转换为56位
{
	int i;
	int key_56[56];
	for(i=0;i<56;i++)
		key_56[i]=key_64[keyT1[i]];
	CircleLeftAndkeyT2(key_56);
}
void CDESDlg::CircleLeftAndkeyT2(int key_56[56])//获得十六个密钥
{
	int i,j,k;
	int temp_C,temp_D,C[28],D[28],CD[56],key[16][48];
	for(i=0;i<28;i++)
		C[i]=key_56[i];
	for(i=28;i<56;i++)
		D[i-28]=key_56[i];
	for(i=0;i<16;i++)
	{
		for(j=0;j<CircleLeft[i];j++)//循环左移
		{
			temp_C=C[0];
			temp_D=D[0];
			for(k=1;k<28;k++)
			{
				C[k-1]=C[k];
				D[k-1]=D[k];
			}
			C[27]=temp_C;
			D[27]=temp_D;
		}
		for(j=0;j<28;j++)
		{
			CD[j]=C[j];
			CD[j+28]=D[j];
		}
		for(j=0;j<48;j++)
			key[i][j]=CD[keyT2[j]];
	}
	if(FLAG==0)
	{
		for(i=0;i<16;i++)
		{
			for(j=0;j<48;j++)
				RoundKey[i][j]=key[15-i][j];
		}
	}
	else
	{
		for(i=0;i<16;i++)
		{
			for(j=0;j<48;j++)
				RoundKey[i][j]=key[i][j];
		}
	}
}
void CDESDlg::Cut(CString str)//将明文分组
{
	CString char_8;
	int i,len,x;
	char ch;
	len=str.GetLength();
	char_8="........";
	for(i=0;i<len;i++)
	{
		if((i+1)%8==0)
		{
			x=i%8;
			ch=str.GetAt(i);
			char_8.SetAt(x,ch);
			charToBit(char_8,1);
			STR+=Result;
		}
		else
		{
			x=i%8;
			ch=str.GetAt(i);
			char_8.SetAt(x,ch);
		}
	}
	if(len%8!=0)
	{
		for(i=len;i<len-len%8+8;i++)
			char_8.SetAt(i%8,'!');
		cnt_a=8-len%8;
		charToBit(char_8,1);
		STR+=Result;
	}	
}
void CDESDlg::IpT(int DATA[64])//进行换位变换
{
	int i;
	int data[64];
	for(i=0;i<64;i++)
		data[i]=DATA[IP[i]];
	F(data);
}
void CDESDlg::F(int data[64])//F函数
{
	int i,j,k,l;
	int R_48[48],/*temp[32],*/R1[48],L[17][32],R[17][32],B[8][6];
	for(i=0;i<32;i++)
		L[0][i]=data[i];
	for(i=32;i<64;i++)
		R[0][i-32]=data[i];
	for(k=0;k<16;k++)
	{
		for(i=0;i<48;i++)
			R1[i]=R[k][E[i]];
		for(j=0;j<48;j++)
			R_48[j]=R1[j]^RoundKey[k][j];
		j=0;
		for(i=0;i<48;i++)
		{
			if((i%6==0)&&(i!=0))
				j++;
			B[j][i%6]=R_48[i];
		}
		S_funcAndP(B);
		for(l=0;l<32;l++)
			R[k+1][l]=L[k][l]^tmp1[l];
		for(l=0;l<32;l++)
			L[k+1][l]=R[k][l];
	}
	RLAndIP_1(R[16],L[16]);
}
void CDESDlg::S_funcAndP(int B[8][6])//S盒变换
{
	int i,j,k,/*a,*/t,x,y;
	int tmp[32];printf("SBox\n");
	for(i=0;i<8;i++)
	{
		j=0;
		x=B[i][0]*2+B[i][5];
		y=B[i][1]*8+B[i][2]*4+B[i][3]*2+B[i][4];
		t=SBox[i][x][y];
		for(j=0;j<4;j++)
		{
			k=i*4+j;
			tmp[k]=B_H[t][j];
		}
	}
	for(i=0;i<32;i++)
		tmp1[i]=tmp[P[i]];
}
void CDESDlg::RLAndIP_1(int R[32],int L[32])//串联左右两部分并进行IP逆置换
{
	int i;
	int tmp2[64],out[64];
	for(i=0;i<32;i++)
	{
		tmp2[i]=R[i];
		tmp2[i+32]=L[i];
	}
	for(i=0;i<64;i++)
		out[i]=tmp2[IP_1[i]];
	BitToChar(out);
}
void CDESDlg::BitToChar(int out[64])//位转换为字符
{
	int i,j,k,x;
	char ch;
	if(FLAG==1)
	{
		Result="................";
		j=0;
		for(i=0;i<64;i+=4)
		{
			x=out[i]*8+out[i+1]*4+out[i+2]*2+out[i+3];
			ch=Hex.GetAt(x);
			Result.SetAt(j,ch);
			j++;
		}
	}
	else
	{
		j=0;
		Result="........";
		for(i=0;i<8;i++)
		{
			x=0;
			k=7;
			for(k=7;k>=0;k--)
			{
				x+=(int)(out[j]*pow(2,k));
				j++;
			}
			Result.SetAt(i,x);
		}
	}
}
void CDESDlg::CutMiwen(CString str)
{
	int i,j,k,x,DATA[64],len;
	len=str.GetLength();
	k=0;
	for(i=0;i<len;i++)
	{
		if(str[i]>='0'&&str[i]<='9')
			x=str[i]-48;
		else
			x=str[i]-87;
		if((i+1)%16==0)
		{
			for(j=0;j<4;j++)
			{
				DATA[k]=B_H[x][j];
				k++;
			}
			IpT(DATA);
			k=0;
			STR+=Result;
		}
		else
		{
			for(j=0;j<4;j++)
			{
				DATA[k]=B_H[x][j];
				k++;
			}
		}
	}
}

⌨️ 快捷键说明

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