DES分步实现,分轮次实现,F扩展实现,S0盒实现

该文详细介绍了DES加密算法的分步实现,包括IP置换、E扩展、子密钥生成、S盒压缩和P置换等步骤,以及不同轮次下加密效果的比较。通过增加轮数可以提高加密的复杂性和安全性。
摘要由CSDN通过智能技术生成

DES分步实现,分轮次实现,F扩展实现,S0盒实现

文章目的:

  • 记录C++实现DES分步代码
  • 记录不同情况下的实现方式
  • 比较不同方式下的加密情况

文章内容:

1.基础分步实现
CPP文件

#include<iostream>
#include<ctime>
#include <windows.h>
#include"Tables.h"
using namespace std;
//缓存函数
void Start_Sub_EX_Key_S_P_LXO_Times_IPR(int Times) {
	Sub_Key = new BITE * [Times];
	EX_Result = new BITE * [Times];
	KeyXOR_Result=new BITE* [Times];
	S_Result = new BITE * [Times];
	P_Result = new BITE * [Times];
	LXOR_Result = new BITE * [Times];
	REX_Result = new BITE * [Times];
	RKeyXOR_Result = new BITE * [Times];
	RS_Result = new BITE * [Times];
	RP_Result = new BITE * [Times];
	RLXOR_Result = new BITE * [Times];
	TimesTEMP= new BITE * [Times];
	for (int i = 0; i < Times; i++)
	{
		Sub_Key[i] = new BITE[48];
		EX_Result[i] = new BITE[48];
		KeyXOR_Result[i] = new BITE[48];
		S_Result[i] = new BITE[32];
		P_Result[i] = new BITE[32];
		LXOR_Result[i] = new BITE[32];
		REX_Result[i] = new BITE[48];
		RKeyXOR_Result[i] = new BITE[48];
		RS_Result[i] = new BITE[32];
		RP_Result[i] = new BITE[32];
		RLXOR_Result[i] = new BITE[32];
		TimesTEMP[i] = new BITE[64];
	}
}
int Distance_H(BITE * A,BITE *B,int Data_Len) {
	int i = 0;
	int dis = 0;
	for (; i < Data_Len; i++) {
		if (A[i] ^ B[i]) { dis++; }
	}
	return dis;
}
void BiteCopy(BITE* NeedCopy, BITE* Data, int DataLen)
{
	int i = 0;
	for (; i < DataLen; i++) {
		NeedCopy[i] = Data[i];
	}
}
//
void PrintMes(BITE* Mes, int DataLen,int Line) {
	int i = 0;
	for (; i < DataLen; i++) {
		cout << Mes[i] << " ";
		if ((i + 1) % Line == 0) { cout << endl; }
	}
	cout << endl;

}
//异或
void XOR(BITE*Data_Out,BITE* Data_InA,BITE * Data_InB,int DataLen) {
	int i = 0;
	for (; i < DataLen; i++) {
		Data_Out[i]= Data_InA[i] ^ Data_InB[i];
	}
}

