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

📄 des.h

📁 实现了三种古典密码学体制和DES体制及其分析。affine(仿射)读入文件11.txt中内容(小写字母
💻 H
字号:
//程序:DES加密解密雪崩效应分析,差分分析
#include<iostream>
#include<vector>
using namespace std;
typedef bool eletype;
#define ZERO false
#define ONE true;

class Des{
public:
    Des(void);
    Des(eletype* p , unsigned long p_len);
    Des(Des& a);
	~Des();

	void  operator =  (Des a);
	void  operator >> (int n);//右移
	void  operator << (int n);//左移
	void  operator += (Des a);
	void  operator ^= (Des a);
    Des   operator ^  (Des a);
	Des   operator +  (Des a);
//	void  operator=(Des a ,int i);
	
	Des Encrpty(Des key);	//加密函数
	Des Decrpty(Des key);   //解密函数

	void  ByteToDes(char* In,int bits);    
	char* DesToByte();						

	void IntToDes(int n,int len);

	void  xuebeng(Des key, Des second,unsigned long* count); //统计雪崩效应
	void  print();
	eletype* getptr();
	void Difference(Des plain1,Des plain2,int n,int* ND);    //差分分析
	void RandonS();											 //随机产生S盒


private:
	eletype* IntToBit(unsigned int i);
	unsigned int BitToInt(int n);


	Des DesF(Des Keyi);
	Des Enc(Des Keyi);
	Des Key(int i);

	Des Substitution(int n)	;
	Des Permutation(int* ip,int n);

	void LS(int round);
	void RS(int n);//循环右移
	

private:
	eletype* desptr;
	unsigned long deslen;

	static int IP[];
	static int IP_1[];
	static int pc_1[];
	static int pc_2[];
	static int E[];
	static int P[];
	static int S[10][4][16];

};


Des::Des(void)
{
	desptr = NULL;
	deslen = 0;
}

Des::Des(Des& a)
{
	deslen = a.deslen;
	desptr = new eletype[deslen];

	for(int i = 0 ; i < deslen ; i++)
		desptr[i] = a.desptr[i];
	
}

Des::Des(eletype* p,unsigned long p_len)
{
	deslen = p_len;
	desptr = new eletype[deslen];
	for(int i = 0 ; i < deslen ; i++)
		desptr[i] = p[i];
}

Des::~Des()
{
	deslen = 0;
	delete [] desptr;
}

void Des::print()
{
	cout<<"It's lenth is "<<deslen<<endl;
	if(desptr != NULL)
    {
        cout<<"It's elements is ";
	    for(int i = 0 ; i < deslen ; i++)
        {
            if(i%8 == 0) cout<<endl;
	 	    cout<<desptr[i]<<" ";
       }
        cout<<endl;
     }
     else cout<<"It has no elments."<<endl;
}

void Des::operator= (Des a)
{//???
    if( a.desptr != desptr )
	{
		delete [] desptr;
		deslen = a.deslen;
		desptr = new eletype[deslen];
		for(int i = 0 ; i < deslen ; i++)
		  desptr[i] = a.desptr[i];
   }
}

/*
void Des::operator=(Des a ,int i)
{
	deslen = i;
	if(a.desptr != desptr)
	{
		delete [] desptr;
		deslen = a.deslen; 
		desptr = new eletype[i];
		for(int j = 0 ; j < deslen ; j++ )
			desptr[j] = a.desptr[j];
	}
}
*/
Des Des::operator+ (Des a)
{//not used
    Des r;
	int i;

    r.deslen = a.deslen+deslen;
	r.desptr = new eletype[r.deslen];
	for( i = 0 ; i < deslen ; i++)
	{
		r.desptr[i] = desptr[i];
	}
	for(i = deslen ; i<r.deslen ; i++)
	{
		r.desptr[i] = a.desptr[i-deslen];
	}

	return r;
	
}
void Des::operator+= (Des a)
{
	Des r;
	int i;

    r.deslen = a.deslen+deslen;
	r.desptr = new eletype[r.deslen];

	for( i = 0 ; i < deslen ; i++)
	{
		r.desptr[i] = desptr[i];
	}
	for(i = deslen ; i < r.deslen ; i++)
	{
		r.desptr[i] = a.desptr[i-deslen];
	}

	*this = r;
}

Des Des::operator^ (Des a)
{
	Des r;
	int i;

    if(deslen == a.deslen)
	{
    	r.deslen = a.deslen;
    	r.desptr = new eletype[r.deslen];

		for(i = 0 ; i < deslen ; i++)
		{
			if( (desptr[i] && a.desptr[i]) || (!desptr[i] && !a.desptr[i]) )
				r.desptr[i] = ZERO;
			else
				r.desptr[i] = ONE;
		}
	}

	return r;
}

void Des::operator^=(Des a)
{//按位异或
	int i;
    if(deslen == a.deslen)
	{
		for(i = 0 ; i < deslen ; i++)
		{
			if( (desptr[i] && a.desptr[i]) || (!desptr[i] && !a.desptr[i]) )
				desptr[i] = ZERO;
			else
				desptr[i] = ONE;
		}
	}	
}

void Des::operator<<(int n)
{
	Des r;
	int i,j;

	r = *this;
	j = n;
	if(deslen < n) n = deslen;

	for(i = 0 ; i < deslen-n ; i++,j++)
	{
		r.desptr[i] = desptr[j];
	}
	for(i = deslen-n ; i<deslen ; i++)
	{
		r.desptr[i] = ZERO;
	}
	*this = r;
}

void Des::operator >> (int n)
{
	Des r;
	int i,j;

	r = *this;
	j = 0;
	if( deslen < n)  n = deslen;

	for(i = 0 ; i < n ; i++)
	{
		r.desptr[i] = ZERO;
	}
	for(i = n ; i < deslen ; i++,j++)
	{
		r.desptr[i] = desptr[j];
	}
	*this = r;
}

eletype* Des::getptr()
{
	return desptr;
}
/**********加密函数。64位明文输入,64位密文输出。key为64位加密密钥**********/
Des Des::Encrpty(Des key )
{

	int i;
	Des ki;
	Des cipher(*this);

	cipher = cipher.Permutation(IP,64);
	key = key.Permutation(pc_1,56);
	for(i = 0 ; i < 16; i++)
	{
		ki = key.Key(i);
		cipher = cipher.Enc(ki);
	}

	cipher.RS(32);

	cipher = cipher.Permutation(IP_1,64);
	return cipher;
}

/**********解密函数。64位密文输入,64位明文输出。key为64位加密密钥**********/
Des Des::Decrpty(Des key)
{
	int i;
	Des k[16];
	Des plain(*this);

	plain = plain.Permutation(IP,64);

	key = key.Permutation(pc_1,56);

	for(i = 15 ; i >=0 ; i--)
	{
		k[i] = key.Key(15-i);
	}
	for(i = 0 ; i < 16 ; i++)
	{
		plain = plain.Enc(k[i]);//change
	}
	
	plain.RS(32);

	plain = plain.Permutation(IP_1,64);
	
	return plain;
}
void Des::ByteToDes(char* In,int bits)
{//in要转化的字符数组。bit转化后的位数。
	int i,j;
	deslen = bits;
	desptr = new eletype[deslen];
	for(j = 0 ; j < bits/8 ; j++)
	{
		for(i = 8*j+0 ; i < 8*j+8 ; i++)
			desptr[8*j-(i%8)+7] = (In[i>>3]>>(i&7))&1;
	}
}

char* Des::DesToByte()
{
	int i,j,res;
	char* out;
	out = new char[deslen/8];

	for(j = 0 ; j < deslen/8 ; j++)
	{
		res=0;
		for(i = 8*j+0 ; i < 8*j+8 ; i++)
			res=res*2+desptr[i];
		out[j]=res;
	}
	return out;
}
	
//------------------------------------------------------------------------
//pravite:
/**********置换函数,ip为置换盒,可以取IP,IP_1,E,P。n为输出位数,分别为64,64,48,32*********/
Des Des::Permutation(int* ip,int n)
{
	eletype* p0 = new eletype[n];
	int i,j;

	for( i = 0 ; i < n ; i++)
	{
		j = ip[i];
		p0[i] = this->desptr[j];
	}

	Des a(p0,n);
	
	return a;
}

/**********S代换。输入6比特,输出4比特。n为盒号(0-7)。**********/
Des Des::Substitution(int n)
{//question
	unsigned int i,j,m;
	eletype* t;
 
    i = desptr[0] * 2 + desptr[5];

    *this<<1;
	j = this->BitToInt(4);
	*this<<5;
    m = S[n][i][j];
	t = this->IntToBit(m);
	Des tmp(t,4);

	return tmp;
}

/**********左移调度函数。28位输入输出。round为轮数(0-15)**********/
void Des::LS(int round)
{
	int i;
	const int n = 28;
	Des tmp(*this);
	if(round >= 0 && round < 16)
	{
		switch(round)
		{
			case 0:    case 1:		case 8:		case 15:
				for(i = 0 ; i < n-1 ; i++)
				{
					desptr[i] = tmp.desptr[i+1];
				}
				desptr[i] = tmp.desptr[0];
			break;

			default:
				for(i = 0 ; i < n-2 ; i++)
				{
					desptr[i] = tmp.desptr[i+2];
				}
				desptr[26] = tmp.desptr[0];
				desptr[27] = tmp.desptr[1];
			break;
		}
	}
}
void Des::RS(int n)
{//循环右移n位
	Des a(*this);
	int i;
	for(i = 0 ; i < n ; i++ )
	{
		desptr[i] = a.desptr[deslen-n+i];
	}
	for(i = n ; i < deslen ; i++)
	{
		desptr[i] = a.desptr[i-n];
	}
}
	
//------------------------------------------------------------------------------
eletype* Des::IntToBit(unsigned int i)
{
	eletype* b;
	b = new eletype[4];

	for(int k = 0 ; k < 4 ; k++)
	    b[k] = ZERO;
AGAIN:
	if(i < 4)
	{
		switch(i)
		{
			case 0:										break;
			case 1:		b[3] = 1;						break;
			case 2:		b[2] = 1;						break;
			case 3:		b[2] = 1;		b[3] = 1;		break;
		}
		return b;
	}
	else if( i<8 )
	{
		b[1] = 1;
		i = i-4;
		goto AGAIN;
	}
	else if(i<16 )
	{
		b[0] = 1;
		i = i-8;
		goto AGAIN;
	}
	else 
		return NULL;
}
void Des::IntToDes(int n,int len)
{//将一个数字n转成len比特
	int a = n;
	int k;
	int i = len-1;
	deslen = len;
	desptr = new eletype [len] ;
	for( k = 0 ; k < len ; k++)
	{
		desptr[k] = false;
	}
	while( a != 0 )
	{
		desptr[i--] = a % 2; 
		a = a / 2;
	}
}
unsigned int Des::BitToInt(int n)
{
	unsigned int a = 0;
	int i;
	for(i = 0 ; i < n ; i++)
	{
		a = a*2;
		if(desptr[i])
			a++;
	}

	return a;
}

/***********F函数,32位输入输出。仅被Enc函数调用。Keyi为48位**************/
Des Des::DesF(Des Keyi)
{
    int k;
	Des c1;
	Des r;

	c1 = (*this).Permutation(E,48);
	c1 ^= Keyi;
	for(k = 0 ; k < 8 ; k++)
	{
		r += c1.Substitution(k);
	//	c1<<6;
	}

	c1 = r.Permutation(P,32);

	return c1;
}

/**********单轮加密解密函数,64位输出输出。仅被Encrpty和Decrpty调用。Keyi为48位**********/
Des Des::Enc(Des Keyi)
{
	Des r(this->desptr+32*sizeof(eletype),32);
	Des l(this->desptr,32);
 //   r<<32;
	Des res(r.desptr,32);
 //	r=res;
 
	r=r.DesF(Keyi);
	r^=l;
	res+=r;
	return res;
}

/***********顺序产生每一轮的子密钥,56位输入输出。i为轮数,从零开始。*************/
Des Des::Key(int i)
{//产生每一轮子密钥56位输入输出
	Des r(this->desptr+28*sizeof(eletype),28);
	Des l(this->desptr,28);
//	Des k;

	r.LS(i);
	l.LS(i);

    *this = l+r;
   	
	return   this->Permutation(pc_2,48);

}	

unsigned long  countDiff (bool* first , bool* second , int n )
{//统计两个字符串中不同的个数
	int i ;
	unsigned long count = 0 ; 
	
	for( i = 0 ; i < n ; i ++)
	{
		if(first[i] != second[i] )
			count ++;
	}
	return count;
}
void Des::xuebeng(Des key, Des second,unsigned long* count)
{//count[]数组共18个元素 第一个用来记录IP置换后不同个数,最后一个用来进行IP_1置换后不同的个数。
	int i;

	Des ki;
	Des cipher1(*this);
	Des cipher2 =  second;

	cipher1 = cipher1.Permutation(IP,64);
	cipher2 = cipher2.Permutation(IP,64);
	key = key.Permutation(pc_1,56);
	count[0] = count[0]+countDiff(cipher1.desptr, cipher2.desptr, deslen);
	
	for(i = 0 ; i < 16; i++)
	{
		ki = key.Key(i);
		cipher1 = cipher1.Enc(ki);
		cipher2 = cipher2.Enc(ki); 
		int j=	countDiff(cipher1.desptr , cipher2.desptr , deslen);
				count[i+1] = count[i+1]+j;
	}

	//cipher.RS(32);

	cipher1 = cipher1.Permutation(IP_1,64);
	cipher2 = cipher2.Permutation(IP_1,64);

	count[17] = count[17] + countDiff(cipher1.desptr, cipher2.desptr, deslen);

}
void Des::Difference(Des plain1,Des plain2,int n,int* ND)
{// i为第几个S盒 plain1,plain2 均为6位
	int i,j;
	Des y1,y2;
	
	y1 = plain1;//x
	y2 = plain2;//x*

	y1 = y1.Substitution(n);//y


	y2 = y2.Substitution(n);//y*

	y1 ^= y2;//y'

	y2 = plain1^plain2;//x'

	i = y2.BitToInt(6);//i
	j = y1.BitToInt(4);//j
	ND[i*16+j] ++;
	
}

void Des::RandonS()
{
	srand(time(NULL));
	int i,j;
	for(i = 0; i < 4 ; i ++)
	{
		for(j = 0 ; j < 16 ; j++ )
		{
			S[8][i][j] = rand() % 16;
			cout<<setw(4)<<left<<S[8][i][j];
		}
		cout<<endl;
	}
}
//private:
int Des::E[]  ={31, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 8,
                7, 8, 9,10,11,12,11,12,13,14,15,16,
                15,16,17,18,19,20,19,20,21,22,23,24,
                23,24,25,26,27,28,27,28,29,30,31, 0};
int Des::P[]  ={15, 6,19,21,28,11,27,16, 0,14,22,25, 4,17,30, 9,
                 1, 7,23,13,31,26, 2, 8,18,12,29, 5,21,10, 3,24};
int Des::IP[] ={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,
			    56,48,40,32,24,16, 8, 0,58,50,42,34,26,18,10, 2,	
			    60,52,44,36,28,20,12, 4,62,54,46,38,30,22,14, 6};

int Des::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,32, 0,40, 8,48,16,56,24};

