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

📄 des.h

📁 des演示算法c++实现
💻 H
字号:
#include<iostream.h>
#include<limits.h>
#include<string.h>
#include <stdlib.h>

class DES
{
	public:
		DES();
		void getkey();
		void encrypt(char R[]);
		void decrypt(char R[]);
		
	protected:
		void EN_ReplaceIP(int sixR[]);//IP置换
		void UN_ReplaceIP(int sixR[]);//IP逆置换
		void ReplaceE(int thiR[]);//E盒
		void Select(int zsR[],int h);//S盒
		void Leftmove(int tweR[],int j);//左移函数
		void Rightmove(int tweR[],int j);
		void ReplaceP(int thiR[]);//P置换
		void KeyReplaceA(int sixR[]);//密钥置换1
		void KeyReplaceB(int tweL[],int tweR[]);//密钥置换2
		int K[64];//64位密钥
		int Mingwen[64];//64位明文
		int Cryptograph[64];//64位密文
		char M[9]; //8字节明文
		char C[9]; //8字节密文
		char k[9];//8字节密钥
		void getM(char R[]);//char_in  int_out
		void getC(char R[]);//char_in  int_out
		void getK(char R[]);//char_in  int_out
		void outM(int N[]);
		void outC(int N[]);
		int forT[48],forK[48],thiT[32],zsT[6],zfT[4],CL[28],DR[28];
//约定64位中间量用sixT表示;56位用fivT表示
//    48位用forT表示;32位用thiT表示  28位用tweT, 6位zsT,4位zfT(对应函数中的R)

		
};

void DES::EN_ReplaceIP(int sixR[])//64_in  64_out
{
	int temp[64];
	int IP[64]={ 
			58,50,42,34,26,18,10,2,
			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
		   };                               //IP变换
	for(int q=0;q<64;q++)
        	temp[q]=sixR[IP[q]-1];
  	for(int w=0;w<64;w++)
  		sixR[w]=temp[w];
};
  		
void DES::UN_ReplaceIP(int sixR[])//64_in  64_out
{
	int temp[64];
	int ip[64]={
			40,8,48,16,56,24,64,32,
			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
		    };                             //IP逆变换	 
	for(int q=0;q<64;q++)
        	temp[q]=sixR[ip[q]-1];
  	for(int w=0;w<64;w++)
  		sixR[w]=temp[w];
};
  		      
void DES::ReplaceE(int thiR[])//32_in  48_out	 	
{
	
	int E[48]={	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};//选择运算E
	for (int i=0;i<48;i++)
		forT[i]=thiR[E[i]-1];
};
	
	
void DES::ReplaceP(int thiR[]) //32_in  32_out
{
	int temp[32];
	int P[32]={     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};//P置换
  	for(int j=0;j<32;j++)
  		temp[j]=thiR[P[j]-1];
  	for(int w=0;w<32;w++)
  		thiR[w]=temp[w];
};
  	

