3DES,C++实现-创新互联
首先将3DES原理简单介绍
1.3DES是一种对称加密,加解密使用的密钥相同。
2.属于分组加密算法,每一组明文大小64比特
3.密钥长度为64比特,但实际作用的只有56比特
4.整体结构由三个DES结构拼接而来:加密-解密-加密(可反过来),原因是为了向下兼容,当前两个使用相同的密钥时就相当于只进行了一次DES
c++代码如下
首先进行头文件的导入
#include#include#include "memory.h"
#include#include#includeusing namespace std;
接着进行定义一些需要的常量,这些常量的取值来源于DES标准,不是自己设置的,只需要拿下来用即可
//初始置换表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
};
//对左移次数的规定
int left_bits[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1 };
加密单个分组,按照Feistel密码结构进行即可
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比特)进行分组,这里就会产生一个问题,即明文长度不为64的整数倍,这时候就需要规定一个填充标准,在加解密时采用一致的标准即可
这里采用的标准是在末尾补0,最后一位设置为校验位记录补了多少,没补即为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");
//设置密钥
//生成子密钥
Subkey_generation(key_1, subKeys1);
Subkey_generation(key_2, subKeys2);
Subkey_generation(key_3, subKeys3);
//如果文件非空,循环分组处理
while (!feof(plain))
{//每次读64bite,8字节
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);
//最后一个字符作为校验位,保存包括最后一个字符在内的所填充的字符数量
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
};
//对左移次数的规定
int left_bits[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1 };
//字节转换成二进制
void ByteToBit(const char ch, char bit[8])
{int cnt;
for (cnt = 0; cnt< 8; cnt++)
{bit[cnt] = (ch >>cnt) & 1;
}
}
//二进制转换成字节
void BitToByte(const char bit[8], char* ch)
{int cnt;
for (cnt = 0; cnt< 8; cnt++)
{*ch |= *(bit + cnt)<< cnt;
}
}
//将长度为8的字符串转为二进制位串
void Char8ToBit64(const char ch[8], char bit[64])
{int cnt;
for (cnt = 0; cnt< 8; cnt++)
{ByteToBit(*(ch + cnt), bit + (cnt<< 3));
}
}
//将二进制位串转为长度为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]];
}
}
//循环左移
void left_shift(char data[56], int time)
{char temp[56];
//保存将要循环移动到右边的位
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]);//循环左移
transform2(temp, subKeys[i]);//置换2,产生子密钥
}
}
//加密单个分组
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");
//设置密钥
//生成子密钥
Subkey_generation(key_1, subKeys1);
Subkey_generation(key_2, subKeys2);
Subkey_generation(key_3, subKeys3);
//如果文件非空,循环分组处理
while (!feof(plain))
{//每次读64bite,8字节
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);
//最后一个字符作为校验位,保存包括最后一个字符在内的所填充的字符数量
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);
//循环分组处理
while (1)
{//密文的字节数一定是8的整数倍
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) //对上了,有填充
{//舍弃填充数据
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;
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
网页名称:3DES,C++实现-创新互联
URL地址:http://hbruida.cn/article/hgccg.html