int Des::pc_1[]={56,48,40,32,24,16, 8, 0,57,49,41,33,25,17,
				  9, 1,58,50,42,34,26,18,10, 2,59,51,43,35,
				 62,54,46,38,30,22,14, 6,61,53,45,37,29,21,
				 13, 5,60,52,44,36,28,20,12, 4,27,19,11, 3};

int Des::pc_2[]={13,16,10,23, 0, 4, 2,27,14, 5,20, 9,22,18,11, 3,
				 25, 7,15, 6,26,19,12, 1,40,51,30,36,46,54,29,39,
				 50,44,32,47,43,48,38,55,33,52,45,41,49,35,28,31};

int Des::S[10][4][16]={
	{{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
	{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}},

	{{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}},

	{{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}},

	{{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}},

	{{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}},

	{{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}},

	{{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}},

	{{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}},
	
/*	{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
	{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
	{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
	{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}},//*/

/*	{{0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3},
	{4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7},
	{8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11},
	{12,12,12,12,13,13,13,13,14,14,14,14,15,15,15,15}}//*/

	{{12,0,5,6,7,11,11,4,14,15,2,8,9,8,7,9},
	{2,10,13,3,7,8,12,6,2,5,13,2,11,9,4,1},
    {11,6,13,5,5,11,10,5,1,9,11,2,8,5,15,14},
    {7,13,11,15,13,15,9,1,7,6,2,10,15,5,14,1}}, 	//random*/
					
	{{4,9,14,3,8,13,2,7,12,1,6,11,0,5,10,15},
	{11,0,5,10,15,4,9,14,3,8,13,2,7,12,1,6},
	{2,7,12,1,6,11,0,5,10,15,4,9,14,3,8,13},
	{9,14,3,8,13,2,7,12,1,6,11,0,5,10,15,4}} //b=7x+5y+4 mod 16*/

};

⌨️ 快捷键说明

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