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

📄 sdes.cpp

📁 密码学实验 SDES
💻 CPP
字号:
// SDes.cpp: implementation of the CSDes class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Ldw.h"
#include "SDes.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

const byte S0[4][4] ={1,0,3,2,
					  3,2,1,0,
					  0,2,1,3,
					  3,1,3,2};

const byte S1[4][4] ={0,1,2,3,
					  2,0,1,3,
					  3,0,1,0,
					  2,1,0,3};

const byte ip[8]  ={2,6,3,1,4,8,5,7};
const byte ip_1[8]={4,1,3,5,7,2,8,6};
const byte p4 [8] ={2,4,3,1,2,4,3,1};
const byte ep[8]  ={4,1,2,3,2,3,4,1};
const byte p10[16] = {1,1,1,1,1,1,9,11,8,13,10,16,7,15,14,12};
const byte p8[16] = {1,1,1,1,1,1,1,1,12,9,13,10,14,11,16,15};

///////////////////////////////////////////////////////////////////////
const byte and8[9] = {0xFF,0x7F,0XBF,0XDF,0XEF,0XF7,0XFB,0XFD,0XFE};

const byte or8 [9] = {0xFF,0x80,0X40,0X20,0X10,0X08,0X04,0X02,0X01};

const word and16[17] = {0xFF,0X7FFF,0XBFFF,0XDFFF,0XEFFF,0XF7FF,0XFBFF,0XFDFF,0xFEFF,
					         0XFF7F,0XFFBF,0XFFDF,0XFFEF,0XFFF7,0XFFFB,0XFFFD,0xFFFE};

const word or16 [17] = {0xFF,0X8000,0X4000,0X2000,0X1000,0X0800,0X0400,0X0200,0x0100,
						     0X0080,0X0040,0X0020,0X0010,0X0008,0X0004,0X0002,0x0001};

CSDes::CSDes()
{
	bytes	= NULL;
	length	= 0;
}
CSDes::CSDes(long len)
{
	length = len;
	bytes = new byte[len+1];
}
CSDes::CSDes(char *p)
{
	length = strlen(p);
	bytes = new byte[length +1];
	if(bytes)
	{
		strcpy((char *)bytes,p);
	}
}
CSDes::CSDes(CString p)
{
	length = p.GetLength();
	bytes = new byte[length +1];
	if(bytes)
	{
		strcpy((char *)bytes,p.GetBuffer(0));
	}
}
CSDes::CSDes(byte *p,long len)
{
	length = len;
	bytes = new byte[length +1];
	if(bytes)
	{
		for(long i=0;i<length;i++)
		{
			bytes[i] = p[i];
		}
	}
}
void CSDes::SetBytes(byte *bt,long len)
{
	if(bytes)
	{
		delete []bytes;
		bytes = NULL;
	}
	length = len;
	bytes = new byte[length +1];
	if(bytes)
	{
		for(long i=0;i<length;i++)
		{
			bytes[i] = bt[i];
		}
	}
}
void CSDes::SetBytes(CString bt)
{
	CString bts = "";
	for(int j=0;j<bt.GetLength();j++)
	{
		if(bt.GetAt(j) == '1' || bt.GetAt(j) == '0')
		{
			bts += bt.GetAt(j);
		}
	}
	if(bts.GetLength() % 8 != 0)
	{
		bts += CString("00000000").Mid(1,8-(bts.GetLength() % 8));
	}
	if(bytes)
	{
		delete []bytes;
		bytes = NULL;
	}
	length = long(bts.GetLength() / 8);
	bytes = new byte[length +1];
	for(int i=0;i<bts.GetLength();i++)
	{
		SetBit(bytes[int(i/8)],i%8+1,bts.GetAt(i) == '1');
	}
}
CSDes::~CSDes()
{
	if(bytes)
	{
		delete []bytes;
		length = 0;
	}
}
bool CSDes::GetBit(byte  bt,byte pc)
{
	return (bt&or8[pc]) == or8[pc];
}
void CSDes::SetBit(byte &bt,byte pc,bool dc)
{
	if(dc)
	{
		bt = bt & and8[pc] | or8[pc];
	}
	else
	{
		bt = bt & and8[pc];
	}
	
}
bool CSDes::GetBit  (word  bt,byte pc)
{
	return (bt&or16[pc]) == or16[pc];
}
void CSDes::SetBit  (word &bt,byte pc,bool dc)
{
	if(dc)
	{
		bt = bt & and16[pc] | or16[pc];
	}
	else
	{
		bt = bt & and16[pc];
	}
}
void CSDes::MoveL   (byte &bt,byte len,byte ti)
{
	byte and=0xFF;
	and = and >> (8-len);
	bool tmp;
	for(int i=0;i<ti;i++)
	{
		tmp = GetBit(bt,9-len);
		bt  = bt << 1;
		SetBit(bt,8,tmp);
		bt = bt & and;
	}
}
void CSDes::MoveR   (byte &bt,byte len,byte ti)
{
	bool tmp;
	for(int i=0;i<ti;i++)
	{
		tmp = GetBit(bt,8);
		bt  = bt >> 1;
		SetBit(bt,9-len,tmp);
	}
}