//IP置换目的是初步打乱明文,初步混淆,此时得出的数据,每一个都跟明文紧密联系
void IP_change(BITE* Data_Out,BITE* Mes_In)
{
	int i = 0;
	//ip置换就是将二进制明文按照IP置换表的顺序,重排列
	for (; i < 64; i++) {
		Data_Out[i] = Mes_In[IP_Change[i] - 1];
	}
}
//E扩展函数
void Expand(BITE* Data_Out,BITE* Mes_in)
{
	int i = 0;
	for (; i <48; i++)
	{
		Data_Out[i] = Mes_in[E_Change[i] - 1];
	}
}
//子秘钥,只有第一轮需要PC1选择置换,后面需要移位,PC2选择置换
void SubKey(BITE *Data_In,int times) {
	//PCI去除奇偶校验同时选择置换
	//cout << "秘钥8位一行:" << endl;
	//PrintMes(Data_In,64,8);
	BITE* temp=new BITE[56];
	for (int i=0; i < 56; i++) {
		temp[i] = Data_In[PC1_Change[i] - 1];
	}
	//cout << "PCI置换选择后的结果:【7位一排】:" << endl;
	//PrintMes(temp, 56, 7);
	BITE* C = &temp[0];
	BITE* D = &temp[28];
	//cout << "移位前:" << endl;
	//cout << "C:" << endl;
	//PrintMes(C, 28, 28);
	//cout << "D:" << endl;
	//PrintMes(D, 28, 28);
	for (int i=0; i < times; i++) {
	//移位
		if (LSi[i] == 1) {
			BITE A = C[0];
			BITE B = D[0];
			for (int j = 0; j < 27; j++) {
				C[j] = C[j + 1];
				D[j] = D[j + 1];
			}
			C[27] = A;
			D[27] = B;	
		}
		else {
			BITE A1 = C[0];
			BITE A2 = C[1];
			BITE B1 = D[0];
			BITE B2 = D[1];
			for (int j = 1; j < 27; j++) {
				C[j - 1] = C[j + 1];
				D[j - 1] = D[j + 1];
			}
			C[26] = A1;
			C[27] = A2;
			D[26] = B1;
			D[27] = B2;
		}
		//cout << "C"<<i<<":" << endl;
		//PrintMes(C, 28, 28);
		//cout << "D" <<i << ":" << endl;
		//PrintMes(D, 28, 28);
		//PC2选择置换同时存储到子秘钥数组里面
	//	cout << "PC2置换后:" << endl;
		for (int j = 0; j < 48; j++) {
			Sub_Key[i][j] = temp[PC2_Change[j] - 1];
			//cout << Sub_Key[i][j] << " ";
			//if ((j + 1) % 6 == 0) { cout << endl; }
		}
	//	cout << endl;
	}
}
//S盒压缩 6bit-->4bit
void S_BOX(BITE* Data_Out,BITE *Data_In) {
	int i = 0;
	int row = 0;
	int line = 0;
	int temp = 0;
	for (; i < 8; i++) {
		row = Data_In[i * 6] * 2 + Data_In[i*6 + 5];
		line = Data_In[i * 6 + 1] * 8 + Data_In[i * 6 + 2] * 4 + Data_In[i * 6 + 3] * 2 + Data_In[i * 6 + 4];
		temp = S_Box[i][row][line];
		for (int j = 0; j < 4; j++) {
			Data_Out[(i + 1) * 4 - j - 1] = (temp >> j) & 1;
		}
	}
}
void P_change(BITE* Data_Out,BITE* Data_In) {
	int i = 0;
	for (; i < 32; i++) {
		Data_Out[i] = Data_In[P_Change[i] - 1];
	}
}
void IPR_change(BITE* Data_Out,BITE* Data_In) {
	int i = 0;
	for (; i < 64; i++) {
		Data_Out[i] = Data_In[IPR_Change[i] - 1];
	}
}
BITE* DES_ENC(BITE *Data_In,int Times) {
	//加密第一步IP选择置换
	IP_change(IP_Result,Data_In);
	//加密第二部分成L,R部分
	BITE* TEMPLR = new BITE[64];
	BiteCopy(TEMPLR, IP_Result, 64);
	BITE *L = &TEMPLR[0];
	BITE *R = &TEMPLR[32];
	//16轮函数
	int i = 0;
	for (; i < Times; i++) {
	//E扩展
		Expand(EX_Result[i],R);
	//子秘钥异或
		XOR(KeyXOR_Result[i],EX_Result[i], Sub_Key[i],48);
	//S盒压缩
		S_BOX(S_Result[i],KeyXOR_Result[i]);
	//P置换
		P_change(P_Result[i],S_Result[i]);
	//与L异或
		XOR(LXOR_Result[i],L,P_Result[i], 32);
	//R变成L,与L异或的结果变成R
		BiteCopy(L, R,32);
		BiteCopy(R, LXOR_Result[i], 32);
		for (int j = 0; j < 64; j++) {
			TimesTEMP[i][j] = TEMPLR[j];
		}
	}
	//IP逆置换
	IPR_change(IPR_Result,TEMPLR);
	return IPR_Result;
}
BITE* DES_DEC(BITE* Data_In,int Times) {
//解密是加密的逆
//IP置换
	IP_change(RIP_Result, Data_In);
//分组
	BITE* RTEMP = new BITE[64];
	BiteCopy(RTEMP, RIP_Result, 64);
	BITE* L = &RTEMP[0];
	BITE* R = &RTEMP[32];
	//16轮函数
	int i = Times-1;
	for (; i >=0; i--) {
	//E扩展
		Expand(REX_Result[15-i], L);
	//异或
		XOR(RKeyXOR_Result[15 - i], REX_Result[15 - i], Sub_Key[i],48);
	//S盒
		S_BOX(RS_Result[15 - i], RKeyXOR_Result[15 - i]);
	//P置换
		P_change(RP_Result[15-i],RS_Result[15-i]);
	//与R异或
		XOR(RLXOR_Result[15 - i], RP_Result[15 - i], R, 32);
	//交换L部分给到R,异或的结果给到L
		BiteCopy(R, L, 32);
		BiteCopy(L, RLXOR_Result[15 - i], 32);
	}
	//IP逆置换
	IPR_change(RIPR_Result, RTEMP);
	return RIPR_Result;
}
void PrintCheckData() {
	cout << "检查问题:" << endl;
	cout << "IP置换:" << endl;
	PrintMes(IP_Result, 64, 8);
	cout << "R0:" << endl;
	PrintMes(&IP_Result[32], 32, 4);
	cout << "第一轮E扩展:" << endl;
	PrintMes(EX_Result[0], 48, 6);
	cout << "第一轮秘钥:" << endl;
	PrintMes(Sub_Key[0], 48, 6);
	cout << "第一轮K异或" << endl;
	PrintMes(KeyXOR_Result[0], 48, 6);
	cout << "第一轮S盒:" << endl;
	PrintMes(S_Result[0], 32, 4);
	cout << "第一轮P置换:" << endl;
	PrintMes(P_Result[0], 32, 4);
	cout << "L部分:" << endl;
	PrintMes(&IP_Result[0], 32, 4);
	cout << "第一轮L异或:" << endl;
	PrintMes(LXOR_Result[0], 32, 4);
	cout << "第一轮之后的TEMP:" << endl;
	PrintMes(TimesTEMP[0], 64, 8);
	cout << "IPR:" << endl;
	PrintMes(IPR_Result, 64, 8);

}
int main() {
	//
	clock_t  Begin, End;
	double duration;
	int i = 0;
	int times = 48;
	BITE Mes[64];
	BITE* KEY=new BITE[64];
	BITE* Mes_Code = new BITE[64];
	BITE* Mes_UnCode = new BITE[64];
	cout << "输入64位二进制密文" << endl;
	for (; i < 64; i++)
	{
		cin >> Mes[i];
	}
	cout << "输入64位二进制KEY" << endl;
	for (i=0; i < 64; i++)
	{
		cin >> KEY[i];
	}
	Start_Sub_EX_Key_S_P_LXO_Times_IPR(times);
	SubKey(KEY,times);
	cout << "明文8位一组表示" << endl;
	PrintMes(Mes, 64,8);
	//Begin = clock();//开始计时
	Mes_Code = DES_ENC(Mes,times);
	//End = clock();//结束计时
	//duration = double(End - Begin) / CLOCKS_PER_SEC;
	//cout << "tick=" << double(End - Begin) << endl; //运行函数所打的点数
	//PrintCheckData();
	cout << "密文:" << endl;
	PrintMes(Mes_Code, 64, 8);
	//cout <<times<< "轮相似度:[越大相似度越低]" << endl;
	//cout << Distance_H(Mes, Mes_Code,64)<<endl;
	cout << "解密后的明文:" << endl;
	Mes_UnCode=DES_DEC(Mes_Code);
	PrintMes(Mes_UnCode, 64, 8);
	return 0;
}

