密码学实验–DES加密算法实现
实现效果:分步实现加密结果
具体实现描述:
1.定义密钥变量key
2.定义子密钥存储变量subkey
3.将pc_1,pc_2,ip,ip_1,e盒和s盒,p,加密轮数据新建数组变量保存
4.定义charToBitset方法进行字符串到二进制数的转换
5.定义exec函数对数据进行进行扩展置换,异或,查s盒置换,p置换的初始操作
6.定义leftshift函数进行左移操作
7.定义madekey函数进行子密钥生成
8.定义encrypto函数进行ip置换,迭代加密并输出加密结果
代码如下:
#include <iostream>
#include <string>
#include <bitset>
#include <fstream>
#include <algorithm>
#include <sstream>
using namespace std;
bitset<64> key;
bitset<48> subkey[16];
int pc_1[] = {
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 pc_2[] = {
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, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
};
int ip[] = {
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 ip_1[] = {
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
};
int E_box[]