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

📄 des.cpp

📁 应用编码与计算机密码学>程序 如果好的话请发言
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		rkey &= SEVEN_F;
		return;
	}
	lkey <<= 1;
	rkey <<= 1;
	if (lkey&ONE_SEVEN_ZERO) lkey++;
	if (rkey&ONE_SEVEN_ZERO) rkey++;
	lkey &= SEVEN_F;
	rkey &= SEVEN_F;
	return;
}


inline void RotateR(unsigned long& lkey, unsigned long& rkey, int round)
{//If RotateL and RoateR aren't used in the same time, the highest 4 bits of lkey and rkey
 // are zero.
 //INPUT: HIGHEST 4 BITS MUST BE ZERO IN BOTH LKEY AND RKEY!
	static short ri;
	ri = RotateRIndex[round];
	if (ri == 0)
		return;
	if (lkey&1) lkey |= ONE_SEVEN_ZERO;
	if (rkey&1) rkey |= ONE_SEVEN_ZERO;
	lkey >>= 1;
	rkey >>= 1;
	if (ri == 1) return;
	if (lkey & 1) lkey |= ONE_SEVEN_ZERO;
	if (rkey & 1) rkey |= ONE_SEVEN_ZERO;
	lkey >>= 1;
	rkey >>= 1;
	return;
}

inline void Sub_Key(unsigned long& lkey, unsigned long& rkey)
{
	int i, j;
	unsigned long temp1 = 0, temp2 = 0;

	for (i = 0; i < 24; i += 8)
	{
		for (j = 0; j < 4; j++)
		{
			temp1 <<= 1;
			temp1 |= (lkey & SubBox[i + j]) ? 1 : 0;
		}
		for (; j < 8; j++)
		{
			temp1 <<= 1;
			temp1 |= (rkey & SubBox[i + j]) ? 1 : 0;
		}
	}
	for (j = 0; j < 4; j++)
	{
		temp1 <<= 1;
		temp1 |= (lkey & SubBox[i + j]) ? 1 : 0;
	}
	for (i = 28; i < 52; i += 8)
	{
		for (j = 0; j < 4; j++)
		{
			temp2 <<= 1;
			temp2 |= (lkey & SubBox[i + j]) ? 1 : 0;
		}
		for (; j < 8; j++)
		{
			temp2 <<= 1;
			temp2 |= (rkey & SubBox[i + j]) ? 1 : 0;
		}
	}
	for (j = 0; j < 4; j++)
	{
		temp2 <<= 1;
		temp2 |= (rkey&SubBox[i + j]) ? 1 : 0;
	}
	lkey = temp1;
	rkey = temp2;
}

inline void Compression(unsigned long lkey, unsigned long rkey, unsigned char cmpkey[8])
{//Results are in cmpkey[0~7]. The highest 2 bits should be 0.
	int i, k, j;

	for (i = 0; i < 8; i++)
		cmpkey[i] = 0;
	k = 0;
	for (i = 0; i < 4; i++)
	{
		k += 6;
		for (j = 0; j < 6; j++)
		{
			cmpkey[i] <<= 1;
			cmpkey[i] |= (lkey & Compression_BoxL[k + j]) ? 1 : 0;
			cmpkey[i+4] <<= 1;
			cmpkey[i+4] |= (rkey & Compression_BoxR[k + j]) ? 1 : 0;
		}
	}
}

inline void Expansion(unsigned long input, unsigned char expword[8])
{//Results of expansion are in expword[0~7].
 //The upper 2 bits of each expword[i] (i = 0 to 7) must be 0. This is done outside.
	unsigned char expword8;

	expword8 = unsigned char(input) & 1;
	expword[0] = unsigned char(input) << 1;

	input >>= 3;
	expword[1] = (unsigned char)input;
	input >>= 4;
	expword[2] = (unsigned char)input;
	input >>= 4;
	expword[3] = (unsigned char)input;
	input >>= 4;
	expword[4] = (unsigned char)input;
	input >>= 4;
	expword[5] = (unsigned char)input;
	input >>= 4;
	expword[6] = (unsigned char)input;

	input >>= 4;

	expword[0] |= ((unsigned char(input) & 0x10) ? 1 : 0);
	expword[7] = (unsigned char(input) & 0x1f) | (expword8 ? 0x20 : 0);
}