Tables.h文件

#ifndef TABLES_H
#define TABLES_H
typedef bool BITE;
int IP_Change[64] = {
58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
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
};
int E_Change[48] = {
32,1,2,3,4,5,
4,5,6,7,8,9,
8,9,10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32,1
};
int PC1_Change[56] = {
57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
63,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4
};
int PC2_Change[48] = {
14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,34,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32
};
int LSi[48] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
int S_Box[8][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}
}
};
int P_Change[32] = {
16,7,20,21,
29,12,28,17,
1,15,23,26,
5,18,31,10,
2,8,24,14,
32,27,3,9,
19,13,30,6,
22,11,4,25
};
int IPR_Change[64] = {
40,8,48,16,56,24,64,32,
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
};
BITE **Sub_Key;
BITE* IP_Result = new BITE[64];
BITE** EX_Result;
BITE** KeyXOR_Result;
BITE** S_Result;
BITE** P_Result;
BITE** LXOR_Result;
BITE** TimesTEMP;
BITE* IPR_Result = new BITE[64];

//反向轮的数据
BITE* RIP_Result = new BITE[64];
BITE** REX_Result;
BITE** RKeyXOR_Result;
BITE** RS_Result;
BITE** RP_Result;
BITE** RLXOR_Result;
BITE* RIPR_Result = new BITE[64];
#endif

2.F扩展【扩展成56位】以及S0盒【56压缩到48位】
CPP文件

#include<iostream>
#include<ctime>
#include <windows.h>
#include"Tables.h"
using namespace std;
//缓存函数
void Start_Sub_EX_Key_S_P_LXO_Times_IPR(int Times) {
	Sub_Key = new BITE * [Times];
	EX_Result = new BITE * [Times];
	KeyXOR_Result = new BITE * [Times];
	S0_Result = new BITE * [Times];
	S_Result = new BITE * [Times];
	P_Result = new BITE * [Times];
	LXOR_Result = new BITE * [Times];
	REX_Result = new BITE * [Times];
	RKeyXOR_Result = new BITE * [Times];
	RS_Result = new BITE * [Times];
	RP_Result = new BITE * [Times];
	RLXOR_Result = new BITE * [Times];
	TimesTEMP = new BITE * [Times];
	for (int i = 0; i < Times; i++)
	{
		Sub_Key[i] = new BITE[56];
		EX_Result[i] = new BITE[56];
		KeyXOR_Result[i] = new BITE[56];
		S0_Result[i] = new BITE[48];
		S_Result[i] = new BITE[32];
		P_Result[i] = new BITE[32];
		LXOR_Result[i] = new BITE[32];
		REX_Result[i] = new BITE[56];
		RKeyXOR_Result[i] = new BITE[56];
		RS_Result[i] = new BITE[32];
		RP_Result[i] = new BITE[32];
		RLXOR_Result[i] = new BITE[32];
		TimesTEMP[i] = new BITE[64];
	}
}
float Distance_H(BITE* A, BITE* B, int Data_Len) {
	int i = 0;
	float dis = 0;
	for (; i < Data_Len; i++) {
		if (A[i] ^ B[i]) {
			dis++;
		}
	}
	return dis;
}
void BiteCopy(BITE* NeedCopy, BITE* Data, int DataLen)
{
	int i = 0;
	for (; i < DataLen; i++) {
		NeedCopy[i] = Data[i];
	}
}
//
void PrintMes(BITE* Mes, int DataLen, int Line) {
	int i = 0;
	for (; i < DataLen; i++) {
		cout << Mes[i] << " ";
		if ((i + 1) % Line == 0) { cout << endl; }
	}
	cout << endl;

}
//异或
void XOR(BITE* Data_Out, BITE* Data_InA, BITE* Data_InB, int DataLen) {
	int i = 0;
	for (; i < DataLen; i++) {
		Data_Out[i] = Data_InA[i] ^ Data_InB[i];
	}
}