void DES::Select(int zsR[],int h)  //6_in 4_out
{
	int k,m,n;
	int *lp;
	k=2*zsR[0]+zsR[5];
	m=8*zsR[1]+4*zsR[2]+2*zsR[3]+zsR[4];
int S1[16][4]={
{14,      0,     4,    15}, 
{ 4,     15,     1,    12}, 
{13,      7,    14,     8}, 
{ 1,      4,     8,     2}, 
{ 2,     14,    13,     4},
{15,      2,     6,     9},
{11,     13,     2,     1}, 
{ 8,      1,    11,     7}, 
{ 3,     10,    15,     5}, 
{10,      6,    12,    11}, 
{ 6,     12,     9,     3}, 
{12,     11,     7,    14},
{ 5,      9,     3,    10},
{ 9,      5,    10,     0},
{ 0,      3,     5,     6},  
{ 7,      8,     0,    13} };  
int	S2[16][4]={
{15,     3,     0,    13}, 
{1 ,    13,    14,     8},
{8 ,     4,     7,    10}, 
{14,     7,    11,     1}, 
{6 ,    15,    10,     3}, 
{11,     2,     4,    15}, 
{3 ,     8,    13,     4}, 
{4 ,    14,     1,     2}, 
{9 ,    12,     5,    11}, 
{7 ,     0,     8,     6}, 
{2 ,     1,    12,     7}, 
{13,    10,     6,    12}, 
{12,     6,     9,     0}, 
{0 ,     9,     3,     5}, 
{5 ,    11,     2,    14}, 
{10,     5,    15,     9}  }; 
int S3[16][4]={
{10 ,   13,   13,     1}, 
{0  ,    7,    6,    10}, 
{9  ,    0,    4,    13}, 
{14 ,    9,    9,     0}, 
{6  ,    3,    8,     6}, 
{3  ,    4,   15,     9}, 
{15 ,    6,    3,     8}, 
{5  ,   10,    0,     7}, 
{1  ,    2,   11,     4}, 
{13 ,    8,    1,    15},
{12 ,    5,    2,    14}, 
{7  ,   14,   12,     3}, 
{11 ,   12,    5,    11}, 
{4  ,   11,   10,     5}, 
{2  ,   15,   14,     2}, 
{8  ,    1,    7,    12}  }; 
int S4[16][4]={
{7 ,    13,    10,     3}, 
{13,     8,     6,    15}, 
{14,    11,     9,     0}, 
{3 ,     5,     0,     6}, 
{0 ,     6,    12,    10},
{6 ,    15,    11,     1}, 
{9 ,     0,     7,    13}, 
{10,     3,    13,     8},
{1 ,     4,    15,     9}, 
{2 ,     7,     1,     4}, 
{8 ,     2,     3,     5}, 
{5 ,    12,    14,    11}, 
{11,     1,     5,    12}, 
{12,    10,     2,     7}, 
{4 ,    14,     8,     2},
{15,     9,     4,    14} };
int S5[16][4]={
{ 2,    14,     4,    11}, 
{12,    11,     2,     8}, 
{ 4,     2,     1,    12}, 
{ 1,    12,    11,     7},
{ 7,     4,    10,     1}, 
{10,     7,    13,    14}, 
{11,    13,     7,     2},
{ 6,     1,     8,    13}, 
{ 8,     5,    15,     6}, 
{ 5,     0,     9,    15}, 
{ 3,    15,    12,     0}, 
{15,    10,     5,     9},
{13,     3,     6,    10}, 
{ 0,     9,     3,     4}, 
{14,     8,     0,     5}, 
{ 9,     6,    14,     3} }; 
int S6[16][4]={
{12,    10,     9,     4}, 
{ 1,    15,    14,     3}, 
{10,     4,    15,     2}, 
{15,     2,     5,    12}, 
{ 9,     7,     2,     9},
{ 2,    12,     8,     5}, 
{ 6,     9,    12,    15}, 
{ 8,     5,     3,    10}, 
{ 0,     6,     7,    11}, 
{13,     1,     0,    14}, 
{ 3,    13,     4,     1}, 
{ 4,    14,    10,     7}, 
{14,     0,     1,     6}, 
{ 7,    11,    13,     0}, 
{ 5,     3,    11,     8}, 
{11,     8,     6,    13} };
int S7[16][4]={
{ 4,    13,     1,     6}, 
{11,     0,     4,    11}, 
{ 2,    11,    11,    13}, 
{14,     7,    13,     8}, 
{15,     4,    12,     1}, 
{ 0,     9,     3,     4},
{ 8,     1,     7,    10}, 
{13,    10,    14,     7}, 
{ 3,    14,    10,     9}, 
{12,     3,    15,     5}, 
{ 9,     5,     6,     0}, 
{ 7,    12,     8,    15}, 
{ 5,     2,     0,    14}, 
{10,    15,     5,     2}, 
{ 6,     8,     9,     3}, 
{ 1,     6,     2,    12} }; 
int S8[16][4]={
{13,     1,     7,     2}, 
{ 2,    15,    11,     1}, 
{ 8,    13,     4,    14}, 
{ 4,     8,     1,     7}, 
{ 6,    10,     9,     4}, 
{15,     3,    12,    10},
{11,     7,    14,     8}, 
{ 1,     4,     2,    13}, 
{10,    12,     0,    15}, 
{ 9,     5,     6,    12}, 
{ 3,     6,    10,     9}, 
{14,    11,    13,     0}, 
{ 5,     0,    15,     3}, 
{ 0,    14,     3,     5}, 
{12,     9,     5,     6}, 
{ 7,     2,     8,    11}      }; 
	switch(h)
	{
		case 1: lp=&S1[m][k]; break;
		case 2: lp=&S2[m][k]; break; 
		case 3: lp=&S3[m][k]; break;		
		case 4: lp=&S4[m][k]; break;	
		case 5: lp=&S5[m][k]; break;	
		case 6: lp=&S6[m][k]; break;	
		case 7: lp=&S7[m][k]; break;	
		case 8: lp=&S8[m][k]; break;
	};
			    n=8;	      		
  				for(int i=0;i<4;i++)
  				{	zfT[i]=(*lp)/n;
  					if (zfT[i]!=0)
  					(*lp)=(*lp)%n;   
  					n=n/2;
  				};
  	
};