void CSDes::Swap4 (byte &bt)
{
	bt = (bt << 4 ) | (bt >> 4);
}
void CSDes::SwapAll (byte &bt,byte b7,byte b6,byte b5,byte b4,byte b3,byte b2,byte b1,byte b0)
{
	byte tmp=0;
	if(bt & or8[b7]) tmp |= or8[1];
	if(bt & or8[b6]) tmp |= or8[2];
	if(bt & or8[b5]) tmp |= or8[3];
	if(bt & or8[b4]) tmp |= or8[4];
	if(bt & or8[b3]) tmp |= or8[5];
	if(bt & or8[b2]) tmp |= or8[6];
	if(bt & or8[b1]) tmp |= or8[7];
	if(bt & or8[b0]) tmp |= or8[8];
	bt = tmp;
}
void CSDes::SwapAll (byte &bt,const byte *pc)
{
	byte tmp=0;
	for(byte i=0;i<8;i++)
	{
		if(bt & or8[pc[i]])
		{
			tmp |= or8[i+1];
		}
	}
	bt = tmp;
}
void CSDes::SwapAll (word &bt,const byte *pc)
{
	word tmp=0;
	for(byte i=0;i<16;i++)
	{
		if(bt & or16[pc[i]])
		{
			tmp |= or16[i+1];
		}
	}
	bt = tmp;
}

CString CSDes::GetBitString()
{
	CString str="";
	for(int i=0;i<length;i++)
	{
		for(int j=1;j<=8;j++)
		{
			if(GetBit(bytes[i],j))
			{
				str += "1";
			}
			else
			{
				str += "0";
			}
		}
		str += " ";
	}
	return str;
}
CString CSDes::GetString()
{
	bytes[length]=0;
	CString str = (char *)bytes;
	return str;
}

word CSDes::GetKey()
{
	byte k1,k2,tmp1,tmp2;
	word tmp,keytmp=0;
	keytmp = key;
	
	SwapAll(keytmp,p10);
	
	tmp1= (keytmp >> 5) & 0x001F;
	tmp2= keytmp & 0x001F;
	MoveL(tmp1,5,1);
	MoveL(tmp2,5,1);
	keytmp = tmp1;
	keytmp = keytmp << 5;
	tmp = tmp2;
	keytmp = keytmp | tmp;
	k1=k2=0;

	SwapAll(keytmp,p8);
	k1 = keytmp & 0x00FF;
	MoveL(tmp1,5,2);
	MoveL(tmp2,5,2);
	keytmp = tmp1;
	keytmp = keytmp << 5;
	tmp = tmp2;
	keytmp = keytmp | tmp;

	SwapAll(keytmp,p8);
	k2 = keytmp & 0x00FF;

	keytmp = key1=k1;
	keytmp = keytmp << 8;
	tmp = key2 = k2;
	keytmp = keytmp | tmp;
	return keytmp;
}
void CSDes::IP		 (byte &bt)
{
	SwapAll(bt,2,6,3,1,4,8,5,7);
}
void CSDes::IP_1     (byte &bt)
{
	SwapAll(bt,4,1,3,5,7,2,8,6);
}
void CSDes::FK(byte &bt,byte k)
{
	byte tmp;
	tmp = bt << 4;
	F(tmp,k);
	tmp = (tmp ^ bt) & 0xF0;
	tmp = tmp | (bt &0x0F);
	bt = tmp;	
}
void CSDes::F(byte &bt,byte k)
{
	byte tmp,tmp1,tmp2,re,x,y;
	re = 0x00;

	tmp = bt;
	SwapAll(tmp,4,1,2,3,2,3,4,1);

	tmp = tmp ^ k;
	x=y=0;
	if(tmp & or8[1]) x |= 0x02;
	if(tmp & or8[4]) x |= 0x01;
	if(tmp & or8[2]) y |= 0x02;
	if(tmp & or8[3]) y |= 0x01;
	tmp1 = S0[x][y];
	x=y=0;
	if(tmp & or8[5]) x |= 0x02;
	if(tmp & or8[8]) x |= 0x01;
	if(tmp & or8[6]) y |= 0x02;
	if(tmp & or8[7]) y |= 0x01;
	tmp2 = S1[x][y];

	bt = tmp1 << 6;
	bt |= tmp2 <<4;
	bt &= 0xF0;
	SwapAll(bt,2,4,3,1,2,4,3,1);
}
void CSDes::DesOne  (byte &bt)
{
	IP(bt);
	FK(bt,key1);
	Swap4(bt);
	FK(bt,key2);
	IP_1(bt);
}
void CSDes::UnDesOne(byte &bt)
{
	IP(bt);
	FK(bt,key2);
	Swap4(bt);
	FK(bt,key1);
	IP_1(bt);
}

void CSDes::Des()
{
	word k = GetKey();
	for(long i=0;i<length;i++)
	{
		DesOne(bytes[i]);
	}
}
void CSDes::UnDes()
{
	word k = GetKey();
	for(long i=0;i<length;i++)
	{
		UnDesOne(bytes[i]);
	}
}

void CSDes::SetKey(word new_key)
{
	key = new_key;
	GetKey();
}
void CSDes::SetKey(CString new_key)
{
	CString bts = "";
	for(int j=0;j<new_key.GetLength();j++)
	{
		if(new_key.GetAt(j) == '1' || new_key.GetAt(j) == '0')
		{
			bts += new_key.GetAt(j);
		}
	}
	if(bts.GetLength() < 10)
	{
		bts += CString("00000000000").Mid(1,10-bts.GetLength());
	}

	if(bts.GetLength() >=10)
	{
		key = 0;
		for(int i=7;i<=16;i++)
		{
			char ch = bts.GetAt(i-7);
			SetBit(key,i,bts.GetAt(i-7) == '1');
		}
	}
	GetKey();
}

⌨️ 快捷键说明

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