//IP置换目的是初步打乱明文,初步混淆,此时得出的数据,每一个都跟明文紧密联系
void IP_change(BITE* Data_Out, BITE* Mes_In)
{
	int i = 0;
	//ip置换就是将二进制明文按照IP置换表的顺序,重排列
	for (; i < 64; i++) {
		Data_Out[i] = Mes_In[IP_Change[i] - 1];
	}
}
//E扩展函数
void Expand(BITE* Data_Out, BITE* Mes_in)
{
	int i = 0;
	for (; i < 56; i++)
	{
		Data_Out[i] = Mes_in[E_Change[i] - 1];
	}
}
//子秘钥,只有第一轮需要PC1选择置换,后面需要移位,PC2选择置换
void SubKey(BITE* Data_In, int times) {
	//PCI去除奇偶校验同时选择置换
	//cout << "秘钥8位一行:" << endl;
	//PrintMes(Data_In,64,8);
	BITE* temp = new BITE[56];
	for (int i = 0; i < 56; i++) {
		temp[i] = Data_In[PC1_Change[i] - 1];
	}
	//cout << "PCI置换选择后的结果:【7位一排】:" << endl;
	//PrintMes(temp, 56, 7);
	BITE* C = &temp[0];
	BITE* D = &temp[28];
	//cout << "移位前:" << endl;
	//cout << "C:" << endl;
	//PrintMes(C, 28, 28);
	//cout << "D:" << endl;
	//PrintMes(D, 28, 28);
	for (int i = 0; i < times; i++) {
		//移位
		if (LSi[i] == 1) {
			BITE A = C[0];
			BITE B = D[0];
			for (int j = 0; j < 27; j++) {
				C[j] = C[j + 1];
				D[j] = D[j + 1];
			}
			C[27] = A;
			D[27] = B;
		}
		else {
			BITE A1 = C[0];
			BITE A2 = C[1];
			BITE B1 = D[0];
			BITE B2 = D[1];
			for (int j = 1; j < 27; j++) {
				C[j - 1] = C[j + 1];
				D[j - 1] = D[j + 1];
			}
			C[26] = A1;
			C[27] = A2;
			D[26] = B1;
			D[27] = B2;
		}
		//cout << "C"<<i<<":" << endl;
		//PrintMes(C, 28, 28);
		//cout << "D" <<i << ":" << endl;
		//PrintMes(D, 28, 28);
		//PC2选择置换同时存储到子秘钥数组里面
	//  cout << "PC2置换后:" << endl;取消PC2置换
		for (int j = 0; j < 56; j++) {
			Sub_Key[i][j] = temp[j];
			//cout << Sub_Key[i][j] << " ";
			//if ((j + 1) % 6 == 0) { cout << endl; }
		}
		//  cout << endl;
	}
}
//S0盒压缩
void S0_BOX(BITE* Data_Out, BITE* Data_In) {
	int i = 0;
	int row = 0;
	int line = 0;
	int temp = 0;
	for (; i < 8; i++) {
		row = Data_In[i * 6] * 2+ Data_In[i * 6 + 6];
		line = Data_In[i * 6 + 2] * 8 + Data_In[i * 6 + 3] * 4 + Data_In[i * 6 + 4] * 2 + Data_In[i * 6 + 5];
		temp = S0_Box[row][line];
		for (int j = 0; j < 6; j++) {
			Data_Out[(i + 1) * 6 - j - 1] = (temp >> j) & 1;
		}
	}
}
//S盒压缩 6bit-->4bit
void S_BOX(BITE* Data_Out, BITE* Data_In) {
	int i = 0;
	int row = 0;
	int line = 0;
	int temp = 0;
	for (; i < 8; i++) {
		row = Data_In[i * 6] * 2 + Data_In[i * 6 + 5];
		line = Data_In[i * 6 + 1] * 8 + Data_In[i * 6 + 2] * 4 + Data_In[i * 6 + 3] * 2 + Data_In[i * 6 + 4];
		temp = S_Box[i][row][line];
		for (int j = 0; j < 4; j++) {
			Data_Out[(i + 1) * 4 - j - 1] = (temp >> j) & 1;
		}
	}
}
void P_change(BITE * Data_Out, BITE * Data_In) {
	int i = 0;
	for (; i < 32; i++) {
		Data_Out[i] = Data_In[P_Change[i] - 1];
	}
}
void IPR_change(BITE * Data_Out, BITE * Data_In) {
	int i = 0;
	for (; i < 64; i++) {
		Data_Out[i] = Data_In[IPR_Change[i] - 1];
	}
}
void PC2_change(BITE* Data_Out, BITE* Data_In) {
	for (int i = 0; i < 48; i++) {
		Data_Out[i] = Data_In[PC2_Change[i] - 1];
	}

}
BITE* DES_ENC(BITE * Data_In, int Times) {
	//加密第一步IP选择置换
	IP_change(IP_Result, Data_In);
	//加密第二部分成L,R部分
	BITE* TEMPLR = new BITE[64];
	BiteCopy(TEMPLR, IP_Result, 64);
	BITE* L = &TEMPLR[0];
	BITE* R = &TEMPLR[32];
	//16轮函数
	int i = 0;
	for (; i < Times; i++) {
		//E扩展
		Expand(EX_Result[i], R);
		//子秘钥异或
		XOR(KeyXOR_Result[i], EX_Result[i], Sub_Key[i], 56);
		pC2置换
		BITE* TEMP = new BITE[48];
		//PC2_change(TEMP, KeyXOR_Result[i]);
		//S0压缩
		S0_BOX(TEMP, KeyXOR_Result[i]);
		//S盒压缩
		S_BOX(S_Result[i], TEMP);
		//P置换
		P_change(P_Result[i], S_Result[i]);
		//与L异或
		XOR(LXOR_Result[i], L, P_Result[i], 32);
		//R变成L,与L异或的结果变成R
		BiteCopy(L, R, 32);
		BiteCopy(R, LXOR_Result[i], 32);
		for (int j = 0; j < 64; j++) {
			TimesTEMP[i][j] = TEMPLR[j];
		}
	}
	//IP逆置换
	IPR_change(IPR_Result, TEMPLR);
	return IPR_Result;
}
BITE* DES_DEC(BITE * Data_In, int Times) {
	//解密是加密的逆
	//IP置换
	IP_change(RIP_Result, Data_In);
	//分组
	BITE* RTEMP = new BITE[64];
	BiteCopy(RTEMP, RIP_Result, 64);
	BITE* L = &RTEMP[0];
	BITE* R = &RTEMP[32];
	//16轮函数
	int i = Times - 1;
	for (; i >= 0; i--) {
		//E扩展
		Expand(REX_Result[15 - i], L);
		//异或
		XOR(RKeyXOR_Result[15 - i], REX_Result[15 - i], Sub_Key[i], 48);
		//S盒
		S_BOX(RS_Result[15 - i], RKeyXOR_Result[15 - i]);
		//P置换
		P_change(RP_Result[15 - i], RS_Result[15 - i]);
		//与R异或
		XOR(RLXOR_Result[15 - i], RP_Result[15 - i], R, 32);
		//交换L部分给到R,异或的结果给到L
		BiteCopy(R, L, 32);
		BiteCopy(L, RLXOR_Result[15 - i], 32);
	}
	//IP逆置换
	IPR_change(RIPR_Result, RTEMP);
	return RIPR_Result;
}
void PrintCheckData() {
	cout << "检查问题:" << endl;
	cout << "IP置换:" << endl;
	PrintMes(IP_Result, 64, 8);
	cout << "R0:" << endl;
	PrintMes(&IP_Result[32], 32, 4);
	cout << "第一轮E扩展:" << endl;
	PrintMes(EX_Result[0], 48, 6);
	cout << "第一轮秘钥:" << endl;
	PrintMes(Sub_Key[0], 48, 6);
	cout << "第一轮K异或" << endl;
	PrintMes(KeyXOR_Result[0], 48, 6);
	cout << "第一轮S盒:" << endl;
	PrintMes(S_Result[0], 32, 4);
	cout << "第一轮P置换:" << endl;
	PrintMes(P_Result[0], 32, 4);
	cout << "L部分:" << endl;
	PrintMes(&IP_Result[0], 32, 4);
	cout << "第一轮L异或:" << endl;
	PrintMes(LXOR_Result[0], 32, 4);
	cout << "第一轮之后的TEMP:" << endl;
	PrintMes(TimesTEMP[0], 64, 8);
	cout << "IPR:" << endl;
	PrintMes(IPR_Result, 64, 8);

}
int main() {
	//
	clock_t  Begin, End;
	double duration;
	int i = 0;
	int times = 1;
	BITE Mes[64] = { 1,0,0,1,0,0,1,0,0,0,0,1,1,0,0,1,0,1,0,1,1,0,1,1,0,0,0,1,1,0,1,1,0,0,0,1,1,0,1,1,1,1,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,0,0,1,1,1,0,1};
	BITE KEY[64] = { 1,0,0,0,1,1,0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,1,0,0,1,1,0,0,1,1,0,1,0,0,0,0,1,1,0,1,0,1,0,0,1,1,0,1,1,0,0,0,1,1,0,1,1,1,0,0,1,1,1,0};
	BITE* Mes_Code = new BITE[64];
	BITE* Mes_UnCode = new BITE[64];
	//cout << "输入64位二进制密文" << endl;
	//for (; i < 64; i++)
	//{
	//	cin >> Mes[i];
	//}
	//cout << "输入64位二进制KEY" << endl;
	//for (i = 0; i < 64; i++)
	//{
	//	cin >> KEY[i];
	//}
	for (times = 1; times < 37; times++) {
		Start_Sub_EX_Key_S_P_LXO_Times_IPR(times);
		SubKey(KEY, times);
		//cout << "明文8位一组表示" << endl;
		//PrintMes(Mes, 64, 8);
		//Begin = clock();//开始计时
		Mes_Code = DES_ENC(Mes, times);
		//End = clock();//结束计时
		//duration = double(End - Begin) / CLOCKS_PER_SEC;
		//cout << "tick=" << double(End - Begin) << endl; //运行函数所打的点数
		//PrintCheckData();
		//cout << "密文:" << endl;
		//PrintMes(Mes_Code, 64, 8);
		cout << times << "轮相似度:[越大相似度越低]" << endl;
		cout << Distance_H(Mes, Mes_Code, 64) << endl;
	}
	//cout << "解密后的明文:" << endl;
	//Mes_UnCode=DES_DEC(Mes_Code);
	//PrintMes(Mes_UnCode, 64, 8);
	return 0;
}

