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

📄 des.cpp

📁 用C++开发的一个简单的DES加密解密程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	S[0][2][3] = 3;

	S[0][3][0] = 3;
	S[0][3][1] = 1;
	S[0][3][2] = 3;
	S[0][3][3] = 2;
}

void CDES::FillS1_SDES()
{
	S[1][0][0] = 0;
	S[1][0][1] = 1;
	S[1][0][2] = 2;
	S[1][0][3] = 3;

	S[1][1][0] = 2;
	S[1][1][1] = 0;
	S[1][1][2] = 1;
	S[1][1][3] = 3;

	S[1][2][0] = 3;
	S[1][2][1] = 0;
	S[1][2][2] = 1;
	S[1][2][3] = 0;

	S[1][3][0] = 2;
	S[1][3][1] = 1;
	S[1][3][2] = 0;
	S[1][3][3] = 3;
}

char* CDES::GetSValue(char *b, int s)
{
	char ra,rb,ca,cb;
	char* svalue;

#ifdef SDES
	if (s == 0)			// value from S0
	{
		ra = b[1-1];
		rb = b[4-1];

		ca = b[2-1];
		cb = b[3-1];
	}
	else if (s == 1)	// value from S1
	{
		ra = b[5-1];
		rb = b[8-1];

		ca = b[6-1];
		cb = b[7-1];
	}
	
	svalue = Bin2bit(GetValue(Dec2bit(ra,rb),Dec2bit(ca,cb),s));
#else
	char cc,cd;

	if (s == 0)
	{
		ra = b[1-1];
		rb = b[6-1];

		ca = b[2-1];
		cb = b[3-1];
		cc = b[4-1];
		cd = b[5-1];
	}
	else if (s == 1)
	{
		ra = b[7-1];
		rb = b[12-1];

		ca = b[8-1];
		cb = b[9-1];
		cc = b[10-1];
		cd = b[11-1];
	}
	else if (s == 2)
	{
		ra = b[13-1];
		rb = b[18-1];

		ca = b[14-1];
		cb = b[15-1];
		cc = b[16-1];
		cd = b[17-1];
	}
	else if (s == 3)
	{
		ra = b[19-1];
		rb = b[24-1];

		ca = b[20-1];
		cb = b[21-1];
		cc = b[22-1];
		cd = b[23-1];
	}
	else if (s == 4)
	{
		ra = b[25-1];
		rb = b[30-1];

		ca = b[26-1];
		cb = b[27-1];
		cc = b[28-1];
		cd = b[29-1];
	}
	else if (s == 5)
	{
		ra = b[31-1];
		rb = b[36-1];

		ca = b[32-1];
		cb = b[33-1];
		cc = b[34-1];
		cd = b[35-1];
	}
	else if (s == 6)
	{
		ra = b[37-1];
		rb = b[42-1];

		ca = b[38-1];
		cb = b[39-1];
		cc = b[40-1];
		cd = b[41-1];
	}
	else if (s == 7)
	{
		ra = b[43-1];
		rb = b[48-1];

		ca = b[44-1];
		cb = b[45-1];
		cc = b[46-1];
		cd = b[47-1];
	}

	svalue = Bin4bit(GetValue(Dec2bit(ra,rb),Dec4bit(ca,cb,cc,cd),s));
#endif
	return svalue;
}

int CDES::GetValue(int a, int b, int s)
{
	cout << endl << "Getting S" << s << " value of " << a << " " << b;

	return S[s][a][b];
}

int CDES::Dec2bit(char a, char b)
{
	if (a == '0' && b == '0')
		return 0;
	else if (a == '0' && b == '1')
		return 1;
	else if (a == '1' && b == '0')
		return 2;
	else
		return 3;
}

