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

📄 2.cpp

📁 SDES 的加密
💻 CPP
字号:
#include<iostream.h>
int * jiao1 (int * a ,int i = 3)//二进制进行加1的运算
{
	a[i] = a[i]++;
	if(a[i] ==2 && i>=0) 
	{
		a[i] = 0;
		jiao1(a,--i);
	}
	 return a;
}
int * XOR(int * a, int * b)//  四位二进制的数进行xor 运算
{
	for(int i = 0; i<4 ; i++)
	{
		if( a[i] == b[i] )
			a[i] =  0;
		else 
			a[i] = 1;
	}
	return a;
}
int * s ( int * s,int *s0) // s0 是s盒子的值的变化字  s是输入的4bit的数
{                         //  注意:s 是4位的,返回的结果是2位,所以,就只有2 3位有效
	int a = s[0]*2 + s[3];
	int b = s[1]*2 + s[2];
for(int i = 0;i<4;i++)
   *(s+i) = 0;
	for( i = 0; i<s0[a*4+b]; i++)
		jiao1(s);
	return s;
}

	
int * fk(int * sw , int * k2)
{
	int E_P [] = {4,1,2,3,2,3,4,1} ;
	int s0  [] = {1,0,3,2,3,2,1,0,0,2,1,3,3,1,3,2};
	int s1  [] = {0,1,2,3,2,0,1,3,3,0,1,0,2,1,0,3};
	int p4  [] = {2,4,3,1};
	int e_phou [8];
	for(int i = 0;i<8;i++)
		e_phou[i] = sw[E_P[i]+4-1];
	XOR(e_phou,k2);
	XOR(e_phou+4,k2+4);
	s(e_phou,s0);
	s(e_phou+4,s1);
	e_phou[4] = e_phou[2];
	e_phou[5] = e_phou[3];// 4 5   6 7位为s0 和s1后的四个数
	for(i = 0;i<4;i++)
		e_phou[i] = e_phou[p4[i]+4-1];
	 // p4 后的四个数 0 1 2 3 中
	XOR(sw ,e_phou);
	return sw;
}
int *SW(int *a)
{
	for(int i = 0;i<4;i++)
	{
		int temp = a[i+4];
		a[i+4]   = a[i];
		a[i]     = temp;
	}
	return a;
}
/////////////////////////////////////////////////////////////////
//下面的函数是对求密匙的运算k1 k2 
 int *Ls_1(int * a)// 循环左移一位  , 是5为数的
{
	int temp = a[0];
	for(int i =0;i<4;i++)
		a[i] = a[i+1];
	a[4]  =  temp;
	return a;
}
int * Mishi(int *ms,int a)// 求k1 和k2   a=1 是k1   a= 3 是k2
{
	int p10 [] = {3,5,2,7,4,10,1,9,8,6};
	int p8  [] = {6,3,7,4,8,5,10,9};
	int temp[10];
	for(int i=0;i<10;i++)
		temp[i] = ms[p10[i]-1];
	for( i= 0 ; i<a;i++)
	{
		Ls_1(temp);
		Ls_1(temp+5);
	}
	for(i=0;i<8;i++)
		*(ms+i) = temp[p8[i]-1];
	return ms;
}
void cout1(int *a,int b)
{
	for(int i = 0 ;i<b;i++)
	{
		cout<<a[i];
		if(i !=b-1) 
			cout<<" ";
		else cout<<endl;
	}
}

/////////////////////////////////////////////////////////////////////////
//主函数开始了
int  main()
{   


	cout<<"       本程序用于模拟S-DES加密的过程!"<<endl;
	cout<<"       按要求输入明文和密匙!         "<<endl<<endl;
	cout<<" 输入密文(8bit 二进制数):";
	int temp[8] ;// 存放明文	int temp[8];
   int 	temp1[8]; //临时存放明文  int temp1[8];
	int mishi[10];
	int mishi1[10];
	for(int i=0;i<8;i++)
	{
		cin>>temp[i];
		if(temp[i] !=0&&temp[i] !=1)
		{
			cout<<" 错误!"<<endl;
			cout<<"按要求输入二进制数,中间用空格隔开!"<<endl;
			return 1;
		}
	}
	cout<<"  明文输入正确!"<<endl;
	cout<<"  继续输入密匙(10bit 二进制数):";
		for( i=0;i<10;i++)
	{
		cin>>mishi[i];
		mishi1[i] = mishi[i];
		if(mishi[i] !=0 && mishi[i] !=1)
		{
			cout<<" 错误!"<<endl;
			cout<<"按要求输入二进制数,中间用空格隔开!"<<endl;
			return 1;
		}
	}
	


	int IP  [] = {2,6,3,1,4,8,5,7} ;
	int IP_1[] = {4,1,3,5,7,2,8,6} ;
///////////////////////////////////////////////	
	for( i=0;i<8;i++)
		temp1[i] = temp[IP[i]-1];
	cout<<"IP  之后:";
	cout1(temp1,8);
//////////////////////////////////////////////IP完成


cout<<"k2      =";
    Mishi(mishi1,3) ;
	cout1(mishi1,8);


	fk(temp1,mishi1);
		cout<<"fk  之后:";
	cout1(temp1,8);
/////////////////////////////////////////////
	SW(temp1);
		cout<<"SW  之后:";
	cout1(temp1,8);

//////////////////////////////////////////////
	cout<<"k1      =";
	Mishi(mishi,1);
		cout1(mishi,8);

	fk(temp1,mishi);
		cout<<"fk  之后:";
	cout1(temp1,8);
//////////////////////////////////////////
	for(i=0;i<8;i++)
		temp[i] = temp1[IP_1[i]-1];
		cout<<"IP_1之后:";
	cout1(temp,8);
		/////////////////////////////
		
/*	int bin[4] = { 0 } ;
	int shu;
	cin>>shu;
	while(shu>0)
	{
		jiao1(bin);
		shu--;
	}
	*/
	return 1;

}

⌨️ 快捷键说明

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