Tables.h文件

#ifndef TABLES_H
#define TABLES_H
typedef bool BITE;
int IP_Change[64] = {
58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
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
};
int E_Change[56] = {
32,2,1,2,3,4,5,
4,7,5,6,7,8,9,
8,10,9,10,11,12,13,
12,15,13,14,15,16,17,
16,18,17,18,19,20,21,
20,23,21,22,23,24,25,
24,26,25,26,27,28,29,
28,31,29,30,31,32,1
};
int PC1_Change[56] = {
57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
63,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4
};
int PC2_Change[48] = {
14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,34,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32
};
int LSi[48] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
int S0_Box[4][16] = {
{8,0,9,3,56,16,41,19,12,15,4,7,48,1,32,35,},
{26,18,24,50,62,22,44,54,28,29,20,55,52,5,36,39,},
{2,6,11,15,51,23,33,21,10,27,14,31,49,17,37,53,},
{34,38,42,46,58,30,40,60,43,59,47,63,57,25,45,61} };
int S_Box[8][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}
}
};
int P_Change[32] = {
16,7,20,21,
29,12,28,17,
1,15,23,26,
5,18,31,10,
2,8,24,14,
32,27,3,9,
19,13,30,6,
22,11,4,25
};
int IPR_Change[64] = {
40,8,48,16,56,24,64,32,
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
};
//float Power[64] = {
//2,1.8,1.6,1.4,1.2,1.1,1.0,1.0,
//2,1.8,1.6,1.4,1.2,1.1,1.0,1.0,
//2,1.8,1.6,1.4,1.2,1.1,1.0,1.0,
//2,1.8,1.6,1.4,1.2,1.1,1.0,1.0,
//2,1.8,1.6,1.4,1.2,1.1,1.0,1.0,
//2,1.8,1.6,1.4,1.2,1.1,1.0,1.0,
//2,1.8,1.6,1.4,1.2,1.1,1.0,1.0,
//2,1.8,1.6,1.4,1.2,1.1,1.0,1.0,
//};
BITE** Sub_Key;
BITE* IP_Result = new BITE[64];
BITE** EX_Result;
BITE** KeyXOR_Result;
BITE** S0_Result;
BITE** S_Result;
BITE** P_Result;
BITE** LXOR_Result;
BITE** TimesTEMP;
BITE* IPR_Result = new BITE[64];