inline unsigned long Permutation(unsigned long& input)
{
	int i;
	unsigned long dest = 0;
	
	for (i = 0; i < 32; i++)
	{
		dest <<= 1;
		dest |= ((input & P_Box[i]) ? 1 : 0);
	}
	return dest;
}

//Four highest bits of expword and compressed_key must be 0.

void desenc(unsigned long& LData, unsigned long& RData, 
			unsigned long LKey, unsigned long RKey, 
			unsigned long S_Box[512])
{
	unsigned long Rnext, dword;
	unsigned char expword[8];
	unsigned char cmpword[8];
	unsigned long *exph = (unsigned long*) expword, *expl = (unsigned long*) expword + 1;
	unsigned long *cmph = (unsigned long*) cmpword, *cmpl = (unsigned long*) cmpword + 1;
	int i;

	Sub_Key(LKey, RKey);
	for (i = 0; i < 15; i++)
	{
		dword = 0;
		RotateL(LKey, RKey, i);
		Compression(LKey, RKey, cmpword);
		Expansion(RData, expword);
		*exph = *exph & 0x3f3f3f3f ^ *cmph;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
		*expl = *expl & 0x3f3f3f3f ^ *cmpl;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
		S_Box_Substitude
		Rnext = LData ^ dword;
		LData = RData;
		RData = Rnext;
	}
	dword = 0;
	RotateL(LKey, RKey, i);	//i=15
	Compression(LKey, RKey, cmpword);
	Expansion(RData, expword);
	*exph = *exph & 0x3f3f3f3f ^ *cmph;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
	*expl = *expl & 0x3f3f3f3f ^ *cmpl;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
	S_Box_Substitude;
	LData ^= dword;
	LKey = RKey = 0;	//Clear keys.
}

void desdec(unsigned long& LData, unsigned long& RData, 
			unsigned long LKey, unsigned long RKey, 
			unsigned long S_Box[512])
{
	unsigned long Rnext, dword;
	unsigned char expword[8];
	unsigned char cmpword[8];
	unsigned long *exph = (unsigned long*) expword, *expl = (unsigned long*) expword + 1;
	unsigned long *cmph = (unsigned long*) cmpword, *cmpl = (unsigned long*) cmpword + 1;
	int i;

	Sub_Key(LKey, RKey);
	for (i = 0; i < 15; i++)
	{
		dword = 0;
		RotateR(LKey, RKey, i);
		Compression(LKey, RKey, cmpword);
		Expansion(RData, expword);
		*exph = *exph & 0x3f3f3f3f ^ *cmph;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
		*expl = *expl & 0x3f3f3f3f ^ *cmpl;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
		S_Box_Substitude
		Rnext = LData ^ dword;
		LData = RData;
		RData = Rnext;
	}
	dword = 0;
	RotateR(LKey, RKey, i);	//i=15
	Compression(LKey, RKey, cmpword);
	Expansion(RData, expword);
	*exph = *exph & 0x3f3f3f3f ^ *cmph;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
	*expl = *expl & 0x3f3f3f3f ^ *cmpl;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
	S_Box_Substitude;
	LData ^= dword;
	LKey = RKey = 0;	//Clear keys.
}

void init_s_box(unsigned long LExKey, 
				unsigned long RExKey, 
				unsigned long S_Box[512])
{
	unsigned long xchgflag[16];
	unsigned long mask[8] = {0xf0000000, 0xf000000, 0xf00000, 0xf0000, 0xf000, 0xf00, 0xf0, 0xf};
	unsigned long temp;
	int i, j, k;
	
	for (j = 0; j < 512; j++)
		S_Box[j] = Key_Relative_S_Box[j];

	for (j = 0; j < 16; j++)
	{
		xchgflag[j] = LExKey&1;
		LExKey >>= 1;
	}
	
	RExKey = Permutation(RExKey);
	for (i = 0; i < 8; i++)
		mask[i] = Permutation(mask[i]);
	for (i = 0; i < 8; i++)
	{
		if (xchgflag[i + i])
		{
			//Exchange tow rows.
			k = (i << 6) + 64;
			for(j = (i << 6); j < k ; j += 2)
			{
				temp = S_Box[j];
				S_Box[j] = S_Box[j + 1];
				S_Box[j + 1] = temp;
			}
		}
		if (xchgflag[i + i + 1])
		{
			//Exchange sixteen columns.
			k = (i << 6) + 16;
			for (j = (i << 6); j < k; j++)
			{
				temp = S_Box[j];
				S_Box[j] = S_Box[j + 16];
				S_Box[j + 16] = temp;
			}
			k += 32;	//k = i << 6 + 48;
			for (j = (i << 6) + 32; j < k; j++)
			{
				temp = S_Box[j];
				S_Box[j] = S_Box[j + 16];
				S_Box[j + 16] = temp;
			}
		}
		k = (i << 6) + 64;
		for(j = (i << 6); j < k; j++)
		{
			S_Box[j] ^= (RExKey & mask[i]);
		}
	}
	LExKey = RExKey = 0;	//Hide keys.
}

