-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDESAlgorithm.cpp
More file actions
269 lines (235 loc) · 6.56 KB
/
DESAlgorithm.cpp
File metadata and controls
269 lines (235 loc) · 6.56 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* DESAlgorithm.cpp
*/
#include "DESAlgorithm.h"
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <bitset>
using namespace std;
//#define DEBUG
DESAlgorithm::DESAlgorithm(std::string input, int numberOfRounds) :
input(input), numberOfRounds(numberOfRounds) {
if (input.length() < 64) {
cout << "Message length should be of 64 bits" << endl;
exit(-1);
}
for (size_t var = 0; var < input.length(); var++) {
// cout<< var+1 << ":" <<message[var] << "\t" << initial_message_permutation[var]<< ":" << message[initial_message_permutation[var]-1] << endl;
messageAfterIP += input[initialMessagePermutation[var] - 1];
}
#ifdef DEBUG
printBytes(messageAfterIP);
#endif
}
DESAlgorithm::~DESAlgorithm() {
}
bool DESAlgorithm::verify() {
return true;
}
void DESAlgorithm::printBytes(const std::string& msg, std::string debugMessage,
int subGroupBits) {
debugMessage = debugMessage.empty() ? "msg" : debugMessage;
cout << endl;
for (size_t var = 0; var < msg.length(); var = var + subGroupBits) {
cout << msg.substr(var, subGroupBits) << " ";
}
cout << "\t<---\t" << debugMessage << endl;
}
std::string DESAlgorithm::rotateBits(std::string& input,
int numberOfRotations) {
string output;
// if(numberOfRotations == 1) // Expecting function gets invoked for at least one rotation
{
char temp = input.at(0);
output = input.erase(0, 1);
output += temp;
}
if (numberOfRotations == 2) {
char temp = output.at(0);
output = output.erase(0, 1);
output += temp;
}
return output;
}
void DESAlgorithm::generateKeys(const std::string& key) {
// string key("ramsumkc");
// string bitKey = "";
// for (i = 0; i < key.size(); ++i) {
// bitKey += bitset<8>(key[i]).to_string();
// }
#ifdef DEBUG
printBytes(key, "Trying key");
#endif
size_t i;
string permutedKey;
for (i = 0; i < 56; i++) {
permutedKey += key[initialKeyPermutaion[i] - 1];
}
#ifdef DEBUG
printBytes(permutedKey, "Permuted key", 7);
#endif
string left = permutedKey.substr(0, 28);
string right = permutedKey.substr(28);
#ifdef DEBUG
printBytes(left, "Left sub key", 7);
printBytes(right, "Right sub key", 7);
#endif
string leftRotations[numberOfRounds];
string rightRotations[numberOfRounds];
string temporaryKeyAfterRotation[numberOfRounds];
for (i = 0; i < numberOfRounds; i++) {
leftRotations[i] = rotateBits(left, keyRotations[i]);
rightRotations[i] = rotateBits(right, keyRotations[i]);
left = leftRotations[i];
right = rightRotations[i];
temporaryKeyAfterRotation[i] = left + right;
}
// for (i = 0; i<16; i++)
// printBytes(temporaryKeyAfterRotation[i], "Rotated key " + i, 7);
// return;
// string finalKeys[16];
for (i = 0; i < numberOfRounds; i++) {
finalKeys[i] = "";
for (int j = 0; j < 48; j++)
finalKeys[i] += temporaryKeyAfterRotation[i][subKeyPermutation[j]
- 1];
//printBytes(finalKeys[i], "Key", 6);
}
}
int DESAlgorithm::getDecimalVale(const std::string& input) {
/*
* char a = '4';
* int ia = a - '0';
*
* input = 101
* decimalValue = 2^2 * 1 + 2^1 * 0 + 2^0 * 1
*/
int decimalValue = 0, base = 1;
size_t totalBits = input.length();
for (size_t i = 0; i < totalBits; i++) {
decimalValue += (input[totalBits - i - 1] - '0') * base;
base *= 2;
}
//cout << endl << endl << "The decimal value of " << input << " is: " << decimalValue << endl;
return decimalValue;
}
std::string DESAlgorithm::functionF(const std::string& right,
const std::string& key) {
#ifdef DEBUG
printBytes(right, "initial Right", 4);
#endif
string response = "";
std::string expandedRight;
for (int i = 0; i < 48; i++) {
expandedRight += right[messageExpansion[i] - 1];
}
#ifdef DEBUG
printBytes(key, "Key", 6);
printBytes(expandedRight, "expanded right", 6);
#endif
string rXorKey;
char temp[10];
for (int i = 0; i < 48; i++) {
sprintf(temp, "%d",
getDecimalVale(expandedRight.substr(i, 1))
^ getDecimalVale(key.substr(i, 1)));
rXorKey.append(temp);
}
#ifdef DEBUG
printBytes(rXorKey, "after XOR", 6);
#endif
string sBoxesOutput;
int currentBox = 1;
for (int i = 0; i < 48; i += 6) {
string sBoxInput = rXorKey.substr(i, 6);
char rowValue[5];
sprintf(rowValue, "%c%c", sBoxInput[0], sBoxInput[5]);
int row = getDecimalVale(rowValue); //sBoxInput[0]+sBoxInput[5]);
int column = getDecimalVale(sBoxInput.substr(1, 4));
if (currentBox == 1)
sBoxesOutput += sBox1[row][column];
else if (currentBox == 2)
sBoxesOutput += sBox2[row][column];
else if (currentBox == 3)
sBoxesOutput += sBox3[row][column];
else if (currentBox == 4)
sBoxesOutput += sBox4[row][column];
else if (currentBox == 5)
sBoxesOutput += sBox5[row][column];
else if (currentBox == 6)
sBoxesOutput += sBox6[row][column];
else if (currentBox == 7)
sBoxesOutput += sBox7[row][column];
else if (currentBox == 8)
sBoxesOutput += sBox8[row][column];
currentBox++;
}
#ifdef DEBUG
printBytes(sBoxesOutput, "Output of sBoxe", 4);
#endif
for (int i = 0; i < 32; i++) {
response += sBoxesOutput[rightSubMessagePermutation[i] - 1];
}
#ifdef DEBUG
printBytes(response, "Final output of function", 4);
#endif
return response;
}
/*void DESAlgorithm::intToBin(int value)
{
cout << "\n{";
for(int i = 1; i <= 64; i++)
{
cout<<" \""<<bitset<4>(value)<<"\",";
if(i != 0 && i%16 == 0)
cout <<"},\n{";
}
}*/
std::string DESAlgorithm::getRightSubGroup(const std::string& left,
const std::string& right, const std::string& key) {
string response;
string functionFResponse = functionF(right, key);
char temp[10];
for (int i = 0; i < 32; i++) {
sprintf(temp, "%d",
getDecimalVale(left.substr(i, 1))
^ getDecimalVale(functionFResponse.substr(i, 1)));
response.append(temp);
}
#ifdef DEBUG
printBytes(response, "Right subgroup", 4);
#endif
return response;
}
string DESAlgorithm::encrypt(const std::string& key) {
#ifdef DEBUG
printBytes(input);
#endif
string left = messageAfterIP.substr(0, 32);
string right = messageAfterIP.substr(32);
#ifdef DEBUG
printBytes(left);
printBytes(right);
#endif
generateKeys(key);
for (int round = 0; round < numberOfRounds; round++) {
string leftSubGroup = right;
string rightSubGroup = getRightSubGroup(left, right, finalKeys[round]); // left XOR function
left = leftSubGroup;
right = rightSubGroup;
}
#ifdef DEBUG
printBytes(left, "Final Left subgroup", 4);
printBytes(right, "Final Right subgroup", 4);
#endif
string tempOutput = right + left;
string encryptedMessage;
for (size_t i = 0; i < tempOutput.length(); i++) {
encryptedMessage += tempOutput[finalMessagePermutation[i] - 1];
}
#ifdef DEBUG
printBytes(encryptedMessage, "Final Encrypted Message", 8);
#endif
return encryptedMessage;
}