//反向轮的数据
BITE* RIP_Result = new BITE[64];
BITE** REX_Result;
BITE** RKeyXOR_Result;
BITE** RS_Result;
BITE** RP_Result;
BITE** RLXOR_Result;
BITE* RIPR_Result = new BITE[64];
#endif

3.N-DES
CPP文件

#include<iostream>
#include<ctime>
#include <windows.h>
#include"Tables.h"
using namespace std;
//缓存函数
void Start_Sub_EX_Key_S_P_LXO_Times_IPR(int Times,int K) {
	Sub_Key = new BITE ** [K];
	EX_Result = new BITE ** [K];
	KeyXOR_Result = new BITE * [Times];
	RKeyXOR_Result = new BITE * [Times];
	for (int j = 0; j < K; j++) {
		EX_Result[j] = new BITE * [Times];
		Sub_Key[j] = new BITE * [Times];
		for (int i = 0; i < Times; i++)
		{
			EX_Result[j][i] = new BITE  [48];
			Sub_Key[j][i] = new BITE[48];
		}
	}
	S_Result = new BITE * [Times];
	P_Result = new BITE * [Times];
	LXOR_Result = new BITE * [Times];
	REX_Result = new BITE * [Times];
	RS_Result = new BITE * [Times];
	RP_Result = new BITE * [Times];
	RLXOR_Result = new BITE * [Times];
	TimesTEMP = new BITE * [Times];
	for (int i = 0; i < Times; i++)
	{
		KeyXOR_Result[i] = new BITE[48];
		RKeyXOR_Result[i] = new BITE[48];
		S_Result[i] = new BITE[32];
		P_Result[i] = new BITE[32];
		LXOR_Result[i] = new BITE[32];
		REX_Result[i] = new BITE[48];
		RS_Result[i] = new BITE[32];
		RP_Result[i] = new BITE[32];
		RLXOR_Result[i] = new BITE[32];
		TimesTEMP[i] = new BITE[64];
	}
}
int Distance_H(BITE* A, BITE* B, int Data_Len) {
	int i = 0;
	int dis = 0;
	for (; i < Data_Len; i++) {
		if (A[i] ^ B[i]) { dis++; }
	}
	return dis;
}
void BiteCopy(BITE* NeedCopy, BITE* Data, int DataLen)
{
	int i = 0;
	for (; i < DataLen; i++) {
		NeedCopy[i] = Data[i];
	}
}
//
void PrintMes(BITE* Mes, int DataLen, int Line) {
	int i = 0;
	for (; i < DataLen; i++) {
		cout << Mes[i] << " ";
		if ((i + 1) % Line == 0) { cout << endl; }
	}
	cout << endl;

}
//异或
BITE * XOR(BITE* Data_InA, BITE* Data_InB, int DataLen) {
	int i = 0;
	BITE* RE = new BITE[48];
	for (; i < DataLen; i++) {
		RE[i] = Data_InA[i] ^ Data_InB[i];
	}
	return RE;
}