void DES::KeyReplaceA(int sixR[])//64_in  28_out
{
	int AC[28]={
			57,49,41,33,25,17,9,
			1,58,50,42,34,26,18,
			10,2,59,51,43,35,27,
			19,11,3,60,52,44,36
		   };
	int AD[28]={
			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
		   };
	for (int b=0;b<28;b++)
		{CL[b]=sixR[AC[b]-1];
		DR[b]=sixR[AD[b]-1];} 	   
};


void DES::KeyReplaceB(int tweL[],int tweR[])//28_in  48_out
{
	int fivK[56];
	int B[48]={
		14,17,11,24,1,5,
		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
	      };
		
	for(int q=0;q<28;q++)
	{
		fivK[q]=tweL[q];
		fivK[q+28]=tweR[q];
	};
	for (int i=0;i<48;i++)
	forK[i]=fivK[B[i]-1];
};

void DES::Leftmove(int tweR[],int h)//左移函数
{
	int ML[16]={
			1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1
	       };
	if(ML[h]==1)
	{
		int temp=tweR[0];
		for(int i=0;i<27;i++)
			tweR[i]=tweR[i+1];
		tweR[27]=temp;
	}
	else
	{
		int fir=tweR[0],sec=tweR[1];
		for (int w=0;w<26;w++)
			tweR[w]=tweR[w+2];
		tweR[26]=fir;
		tweR[27]=sec;
	}
};

void DES::Rightmove(int tweR[],int j)//右移函数
{
	int ML[16]={
			1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1
	       };
	if(ML[15-j]==1)
	{
		int temp=tweR[27];
		for(int i=0;i<27;i++)
			tweR[27-i]=tweR[26-i];
		tweR[0]=temp;
	}
	else
	{
		int fir=tweR[27],sec=tweR[26];
		for (int w=0;w<26;w++)
			tweR[27-w]=tweR[25-w];
		tweR[1]=fir;
		tweR[0]=sec;
	}
};

DES::DES()
{
	C[0]='\0';
	M[0]='\0';
	k[0]='\0';
	for (int p=0;p<64;p++)
	{
		 K[p]=2;
		 Mingwen[p]=2;
		 Cryptograph[p]=2;
	}
};

	
void DES::getM(char R[])
{       int MR[8];
		int i=0;
		while( R[i]!='\0')
		{ MR[i]=int(R[i]);
			i++;
		};
	
		for(int j=0;j<8;j++)
		{
			int n=128;
			int sum=MR[j];
			for(int p=0;p<8;p++)		
			{
				Mingwen[8*j+p]=sum/n;
				if(Mingwen[8*j+p]!=0)
				sum=sum%n;
				n=n/2;
			};
		};//得出64位的明文;
};


void DES::getC(char R[])//char_in  int_out
{
	int Cry[8];
	int i=0;
	while( R[i]!='\0')
	{ Cry[i]=int(R[i]);
	  i++;
	};
	
	for(int j=0;j<8;j++)
	{
		int n=128;
		int sum=Cry[j];
		for(int p=0;p<8;p++)		
		{
			Cryptograph[8*j+p]=sum/n;
			if(Cryptograph[8*j+p]!=0)
			sum=sum%n;
			n=n/2;
		};
	}//得出64位的密文;
};


void DES::getK(char R[])
{
	int kR[8];
	int i=0;
	for(; R[i]!='\0';i++)
		 kR[i]=int(R[i]);
	for(int j=0;j<8;j++)
	{
		int n=128;
		int sum=kR[j];
		for(int p=0;p<8;p++)		
		{
			K[8*j+p]=sum/n;
			if(K[8*j+p]!=0)
			sum=sum%n;
			n=n/2;
		};
	};//得出64位的密钥;
};

void DES::outM(int N[])
{
	int Mi[8];
	for (int i=0;i<8;i++)
	{
		for(int j=0;j<8;j++) 
		    Mi[j]=N[8*i+j];
	        M[i]=char(128*Mi[0]+64*Mi[1]+32*Mi[2]+16*Mi[3]+8*Mi[4]+4*Mi[5]+2*Mi[6]+Mi[7] );
	        cout << M[i];
	}
	M[8]='\0';
	cout << endl;
};
		
void DES::outC(int N[])
{
	int Cr[8];
	for (int i=0;i<8;i++)
	{
		for(int j=0;j<8;j++) 
		    Cr[j]=N[8*i+j];
	        C[i]=char(128*Cr[0]+64*Cr[1]+32*Cr[2]+16*Cr[3]+8*Cr[4]+4*Cr[5]+2*Cr[6]+Cr[7] );
	        cout<< C[i];
	}
	C[8]='\0';
	cout << endl;
};


void DES::getkey()
{
	cout<< "请输入八位密钥:"<<endl;
	cin >> k;
	k[8]='\0';
}