void reset_s_box(unsigned long S_Box[512])
{
	for (int i = 0; i < 512; i++)
		S_Box[i] = Origin_S_Box[i];
}

long isweakkey(unsigned long& LKey, unsigned long& RKey)
{
	for(int i = 0; i < 128; ++i, ++i)
	{
		if(WeakKeys[i] == LKey && WeakKeys[i + 1] == RKey)
			return -1;
	}
	return 0;
}

void init_enc_cmpkeys(unsigned long LKey, 
					  unsigned long RKey, 
					  unsigned char compressed_key[128])
{
	Sub_Key(LKey, RKey);
	for(int i = 0; i < 16; i++)
	{
		RotateL(LKey, RKey, i);
		Compression(LKey, RKey, compressed_key + i * 8);
	}
}

void init_dec_cmpkeys(unsigned long LKey, 
					  unsigned long RKey, 
					  unsigned char compressed_key[128])
{
	Sub_Key(LKey, RKey);
	for(int i = 0; i < 16; i++)
	{
		RotateR(LKey, RKey, i);
		Compression(LKey, RKey, compressed_key + i * 8);
	}
}

//Four highest bits of expword and compressed_key must be 0.

void fast_desenc(unsigned long& LData, unsigned long& RData, 
				 unsigned char compressed_key[128],
				 unsigned long S_Box[512])
{//For safety,  compressed_keys should be clear after use.
	unsigned long Rnext, dword;
	unsigned char expword[8];
	unsigned long *exph = (unsigned long*) expword, *expl = (unsigned long*)expword + 1;
	unsigned long *cmph = (unsigned long*) compressed_key, *cmpl = cmph + 1;
	int i;

	for(i = 0; i < 15; i++)
	{
		dword = 0;
		Expansion(RData, expword);
		*exph = *exph & 0x3f3f3f3f ^ *cmph;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
		*expl = *expl & 0x3f3f3f3f ^ *cmpl;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
		S_Box_Substitude
		Rnext = LData ^ dword;
		LData = RData;
		RData = Rnext;
		cmph += 2;
		cmpl += 2;
	}
	dword = 0;
	Expansion(RData, expword);
	*exph = *exph & 0x3f3f3f3f ^ *cmph;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
	*expl = *expl & 0x3f3f3f3f ^ *cmpl;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
	S_Box_Substitude;
	LData ^= dword;
}

void fast_desdec(unsigned long& LData, unsigned long& RData, 
				 unsigned char compressed_key[128],
				 unsigned long S_Box[512])
{//For safety,  compressed_keys should be clear after use.
	unsigned long Rnext, dword;
	unsigned char expword[8];
	unsigned long *exph = (unsigned long*) expword, *expl = (unsigned long*) expword + 1;
	unsigned long *cmph = (unsigned long*) compressed_key, *cmpl = cmph+1;
	int i;

	for (i = 0; i < 15; i++)
	{
		dword = 0;
		Expansion(RData, expword);
		*exph = *exph & 0x3f3f3f3f ^ *cmph;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
		*expl = *expl & 0x3f3f3f3f ^ *cmpl;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
		S_Box_Substitude
		Rnext = LData ^ dword;
		LData = RData;
		RData = Rnext;
		cmph += 2;
		cmpl += 2;
	}
	dword = 0;
	Expansion(RData, expword);
	*exph = *exph & 0x3f3f3f3f ^ *cmph;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
	*expl = *expl & 0x3f3f3f3f ^ *cmpl;	//Anding 0x3f3f3f3f is to make the upper 2 bits in each expowrd be 0.
	S_Box_Substitude;
	LData ^= dword;
}

⌨️ 快捷键说明

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