//IP置换目的是初步打乱明文,初步混淆,此时得出的数据,每一个都跟明文紧密联系
void IP_change(BITE* Data_Out, BITE* Mes_In)
{
	int i = 0;
	//ip置换就是将二进制明文按照IP置换表的顺序,重排列
	for (; i < 64; i++) {
		Data_Out[i] = Mes_In[IP_Change[i] - 1];
	}
}
//E扩展函数
BITE * Expand(BITE* Mes_in)
{
	BITE* ER = new BITE[48];
	int i = 0;
	for (; i < 48; i++)
	{
		ER[i] = Mes_in[E_Change[i] - 1];
	}
	return ER;
}
//子秘钥,只有第一轮需要PC1选择置换,后面需要移位,PC2选择置换
void SubKey(BITE* Data_In, int K,int times) {
	//PCI去除奇偶校验同时选择置换
	//cout << "秘钥8位一行:" << endl;
	//PrintMes(Data_In,64,8);
	BITE* temp = new BITE[56];
	for (int i = 0; i < 56; i++) {
		temp[i] = Data_In[PC1_Change[i] - 1];
	}
	//cout << "PCI置换选择后的结果:【7位一排】:" << endl;
	//PrintMes(temp, 56, 7);
	BITE* C = &temp[0];
	BITE* D = &temp[28];
	//cout << "移位前:" << endl;
	//cout << "C:" << endl;
	//PrintMes(C, 28, 28);
	//cout << "D:" << endl;
	//PrintMes(D, 28, 28);
	for (int i = 0; i < times; i++) {
		//移位
		if (LSi[i] == 1) {
			BITE A = C[0];
			BITE B = D[0];
			for (int j = 0; j < 27; j++) {
				C[j] = C[j + 1];
				D[j] = D[j + 1];
			}
			C[27] = A;
			D[27] = B;
		}
		else {
			BITE A1 = C[0];
			BITE A2 = C[1];
			BITE B1 = D[0];
			BITE B2 = D[1];
			for (int j = 1; j < 27; j++) {
				C[j - 1] = C[j + 1];
				D[j - 1] = D[j + 1];
			}
			C[26] = A1;
			C[27] = A2;
			D[26] = B1;
			D[27] = B2;
		}
		//cout << "C"<<i<<":" << endl;
		//PrintMes(C, 28, 28);
		//cout << "D" <<i << ":" << endl;
		//PrintMes(D, 28, 28);
		//PC2选择置换同时存储到子秘钥数组里面
	//	cout << "PC2置换后:" << endl;
		for (int j = 0; j < 48; j++) {
			Sub_Key[K] [i] [j] = temp[PC2_Change[j] - 1];
			//cout << Sub_Key[i][j] << " ";
			//if ((j + 1) % 6 == 0) { cout << endl; }
		}
		//	cout << endl;
	}
}
//S盒压缩 6bit-->4bit
BITE* S_BOX( BITE* Data_In) {
	int i = 0;
	int row = 0;
	int line = 0;
	int temp = 0;
	BITE* RE = new BITE[32];
	for (; i < 8; i++) {
		row = Data_In[i * 6] * 2 + Data_In[i * 6 + 5];
		line = Data_In[i * 6 + 1] * 8 + Data_In[i * 6 + 2] * 4 + Data_In[i * 6 + 3] * 2 + Data_In[i * 6 + 4];
		temp = S_Box[i][row][line];
		for (int j = 0; j < 4; j++) {
			RE[(i + 1) * 4 - j - 1] = (temp >> j) & 1;
		}
	}
	return RE;
}
BITE* P_change( BITE * Data_In) {
	int i = 0;
	BITE* RE = new BITE[32];
	for (; i < 32; i++) {
		RE[i] = Data_In[P_Change[i] - 1];
	}
	return RE;
}
void IPR_change(BITE * Data_Out, BITE * Data_In) {
	int i = 0;
	for (; i < 64; i++) {
		Data_Out[i] = Data_In[IPR_Change[i] - 1];
	}
}
BITE* DES_ENC(BITE * Data_In, int Times,int K) {
	//加密第一步IP选择置换
	IP_change(IP_Result, Data_In);
	//加密第二部分成L,R部分
	BITE* TEMPLR = new BITE[64];
	BiteCopy(TEMPLR, IP_Result, 64);
	BITE* L = &TEMPLR[0];
	BITE* R = &TEMPLR[32];
	//16轮函数
	int i = 0;
	for (; i < Times; i++) {

		//E扩展
		BITE *TEMPE =Expand(R);
		//子秘钥异或
		BITE * TEMPX = XOR(TEMPE, Sub_Key[K][i], 48);
		//S盒压缩
		BITE* TEMPS = S_BOX(TEMPX);
		//P置换
		BITE* TEMPP = P_change( TEMPS);
		//与L异或
		BITE* TEMPXL = XOR(L, TEMPP, 32);
		//R变成L,与L异或的结果变成R
		BiteCopy(L, R, 32);
		BiteCopy(R, TEMPXL, 32);
		for (int j = 0; j < 64; j++) {
			TimesTEMP[i][j] = TEMPLR[j];
		}
	}
	//IP逆置换
	IPR_change(IPR_Result, TEMPLR);
	return IPR_Result;
}
BITE* DES_DEC(BITE * Data_In, int Times,int K) {
	//解密是加密的逆
	//IP置换
	IP_change(RIP_Result, Data_In);
	//分组
	BITE* RTEMP = new BITE[64];
	BiteCopy(RTEMP, RIP_Result, 64);
	BITE* L = &RTEMP[0];
	BITE* R = &RTEMP[32];
	//16轮函数
	int i = Times - 1;
	for (; i >= 0; i--) {
		//E扩展
		BITE*TEMPE  = Expand(L);
		//异或
		BITE* TEMPX = XOR(TEMPE, Sub_Key[K][i], 48);
		//S盒
		BITE* TEMPS = S_BOX(TEMPX);
		//P置换
		BITE* TEMPP = P_change(TEMPS);
		//与R异或
		BITE* TEMPXR = XOR(TEMPP, R, 32);
		//交换L部分给到R,异或的结果给到L
		BiteCopy(R, L, 32);
		BiteCopy(L, TEMPXR, 32);
	}
	//IP逆置换
	IPR_change(RIPR_Result, RTEMP);
	return RIPR_Result;
}

