3DES,C++實現(xiàn)-創(chuàng)新互聯(lián)

首先將3DES原理簡單介紹
1.3DES是一種對稱加密,加解密使用的密鑰相同。
2.屬于分組加密算法,每一組明文大小64比特
3.密鑰長度為64比特,但實際作用的只有56比特
4.整體結(jié)構(gòu)由三個DES結(jié)構(gòu)拼接而來:加密-解密-加密(可反過來),原因是為了向下兼容,當前兩個使用相同的密鑰時就相當于只進行了一次DES

創(chuàng)新互聯(lián)公司從2013年開始,先為福安等服務(wù)建站,福安等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為福安企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

c++代碼如下
首先進行頭文件的導(dǎo)入

#include#include#include "memory.h"
#include#include#includeusing namespace std;

接著進行定義一些需要的常量,這些常量的取值來源于DES標準,不是自己設(shè)置的,只需要拿下來用即可

//初始置換表IP
int IP[64] = {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
};

//逆初始置換表IIP
int IIP[64] = {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
};

//擴充置換表E
int E[48] = {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
};

//P盒
int P[32] = {15,6,19,20,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
};

//S盒
int S[8][4][16] =//S1
{{{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}
	},
	//S2
	{{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}
	},
	//S3
	{{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}
	},
	//S4
	{{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}
	},
	//S5
	{{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}
	},
	//S6
	{{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}
	},
	//S7
	{{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}
	},
	//S8
	{{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
int PC_1[56] = {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
};

//置換選擇2
int PC_2[48] = {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,46,43,48,38,55,
				 33,52,45,41,49,35,28,31
};

//對左移次數(shù)的規(guī)定
int left_bits[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1 };

加密單個分組,按照Feistel密碼結(jié)構(gòu)進行即可

void  encryptBlock(char plainBlock[8], char subKeys[16][48], char cipherBlock[8])
{char plainBits[64];
	char copyRight[48];


	Char8ToBit64(plainBlock, plainBits);
	//初始置換(IP置換)
	ip(plainBits);
	//16輪迭代
	for (int i = 0; i< 16; i++)
	{//保存一份R
		memcpy(copyRight, plainBits + 32, 32);
		//將右半部分進行擴展置換,從32位擴展到48位
		extend_32to48(copyRight);
		//將右半部分與子密鑰進行異或操作
		DES_XOR(copyRight, subKeys[i], 48);
		//S盒壓縮
		s_compression(copyRight);
		//P擾亂
		p_disturb(copyRight);
		//將明文左半部分與右半部分進行異或
		DES_XOR(plainBits, copyRight, 32);
		//左右互換
		des_swap(plainBits, plainBits + 32);

	}
	//左右交換
	des_swap(plainBits, plainBits + 32);
	//逆初始置換
	iip(plainBits);
	Bit64ToChar8(plainBits, cipherBlock);

}

加密整個明文,注意要對明文按照大長度(64比特)進行分組,這里就會產(chǎn)生一個問題,即明文長度不為64的整數(shù)倍,這時候就需要規(guī)定一個填充標準,在加解密時采用一致的標準即可
這里采用的標準是在末尾補0,最后一位設(shè)置為校驗位記錄補了多少,沒補即為0

//加密文件

void Encrypt_3DES(const char* plainFile, const  char* key_1, const char* key_2, const char* key_3, const char* cipherFile)
{FILE* plain = NULL, * cipher = NULL;
	int num;
	long len;
	char plainBlock[8], temp1[8], temp2[8], result[8];

	char subKeys1[16][48], subKeys2[16][48], subKeys3[16][48];
	errno_t err = fopen_s(&cipher, cipherFile, "wb");
	errno_t err1 = fopen_s(&plain, plainFile, "rb");

	//設(shè)置密鑰
	//生成子密鑰
	Subkey_generation(key_1, subKeys1);
	Subkey_generation(key_2, subKeys2);
	Subkey_generation(key_3, subKeys3);



	//如果文件非空,循環(huán)分組處理
	while (!feof(plain))
	{//每次讀64bite,8字節(jié)
		if ((num = fread(plainBlock, sizeof(char), 8, plain)) == 8)
		{	encryptBlock(plainBlock, subKeys1, temp1);
			decryptBlock(temp1, subKeys2, temp2);
			encryptBlock(temp2, subKeys3, result);

			fwrite(result, sizeof(char), 8, cipher);
		}
	}
	//最后一組額外判斷
	if (num)
	{//對于不足一組的位置進行填充\0
		memset(plainBlock + num, '\0', 7 - num);
		//最后一個字符作為校驗位,保存包括最后一個字符在內(nèi)的所填充的字符數(shù)量
		plainBlock[7] = 8 - num;
		encryptBlock(plainBlock, subKeys1, temp1);
		decryptBlock(temp1, subKeys2, temp2);
		encryptBlock(temp2, subKeys3, result);

		fwrite(result, sizeof(char), 8, cipher);
	}
	fclose(plain);
	fclose(cipher);

}

完整代碼如下

#include#include#include "memory.h"
#include#include#includeusing namespace std;

//初始置換表IP
int IP[64] = {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
};

//逆初始置換表IIP
int IIP[64] = {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
};

//擴充置換表E
int E[48] = {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
};

//P盒
int P[32] = {15,6,19,20,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
};

//S盒
int S[8][4][16] =//S1
{{{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}
	},
	//S2
	{{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}
	},
	//S3
	{{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}
	},
	//S4
	{{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}
	},
	//S5
	{{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}
	},
	//S6
	{{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}
	},
	//S7
	{{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}
	},
	//S8
	{{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
int PC_1[56] = {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
};

//置換選擇2
int PC_2[48] = {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,46,43,48,38,55,
				 33,52,45,41,49,35,28,31
};

//對左移次數(shù)的規(guī)定
int left_bits[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1 };

//字節(jié)轉(zhuǎn)換成二進制
void ByteToBit(const char ch, char bit[8])
{int cnt;
	for (cnt = 0; cnt< 8; cnt++)
	{bit[cnt] = (ch >>cnt) & 1;
	}

}

//二進制轉(zhuǎn)換成字節(jié)
void BitToByte(const char bit[8], char* ch)
{int cnt;
	for (cnt = 0; cnt< 8; cnt++)
	{*ch |= *(bit + cnt)<< cnt;
	}

}

//將長度為8的字符串轉(zhuǎn)為二進制位串
void Char8ToBit64(const char ch[8], char bit[64])
{int cnt;
	for (cnt = 0; cnt< 8; cnt++)
	{ByteToBit(*(ch + cnt), bit + (cnt<< 3));
	}

}

//將二進制位串轉(zhuǎn)為長度為8的字符串
void Bit64ToChar8(char bit[64], char ch[8])
{int cnt;
	memset(ch, 0, 8);
	for (cnt = 0; cnt< 8; cnt++)
	{BitToByte(bit + (cnt<< 3), ch + cnt);
	}

}


//密鑰置換pc1
void transform1(char key[64], char tempbts[56])
{int cnt;
	for (cnt = 0; cnt< 56; cnt++)
	{tempbts[cnt] = key[PC_1[cnt]];
	}

}

//密鑰置換pc2
void transform2(char key[56], char tempbts[48])
{int cnt;
	for (cnt = 0; cnt< 48; cnt++)
	{tempbts[cnt] = key[PC_2[cnt]];
	}

}

//循環(huán)左移
void left_shift(char data[56], int time)
{char temp[56];

	//保存將要循環(huán)移動到右邊的位
	memcpy(temp, data, time);
	memcpy(temp + time, data + 28, time);

	//前28位移動
	memcpy(data, data + time, 28 - time);
	memcpy(data + 28 - time, temp, time);

	//后28位移動
	memcpy(data + 28, data + 28 + time, 28 - time);
	memcpy(data + 56 - time, temp + time, time);


}

//IP置換
void ip(char data[64])
{char temp[64];
	for (int cnt = 0; cnt< 64; cnt++)
	{temp[cnt] = data[IP[cnt]];
	}
	memcpy(data, temp, 64);

}

//IP逆置換
void iip(char data[64])
{char temp[64];
	for (int cnt = 0; cnt< 64; cnt++)
	{temp[cnt] = data[IIP[cnt]];
	}
	memcpy(data, temp, 64);

}

//擴展置換
void extend_32to48(char data[48])
{char temp[48];
	for (int cnt = 0; cnt< 48; cnt++)
	{temp[cnt] = data[E[cnt]];
	}
	memcpy(data, temp, 48);

}

//P置換
void p_disturb(char data[32])
{char temp[32];
	for (int cnt = 0; cnt< 32; cnt++)
	{temp[cnt] = data[P[cnt]];
	}
	memcpy(data, temp, 32);

}

//異或
void DES_XOR(char R[48], char L[48], int count)
{for (int cnt = 0; cnt< count; cnt++)
	{R[cnt] ^= L[cnt];
	}

}

//S盒置換
void s_compression(char data[48])
{int cnt, line, row, output, cur1, cur2;
	for (cnt = 0; cnt< 8; cnt++)
	{cur1 = cnt * 6;
		cur2 = cnt<< 2;


		line = (data[cur1]<< 1) + data[cur1 + 5];
		row = (data[cur1 + 1]<< 3) + (data[cur1 + 2]<< 2)
			+ (data[cur1 + 3]<< 1) + data[cur1 + 4];
		output = S[cnt][line][row];


		data[cur2] = (output & 0X08) >>3;
		data[cur2 + 1] = (output & 0X04) >>2;
		data[cur2 + 2] = (output & 0X02) >>1;
		data[cur2 + 3] = output & 0x01;
	}

}

//交換
void des_swap(char left[32], char right[32])
{char temp[32];
	memcpy(temp, left, 32);
	memcpy(left, right, 32);
	memcpy(right, temp, 32);

}

//由二進制生成子密鑰
void bits_make_key(char key[64], char subKeys[16][48])
{char temp[56];
	transform1(key, temp);//置換1
	for (int i = 0; i< 16; i++)  //生成16個子密鑰
	{left_shift(temp, left_bits[i]);//循環(huán)左移
		transform2(temp, subKeys[i]);//置換2,產(chǎn)生子密鑰
	}

}
//加密單個分組
void  encryptBlock(char plainBlock[8], char subKeys[16][48], char cipherBlock[8])
{char plainBits[64];
	char copyRight[48];


	Char8ToBit64(plainBlock, plainBits);
	//初始置換(IP置換)
	ip(plainBits);
	//16輪迭代
	for (int i = 0; i< 16; i++)
	{//保存一份R
		memcpy(copyRight, plainBits + 32, 32);
		//將右半部分進行擴展置換,從32位擴展到48位
		extend_32to48(copyRight);
		//將右半部分與子密鑰進行異或操作
		DES_XOR(copyRight, subKeys[i], 48);
		//S盒壓縮
		s_compression(copyRight);
		//P擾亂
		p_disturb(copyRight);
		//將明文左半部分與右半部分進行異或
		DES_XOR(plainBits, copyRight, 32);
		//左右互換
		des_swap(plainBits, plainBits + 32);

	}
	//左右交換
	des_swap(plainBits, plainBits + 32);
	//逆初始置換
	iip(plainBits);
	Bit64ToChar8(plainBits, cipherBlock);

}

//解密單個分組
void decryptBlock(char cipherBlock[8], char subKeys[16][48], char plainBlock[8])
{char cipherBits[64];
	char copyRight[48];


	Char8ToBit64(cipherBlock, cipherBits);
	//初始置換(IP置換)
	ip(cipherBits);
	//16輪迭代
	for (int i = 15; i >= 0; i--)
	{//保存一份R
		memcpy(copyRight, cipherBits + 32, 32);
		//將右半部分進行擴展置換,從32位擴展到48位
		extend_32to48(copyRight);
		//將右半部分與子密鑰進行異或操作
		DES_XOR(copyRight, subKeys[i], 48);
		//S盒壓縮
		s_compression(copyRight);
		//P擾亂
		p_disturb(copyRight);
		//將明文左半部分與右半部分進行異或
		DES_XOR(cipherBits, copyRight, 32);
		//左右互換
		des_swap(cipherBits, cipherBits + 32);

	}
	//左右交換
	des_swap(cipherBits, cipherBits + 32);
	//逆初始置換
	iip(cipherBits);
	Bit64ToChar8(cipherBits, plainBlock);

}

//生成子密鑰
void Subkey_generation(const char key[8], char subKeys[16][48])
{char Key[64];
	Char8ToBit64(key, Key);
	bits_make_key(Key, subKeys);
}

//加密文件

void Encrypt_3DES(const char* plainFile, const  char* key_1, const char* key_2, const char* key_3, const char* cipherFile)
{FILE* plain = NULL, * cipher = NULL;
	int num;
	long len;
	char plainBlock[8], temp1[8], temp2[8], result[8];

	char subKeys1[16][48], subKeys2[16][48], subKeys3[16][48];
	errno_t err = fopen_s(&cipher, cipherFile, "wb");
	errno_t err1 = fopen_s(&plain, plainFile, "rb");

	//設(shè)置密鑰
	//生成子密鑰
	Subkey_generation(key_1, subKeys1);
	Subkey_generation(key_2, subKeys2);
	Subkey_generation(key_3, subKeys3);



	//如果文件非空,循環(huán)分組處理
	while (!feof(plain))
	{//每次讀64bite,8字節(jié)
		if ((num = fread(plainBlock, sizeof(char), 8, plain)) == 8)
		{	encryptBlock(plainBlock, subKeys1, temp1);
			decryptBlock(temp1, subKeys2, temp2);
			encryptBlock(temp2, subKeys3, result);

			fwrite(result, sizeof(char), 8, cipher);
		}
	}
	//最后一組額外判斷
	if (num)
	{//對于不足一組的位置進行填充\0
		memset(plainBlock + num, '\0', 7 - num);
		//最后一個字符作為校驗位,保存包括最后一個字符在內(nèi)的所填充的字符數(shù)量
		plainBlock[7] = 8 - num;
		encryptBlock(plainBlock, subKeys1, temp1);
		decryptBlock(temp1, subKeys2, temp2);
		encryptBlock(temp2, subKeys3, result);

		fwrite(result, sizeof(char), 8, cipher);
	}
	fclose(plain);
	fclose(cipher);

}

//解密文件

void Decrypt_3DES(const char* cipherFile, const char* key_1, const  char* key_2, const char* key_3, const char* plainFile)
{FILE* plain = NULL, * cipher = NULL;
	int num = 0, times = 0;
	long len;
	char temp1[8], temp2[8], result[8], cipherBlock[8];

	char subKeys1[16][48], subKeys2[16][48], subKeys3[16][48];


	errno_t err2 = fopen_s(&cipher, cipherFile, "rb");
	errno_t err3 = fopen_s(&plain, plainFile, "wb");

	Subkey_generation(key_1, subKeys1);
	Subkey_generation(key_2, subKeys2);
	Subkey_generation(key_3, subKeys3);

	//取文件長度
	fseek(cipher, 0, SEEK_END);
	len = ftell(cipher);
	rewind(cipher);

	//循環(huán)分組處理
	while (1)
	{//密文的字節(jié)數(shù)一定是8的整數(shù)倍
		fread(cipherBlock, sizeof(char), 8, cipher);
		//3DES解密
		decryptBlock(cipherBlock, subKeys3, temp1);
		encryptBlock(temp1, subKeys2, temp2);
		decryptBlock(temp2, subKeys1, result);
		times += 8;
		if (times< len)//跳過處理最后一組
		{	fwrite(result, sizeof(char), 8, plain);
		}
		else
		{	break;
		}
	}
	//檢查最后一組
	if (result[7]< 8)
	{//小于8說明可能是校驗位,因為校驗位不超過8
		for (num = 8 - result[7]; num< 7; num++)
		{//從校驗位的值開始比對,如果都是填充字符說明對上了
			if (result[num] != '\0')
			{//有非填充字符表明無填充
				decryptBlock(cipherBlock, subKeys3, temp1);
				encryptBlock(temp1, subKeys2, temp2);
				decryptBlock(temp2, subKeys1, result);
				break;
			}
		}
	}
	if (num == 7)  //對上了,有填充
	{//舍棄填充數(shù)據(jù)
		fwrite(result, sizeof(char), 8 - result[7], plain);
	}
	else  //無填充
	{fwrite(result, sizeof(char), 8, plain);
	}

	fclose(plain);
	fclose(cipher);

}




int main()
{LARGE_INTEGER cpuFreq;
	LARGE_INTEGER startTime;
	LARGE_INTEGER endTime;
	double rumTime = 0.0;

	QueryPerformanceFrequency(&cpuFreq);
	const char key_1[8] = {'1','2','3','4','5','6','7','8' };
	const char key_2[8] = {'9','8','7','6','5','4','2','1' };
	const char key_3[8] = {'m','n','d','a','a','f','r','!' };
	cout<< "開始加密"<< endl;
	const  char* a = "";//要加密的文件
	const  char* b = "";//加密后的文件儲存地
	const  char* c = "";//解密后的文件儲存地

	QueryPerformanceCounter(&startTime);
	Encrypt_3DES(a, key_1, key_2, key_3, b);
	QueryPerformanceCounter(&endTime);
	rumTime = (((endTime.QuadPart - startTime.QuadPart) * 1000.0f) / cpuFreq.QuadPart);
	cout<< "加密成功,用時"<< rumTime<< "ms"<< endl<< "開始解密"<< endl;
	QueryPerformanceCounter(&startTime);
	Decrypt_3DES(b, key_1, key_2, key_3, c);
	QueryPerformanceCounter(&endTime);
	rumTime = (((endTime.QuadPart - startTime.QuadPart) * 1000.0f) / cpuFreq.QuadPart);

	cout<< "解密完成,用時"<< rumTime<< "ms"<< endl;



}



你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)站欄目:3DES,C++實現(xiàn)-創(chuàng)新互聯(lián)
分享路徑:http://www.muchs.cn/article26/hgccg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站內(nèi)鏈外貿(mào)建站、微信小程序移動網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)