void DES::encrypt(char R[])
{    
	///////////////////////////////////////////////密钥操作部分
	getkey();//得到密钥  get_k[]
	getK(k);//得到64位密钥
	KeyReplaceA(K);//得出C0,D0
	Leftmove(CL,0);
	Leftmove(DR,0);// 得到C1 D1
	///////////////////////////////////////////////明文操作部分
	getM(R);//得出64位明文  out_Mingwen[64]
	EN_ReplaceIP(Mingwen);//进行IP置换
	//		cout<<endl;///////////////////////////////////////////////公共循环部分
	int left[32],right[32];//设l0  r0
	
	for (int j=0;j<16;j++)//从l0,r0 做到l15,r15
	{
		for(int i=0;i<32;i++)
		{
			left[i]=Mingwen[i];//得到lj
			right[i]=Mingwen[i+32];//得到rj
		};
		for(int q=0;q<32;q++)
			Mingwen[q]=right[q];//准备l(j+1)
///////////////////////////////////////////////////////////对右部进行f(Ai,Ki)		
		ReplaceE(right);//进行E扩充out_forT[48];
		KeyReplaceB(CL,DR);//得到K(j+1),  out_forK[48];
		if(j!=15)
		{Leftmove(CL,j+1);//得到C(j+2);
		 Leftmove(DR,j+1);//得到D(j+2);
		};;
	    for(int p=0;p<48;p++)
			forT[p]=(forT[p]+forK[p])%2;//做扩充后的Rj和K(j+1)的异或  out_forT[48]
		for (int a=0;a<8;a++)
		{
			for(int b=0;b<6;b++)
		 		zsT[b]=forT[a*6+b];//把forT按6位分组
		 	Select(zsT,a+1);//out_zfT
		 	for(int c=0;c<4;c++)
		 	    thiT[a*4+c]=zfT[c];//把8个4位组串成32位
		};
		ReplaceP(thiT);// 进行P置换
		for(int n=0;n<32;n++)
		right[n]=(left[n]+thiT[n])%2; //结果与lj异或得到rj
		if(j==15)
		{
			for (int m=0;m<32;m++)
			{
			    Cryptograph[32+m]=Mingwen[m];//把l16给Cryptograph
				Cryptograph[m]=right[m];//把r16给Cryptograph
			};
		}	
		else	
		{	
			for (int l=0;l<32;l++)
				Mingwen[32+l]=right[l];//准备r(j+1)
			
		}
	};//out_Cryptograph[64]
		UN_ReplaceIP(Cryptograph);
	    for(int c=0;c<64;c++)
		{cout<<Cryptograph[c];cout<< ' ';};
			cout<<endl;
		outC(Cryptograph);
};


void DES::decrypt(char R[])
{      
	///////////////////////////////////////////////密钥操作部分
	getkey();//得到密钥
	getK(k);//得到64位密钥
	KeyReplaceA(K);
	///////////////////////////////////////////////明文操作部分
	getC(R);//得出64位密文
	EN_ReplaceIP(Cryptograph);//进行IP置换
	///////////////////////////////////////////////公共循环部分
	int left[32],right[32];
	for (int j=0;j<16;j++)
	{
		for(int i=0;i<32;i++)
		{
			left[i]=Cryptograph[i];
			right[i]=Cryptograph[i+32];
		};
		for(int q=0;q<32;q++)
			Cryptograph[q]=right[q];
		ReplaceE(right);//out_forT[48];
		KeyReplaceB(CL,DR);//out_forK[48];
		Rightmove(CL,j);
		Rightmove(DR,j);
		for(int p=0;p<48;p++)
			forT[p]=(forT[p]+forK[p])%2;	
		for (int a=0;a<8;a++)
		{
			for(int b=0;b<6;b++)
		 		zsT[b]=forT[a*6+b];
		 	Select(zsT,a+1);
		 	for(int c=0;c<4;c++)
		 		thiT[a*4+c]=zfT[c];
		};
		ReplaceP(thiT);
		for(int n=0;n<32;n++)
			right[n]=(thiT[n]+left[n])%2;
		if(j!=15)
		{
			for (int m=0;m<32;m++)
			{
				Cryptograph[32+m]=right[m];
			};
		}
		else	
		{	
			for (int l=0;l<32;l++)
			{
				Mingwen[32+l]=Cryptograph[l];
				Mingwen[l]=right[l];
			};
		};
	};
		UN_ReplaceIP(Mingwen);
		outM(Mingwen);
		for(int c=0;c<64;c++)
		{cout<<Mingwen[c];cout<< ' ';};
			cout<<endl;
		
};



	

	
		
		
		
		 	
		 	
		        
		
		
		 	
		 	
		 		
		 
		 
		
	
	

	
			      

⌨️ 快捷键说明

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