//Mode表示是3des模式或者普通des模式 Mode =3 是3des
BITE* NDES_ENC(BITE* Data_In,int Mode,int Times) {
	Start_Sub_EX_Key_S_P_LXO_Times_IPR(Times,Mode);
		BITE** KEY = new BITE * [Mode];
		for (int x = 0; x < Mode; x++) {
			KEY[x] = new BITE[64];
		}
		//for (int j = 0; j < Mode; j++) {
		//	cout << "输入第"<<j<<"轮64位二进制KEY" << endl;
		//	for (int i = 0; i < 64; i++)
		//	{
		//		cin >> KEY[j][i];
		//	}
		//}
		//for (int i = 0; i < Mode; i++) {
		//	cout << "第"<<i<<"轮秘钥---" << endl;
		//	SubKey(KEY[i], i, Times);
		//}
		//NDES
		SubKey(KEY1, 0, Times);
		SubKey(KEY2, 1, Times);
		SubKey(KEY3, 2, Times);
		SubKey(KEY4, 3, Times);
		SubKey(KEY5, 4, Times);
		BITE* result = new BITE[64];
		BiteCopy(result, Data_In, 64);
		for (int i = 0; i < Mode;) {
			BITE* TEMP = new BITE[64];
			TEMP = DES_ENC(result, Times, i);
			result = DES_DEC(TEMP, Times, i);
			i += 2;
			if (i == Mode - 1) {
				result = DES_ENC(result, Times, i);
				i++;
			}
		}
		return result;

}
//解密
BITE* NDES_DEC(BITE* Data_In, int Mode, int Times) {
	BITE* result = new BITE[64];
	BiteCopy(result, Data_In, 64);
	for (int i = Mode-1; i >=0;) {
		BITE* TEMP = new BITE[64];
		TEMP = DES_DEC(result, Times, i);
		result = DES_ENC(TEMP, Times, i);
		i -= 2;
		if (i == 0) {
			result = DES_DEC(result, Times, i);
			i--;
		}
	}
	return result;

}
int main() {
	//
	clock_t  Begin, End;
	double duration;
	int i = 0;
	int times = 16;
	BITE Mes[64]={ 1,0,0,1,0,0,1,0,0,0,0,1,1,0,0,1,0,1,0,1,1,0,1,1,0,0,0,1,1,0,1,1,0,0,0,1,1,0,1,1,1,1,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,0,0,1,1,1,0,1 };;
	BITE* Mes_Code = new BITE[64];
	BITE* Mes_UnCode = new BITE[64];

	//cout << "输入64位二进制明文" << endl;
	//for (; i < 64; i++)
	//{
	//	cin >> Mes[i];
	//}

	//cout << "明文8位一组表示" << endl;
	//PrintMes(Mes, 64, 8);
	for (times = 1; times <= 36; times++) {
		Mes_Code = NDES_ENC(Mes, 5, times);
		//cout << "密文:" << endl;
		//PrintMes(Mes_Code, 64, 8);
		cout << times << "轮相似度:[越大相似度越低]" << endl;
		cout << Distance_H(Mes, Mes_Code, 64) << endl;
	}
	/*cout << "解密后的明文:" << endl;*/

	//Mes_UnCode= NDES_DEC(Mes_Code,3,times);
	//PrintMes(Mes_UnCode, 64, 8);
	return 0;
}

Tables.h文件

#ifndef TABLES_H
#define TABLES_H
typedef bool BITE;
int IP_Change[64] = {
58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
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
};
int E_Change[48] = {
32,1,2,3,4,5,
4,5,6,7,8,9,
8,9,10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32,1
};
int PC1_Change[56] = {
57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
63,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4
};
int PC2_Change[48] = {
14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,34,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32
};
int LSi[48] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
int S_Box[8][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}
}
};
int P_Change[32] = {
16,7,20,21,
29,12,28,17,
1,15,23,26,
5,18,31,10,
2,8,24,14,
32,27,3,9,
19,13,30,6,
22,11,4,25
};
int IPR_Change[64] = {
40,8,48,16,56,24,64,32,
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
};
BITE KEY1[64] = { 1,0,0,0,1,1,0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,1,0,0,1,1,0,0,1,1,0,1,0,0,0,0,1,1,0,1,0,1,0,0,1,1,0,1,1,0,0,0,1,1,0,1,1,1,0,0,1,1,1,0};
BITE KEY2[64] = { 1,0,0,0,1,1,0,0,0,1,0,0,1,1,1,0,0,1,0,0,1,1,0,1,0,1,0,0,1,1,0,0,0,0,0,0,1,1,1,0,0,1,0,0,1,1,0,0,1,1,0,0,1,1,1,0,0,1,0,0,1,1,0,0};
BITE KEY3[64] = { 1,0,0,0,1,1,0,0,0,1,0,0,1,1,0,1,0,1,0,0,1,1,0,0,1,0,0,0,1,1,0,1,1,1,0,0,1,1,0,0,1,0,0,0,1,1,0,0,1,1,0,0,1,1,1,0,0,1,0,0,1,1,1,0};
BITE KEY4[64] = { 1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,1,0,1,0,0,1,1,0,1,1,0,0,0,1,1,0,1,1,1,0,0,1,1,1,0,0,0,0,0,1,1,1,0,0,1,0,0,1,1,0,0,0,1,0,0,1,1,0,0};
BITE KEY5[64] = { 1,0,0,0,1,1,0,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,0,0,1,1,1,0,0,1,0,0,1,1,1,0,0,0,0,0,1,1,0,1,1,1,0,0,1,1,0,1,0,0,0,0,1,1,1,0};
BITE*** Sub_Key;
BITE* IP_Result = new BITE[64];
BITE*** EX_Result;
BITE** KeyXOR_Result;
BITE** S_Result;
BITE** P_Result;
BITE** LXOR_Result;
BITE** TimesTEMP;
BITE* IPR_Result = new BITE[64];

//反向轮的数据
BITE* RIP_Result = new BITE[64];
BITE** REX_Result;
BITE** RKeyXOR_Result;
BITE** RS_Result;
BITE** RP_Result;
BITE** RLXOR_Result;
BITE* RIPR_Result = new BITE[64];
#endif
#pragma once

总结:

  1. 以汉明距表示算法加密的好坏时,一个好的加密应该会让接近一般的明文的位数发生改变。也就是汉明距越接近32往往越好。
  2. DES明文与秘钥相同时,加密得更加稳定
  3. E扩展成56位后采用PC2置换适当增强了加密的强度,但不及通过一个6bitS0盒的加密程度好。
  4. 3DES加密性能明显优于DES,但不及5DES加密的性能。
    在这里插入图片描述在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阁下莫非东西

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值