int CDES::Dec4bit(char a, char b, char c, char d)
{
	if		(a == '0' && b == '0' && c == '0' && d == '0')
		return 0;
	else if (a == '0' && b == '0' && c == '0' && d == '1')
		return 1;
	else if (a == '0' && b == '0' && c == '1' && d == '0')
		return 2;
	else if (a == '0' && b == '0' && c == '1' && d == '1')
		return 3;
	else if (a == '0' && b == '1' && c == '0' && d == '0')
		return 4;
	else if (a == '0' && b == '1' && c == '0' && d == '1')
		return 5;
	else if (a == '0' && b == '1' && c == '1' && d == '0')
		return 6;
	else if (a == '0' && b == '1' && c == '1' && d == '1')
		return 7;
	else if (a == '1' && b == '0' && c == '0' && d == '0')
		return 8;
	else if (a == '1' && b == '0' && c == '0' && d == '1')
		return 9;
	else if (a == '1' && b == '0' && c == '1' && d == '0')
		return 10;
	else if (a == '1' && b == '0' && c == '1' && d == '1')
		return 11;
	else if (a == '1' && b == '1' && c == '0' && d == '0')
		return 12;
	else if (a == '1' && b == '1' && c == '0' && d == '1')
		return 13;
	else if (a == '1' && b == '1' && c == '1' && d == '0')
		return 14;
	else
		return 15;
}

char* CDES::Bin2bit(int a)
{
	if (a == 0)
		return "00";
	else if (a == 1)
		return "01";
	else if (a == 2)
		return "10";
	else
		return "11";
}

char* CDES::Bin4bit(int a)
{
	if		(a == 0)
		return "0000";
	else if (a == 1)
		return "0001";
	else if (a == 2)
		return "0010";
	else if (a == 3)
		return "0011";
	else if (a == 4)
		return "0100";
	else if (a == 5)
		return "0101";
	else if (a == 6)
		return "0110";
	else if (a == 7)
		return "0111";
	else if (a == 8)
		return "1000";
	else if (a == 9)
		return "1001";
	else if (a == 10)
		return "1010";
	else if (a == 11)
		return "1011";
	else if (a == 12)
		return "1100";
	else if (a == 13)
		return "1101";
	else if (a == 14)
		return "1110";
	else
		return "1111";
}

char* CDES::JoinSres(char *r1, char *r2)
{
	char *res = new char[DATA_LENGTH/2];
	int i,j;

	for (i = 0 ; i < ((DATA_LENGTH/2)/2) ; i++)
	{
		res[i] = r1[i];
	}

	for (i = ((DATA_LENGTH/2)/2),j = 0 ; j < ((DATA_LENGTH/2)/2) ; i++,j++)
	{
		res[i] = r2[j];
	}

	return res;
}

char* CDES::PermuteSres(char *sr)
{
	char *r = new char[(DATA_LENGTH/2)];

	r[0] = sr[2-1];
	r[1] = sr[4-1];
	r[2] = sr[3-1];
	r[3] = sr[1-1];

	return r;
}

void CDES::SampleKeys()
{
	key[1-1][0] = '1';
	key[1-1][1] = '0';
	key[1-1][2] = '1';
	key[1-1][3] = '0';
	key[1-1][4] = '0';
	key[1-1][5] = '1';
	key[1-1][6] = '0';
	key[1-1][7] = '0';

	key[2-1][0] = '0';
	key[2-1][1] = '1';
	key[2-1][2] = '0';
	key[2-1][3] = '0';
	key[2-1][4] = '0';
	key[2-1][5] = '0';
	key[2-1][6] = '1';
	key[2-1][7] = '1';
}

void CDES::print(char *d, int length)
{
	int i;

	cout << endl;
	for (i = 0 ; i < length ; i++)
	{
		cout << d[i];
	}
	cout << endl;
}

char* CDES::XORLandP(char *left, char *p4r)
{
	int i;
	char *res = new char[DATA_LENGTH/2];

	for (i = 0 ; i < (DATA_LENGTH/2) ; i++)
	{
		res[i] = XOR(left[i],p4r[i]);
	}

	return res;
}

