-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDESAlgorithm.h
More file actions
156 lines (130 loc) · 7.08 KB
/
DESAlgorithm.h
File metadata and controls
156 lines (130 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* DESAlgorithm.h
*/
#ifndef DESALGORITHM_H_
#define DESALGORITHM_H_
#include <cstring>
#include <bitset>
class DESAlgorithm {
private:
std::string input;
std::string messageAfterIP;
int numberOfRounds;
int initialMessagePermutation[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 initialKeyPermutaion[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 keyRotations[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
int subKeyPermutation[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, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29,
32 };
int messageExpansion[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 };
void intToBin(int value);
// Storing binary converted values to reduce the computation
std::string sBox1[4][16] = { { "1110", "0100", "1101", "0001", "0010",
"1111", "1011", "1000", "0011", "1010", "0110", "1100", "0101",
"1001", "0000", "0111" }, { "0000", "1111", "0111", "0100", "1110",
"0010", "1101", "0001", "1010", "0110", "1100", "1011", "1001",
"0101", "0011", "1000" }, { "0100", "0001", "1110", "1000", "1101",
"0110", "0010", "1011", "1111", "1100", "1001", "0111", "0011",
"1010", "0101", "0000" }, { "1111", "1100", "1000", "0010", "0100",
"1001", "0001", "0111", "0101", "1011", "0011", "1110", "1010",
"0000", "0110", "1101" } };
std::string sBox2[4][16] = { { "1111", "0001", "1000", "1110", "0110",
"1011", "0011", "0100", "1001", "0111", "0010", "1101", "1100",
"0000", "0101", "1010" }, { "0011", "1101", "0100", "0111", "1111",
"0010", "1000", "1110", "1100", "0000", "0001", "1010", "0110",
"1001", "1011", "0101" }, { "0000", "1110", "0111", "1011", "1010",
"0100", "1101", "0001", "0101", "1000", "1100", "0110", "1001",
"0011", "0010", "1111" }, { "1101", "1000", "1010", "0001", "0011",
"1111", "0100", "0010", "1011", "0110", "0111", "1100", "0000",
"0101", "1110", "1001" } };
std::string sBox3[4][16] = { { "1010", "0000", "1001", "1110", "0110",
"0011", "1111", "0101", "0001", "1101", "1100", "0111", "1011",
"0100", "0010", "1000" }, { "1101", "0111", "0000", "1001", "0011",
"0100", "0110", "1010", "0010", "1000", "0101", "1110", "1100",
"1011", "1111", "0001" }, { "1101", "0110", "0100", "1001", "1000",
"1111", "0011", "0000", "1011", "0001", "0010", "1100", "0101",
"1010", "1110", "0111" }, { "0001", "1010", "1101", "0000", "0110",
"1001", "1000", "0111", "0100", "1111", "1110", "0011", "1011",
"0101", "0010", "1100" } };
std::string sBox4[4][16] = { { "0111", "1101", "1110", "0011", "0000",
"0110", "1001", "1010", "0001", "0010", "1000", "0101", "1011",
"1100", "0100", "1111" }, { "1101", "1000", "1011", "0101", "0110",
"1111", "0000", "0011", "0100", "0111", "0010", "1100", "0001",
"1010", "1110", "1001" }, { "1010", "0110", "1001", "0000", "1100",
"1011", "0111", "1101", "1111", "0001", "0011", "1110", "0101",
"0010", "1000", "0100" }, { "0011", "1111", "0000", "0110", "1010",
"0001", "1101", "1000", "1001", "0100", "0101", "1011", "1100",
"0111", "0010", "1110" } };
std::string sBox5[4][16] = { { "0010", "1100", "0100", "0001", "0111",
"1010", "1011", "0110", "1000", "0101", "0011", "1111", "1101",
"0000", "1110", "1001" }, { "1110", "1011", "0010", "1100", "0100",
"0111", "1101", "0001", "0101", "0000", "1111", "1010", "0011",
"1001", "1000", "0110" }, { "0100", "0010", "0001", "1011", "1010",
"1101", "0111", "1000", "1111", "1001", "1100", "0101", "0110",
"0011", "0000", "1110" }, { "1011", "1000", "1100", "0111", "0001",
"1110", "0010", "1101", "0110", "1111", "0000", "1001", "1010",
"0100", "0101", "0011" } };
std::string sBox6[4][16] = { { "1100", "0001", "1010", "1111", "1001",
"0010", "0110", "1000", "0000", "1101", "0011", "0100", "1110",
"0111", "0101", "1011" }, { "1010", "1111", "0100", "0010", "0111",
"1100", "1001", "0101", "0110", "0001", "1101", "1110", "0000",
"1011", "0011", "1000" }, { "1001", "1110", "1111", "0101", "0010",
"1000", "1100", "0011", "0111", "0000", "0100", "1010", "0001",
"1101", "1011", "0110" }, { "0100", "0011", "0010", "1100", "1001",
"0101", "1111", "1010", "1011", "1110", "0001", "0111", "0110",
"0000", "1000", "1101" } };
std::string sBox7[4][16] = { { "0100", "1011", "0010", "1110", "1111",
"0000", "1000", "1101", "0011", "1100", "1001", "0111", "0101",
"1010", "0110", "0001" }, { "1101", "0000", "1011", "0111", "0100",
"1001", "0001", "1010", "1110", "0011", "0101", "1100", "0010",
"1111", "1000", "0110" }, { "0001", "0100", "1011", "1101", "1100",
"0011", "0111", "1110", "1010", "1111", "0110", "1000", "0000",
"0101", "1001", "0010" }, { "0110", "1011", "1101", "1000", "0001",
"0100", "1010", "0111", "1001", "0101", "0000", "1111", "1110",
"0010", "0011", "1100" } };
std::string sBox8[4][16] = { { "1101", "0010", "1000", "0100", "0110",
"1111", "1011", "0001", "1010", "1001", "0011", "1110", "0101",
"0000", "1100", "0111" }, { "0001", "1111", "1101", "1000", "1010",
"0011", "0111", "0100", "1100", "0101", "0110", "1011", "0000",
"1110", "1001", "0010" }, { "0111", "1011", "0100", "0001", "1001",
"1100", "1110", "0010", "0000", "0110", "1010", "1101", "1111",
"0011", "0101", "1000" }, { "0010", "0001", "1110", "0111", "0100",
"1010", "1000", "1101", "1111", "1100", "1001", "0000", "0011",
"0101", "0110", "1011" } };
int rightSubMessagePermutation[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 finalMessagePermutation[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 };
std::string finalKeys[16];
void printBytes(const std::string& msg, std::string debugMessage = "",
int subGroupBits = 8);
void generateKeys(const std::string& key);
std::string rotateBits(std::string& input, int numberOfRotations);
std::string functionF(const std::string& right, const std::string& key);
std::string getRightSubGroup(const std::string& left,
const std::string& right, const std::string& key);
int getDecimalVale(const std::string& input);
public:
DESAlgorithm(std::string input, int numberOfRounds = 16);
virtual ~DESAlgorithm();
const std::string& getInput() const {
return input;
}
bool verify();
std::string encrypt(const std::string& key);
};
#endif /* DESALGORITHM_H_ */