char* CDES::FKFinal(char *xr, char *right)
{
	char* res = new char[DATA_LENGTH];
	int i;

	for (i = 0 ; i < (DATA_LENGTH/2) ; i++)
	{
		res[i] = xr[i];
	}

	for (i = (DATA_LENGTH/2) ; i < DATA_LENGTH ; i++)
	{
		res[i] = right[i];
	}

	return res;
}

char* CDES::FKFunction(char *ip,int f)
{
	// begin the KF function
	char *ep = ExtendedPermutation(ip);

	cout << endl << "Extended Permutation";
	print(ep,KEY_P_LENGTH);

	cout << endl << "The k" << f;
	print(key[f-1],KEY_P_LENGTH);

	char *keyXor = XORwithKey(ep,f);

	cout << endl << "XOR with k" << f;
	print(keyXor,KEY_P_LENGTH);

	char *S0res = GetSValue(keyXor,0);

	cout << endl << "S0 Result";
#ifdef SDES
	print(S0res,DATA_LENGTH/4);
#else
	print(S0res,4);
#endif

	char *S1res = GetSValue(keyXor,1);

	cout << endl << "S1 Result";
#ifdef SDES
	print(S1res,DATA_LENGTH/4);
#else
	print(S1res,4);
#endif

#ifdef DES
	char *S2res = GetSValue(keyXor,2);
	cout << endl << "S2 Result";
	print(S2res,4);

	char *S3res = GetSValue(keyXor,3);
	cout << endl << "S3 Result";
	print(S3res,4);

	char *S4res = GetSValue(keyXor,4);
	cout << endl << "S4 Result";
	print(S4res,4);

	char *S5res = GetSValue(keyXor,5);
	cout << endl << "S5 Result";
	print(S5res,4);

	char *S6res = GetSValue(keyXor,6);
	cout << endl << "S6 Result";
	print(S6res,4);

	char *S7res = GetSValue(keyXor,7);
	cout << endl << "S7 Result";
	print(S7res,4);
#endif

#ifdef SDES
	char *Sresult = JoinSres(S0res,S1res);
	cout << endl << "Joined S0 and S1 Results";
#else
	char *Sresult = JoinSres_DES(S0res,S1res,S2res,S3res,S4res,S5res,S6res,S7res);
	cout << endl << "Joined Printed S Results";
#endif

	print(Sresult,DATA_LENGTH/2);

#ifdef SDES
	char *pSres = PermuteSres(Sresult);
	cout << endl << "Permutation P4";
#else
	char *pSres = PermuteSres_DES(Sresult);
	cout << endl << "Permutation P32";
#endif

	print(pSres,DATA_LENGTH/2);

	char *XorLandPr = XORLandP(ip,pSres);
#ifdef SDES
	cout << endl << "XOR L and P4 Result";
#else
	cout << endl << "XOR L and P32 Result";
#endif

	print(XorLandPr,DATA_LENGTH/2);

	char *FKFinalRes = FKFinal(XorLandPr,ip);

	return FKFinalRes;
}

char* CDES::Switch(char *p)
{
	int i,j;
	char *res = new char[DATA_LENGTH];

	for (i = 0,j = (DATA_LENGTH/2) ; i < (DATA_LENGTH/2) ; i++,j++)
	{
		res[i] = p[j];
	}

	for (i = (DATA_LENGTH/2),j = 0 ; i < DATA_LENGTH ; i++,j++)
	{
		res[i] = p[j];
	}

	return res;
}

char* CDES::IPInverse(char *r)
{
	char *res = new char[DATA_LENGTH];

#ifdef SDES
	res[0] = r[4-1];
	res[1] = r[1-1];
	res[2] = r[3-1];
	res[3] = r[5-1];
	res[4] = r[7-1];
	res[5] = r[2-1];
	res[6] = r[8-1];
	res[7] = r[6-1];
#else
	res[0] = r[40-1];
	res[1] = r[8-1];
	res[2] = r[48-1];
	res[3] = r[16-1];
	res[4] = r[56-1];
	res[5] = r[24-1];
	res[6] = r[64-1];

⌨️ 快捷键说明

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