-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.cpp
More file actions
415 lines (328 loc) · 9.44 KB
/
encryption.cpp
File metadata and controls
415 lines (328 loc) · 9.44 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
Jeremy Doss
Main Project - Encryption Algorithm
CSCE 4550 - Introduction to Security
12/3/14
*/
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#define MAX 16384
using namespace std;
//Global Declarations.
unsigned char message[MAX];
unsigned char key[16];
unsigned char matrix[MAX/16][4][4];
unsigned int msg_ctr;
ofstream output_log;
//Function Prototypes.
void encrypt(string);
void read_message(string);
void sub_cypher();
void pad();
void shift_rows();
void parity();
void mix_columns();
bool is_odd(unsigned char);
unsigned char rgf_multiply(unsigned char, int);
//Main program function.
int main() {
string input;
output_log.open("output.txt");
output_log << "\n---------------------- A E S - E N C R Y P T I O N ----------------------\n" << endl;
cout << "\n---------------------- A E S - E N C R Y P T I O N ----------------------\n" << endl;
cout << "Options:\n1: 'run'\n2: 'exit'" << endl;
//Main program loop.
while(1) {
cout << "Input:> ";
cin >> input;
if (input == "run" || input == "1") {
cout << "Enter filename: ";
cin >> input;
//Initialize the encryption algorithm.
encrypt(input);
}
else if (input == "exit" || input == "2") {
output_log << "\n-----------------THANK YOU! Exiting encryption program..-----------------\n" << endl;
cout << "\n-----------------THANK YOU! Exiting encryption program..-----------------\n" << endl;
break;
}
else
cout << "Error: Invalid command." << endl;
}
output_log.close();
return 0;
}
void encrypt(string filename) {
//Step 1: Preprocessing - Read and process the message from the input file.
read_message(filename);
//Step 2: Substitution - Use key.txt to encrypt the message.
sub_cypher();
//Step 3: Padding - Pad the encrypted message with 'A's.
pad();
//Step 4: ShiftRows - Perform a circular shift on each 4x4 matrix.
shift_rows();
//Step 5: Parity Bit - Alter each characters binary representation by altering the significant bit.
parity();
//Step 6: MixColumns - Multiply the circulant MDS matrix with each input column.
mix_columns();
cout << "Message finished encrypting! Thank you!" << endl;
output_log << "\nMessage finished encrypting! Thank you!\n" << endl;
}
void read_message(string filename) {
ifstream input;
string line;
input.open(filename.c_str());
if (input.is_open()) {
msg_ctr = 0;
output_log << "\n----------------- Step 1: Preprocessing! ----------------" << endl;
output_log << "\nInput Text:" << endl;
while (getline(input, line)) {
output_log << line;
//Store each line in unsigned char array up to 80 characters.
for (unsigned int i = 0; i < line.size(); i++) {
if (i == 80) {
output_log << "\nInput line length greater than 80 and was truncated." << endl;
break;
}
if (line[i] >= 65 && line[i] <= 90) {
message[msg_ctr] = line[i];
msg_ctr++;
}
}
}
output_log << "\n\nOutput Text:" << endl;
output_log << message << endl;
input.close();
}
else {
cout << "Error opening the input file!" << endl;
cout << "Terminating program...\n" << endl;
exit(0);
}
}
void sub_cypher() {
unsigned int m_val;
unsigned int k_val;
unsigned int e_val;
ifstream input;
string line;
input.open("key.txt");
if (input.is_open()) {
output_log << "\n----------------- Step 2: Substitution! -----------------" << endl;
output_log << "\nKey:" << endl;
if (getline(input, line) && line.size() == 16) {
for (int i = 0; i < 16; i++)
key[i] = line[i];
output_log << key << endl;
output_log << "\nInput Text:\n" << message << endl;
}
else {
cout << "Error! Incorrect encryption key size!" << endl;
cout << "Terminating program...\n" << endl;
return;
}
for (unsigned int i = 0; i < msg_ctr; i++) {
m_val = message[i] - 65;
k_val = key[i % 16] - 65;
e_val = (m_val + k_val) % 26;
message[i] = (unsigned char)(e_val + 65);
}
output_log << "\nOutput Text:" << endl;
output_log << message << endl;
input.close();
}
else {
cout << "Error accessing the encryption key file!" << endl;
cout << "Terminating program...\n" << endl;
exit(0);
}
}
void pad() {
int count = 0;
output_log << "\n-------------------- Step 3: Padding! -------------------" << endl;
output_log << "\nInput:" << endl;
//A loop for formatting the character array into a 4x4 look.
for (unsigned int i = 0; i < msg_ctr; i++) {
output_log << message[i];
if ((i + 1) % 4 == 0)
output_log << endl;
if ((i + 1) % 16 == 0)
output_log << endl;
}
//Add 'A's to make the correct message length.
while (msg_ctr % 16 != 0) {
message[msg_ctr] = 'A';
msg_ctr++;
}
//Translate encrypted message string into an array of 4x4 unsigned characters.
for (unsigned int i = 0; i < (msg_ctr / 16); i++)
for (int j = 0; j < 4; j++)
for (int k = 0; k < 4; k++)
matrix[i][j][k] = message[count++];
output_log << "\n\nOutput:" << endl;
//A loop for outputting the 4x4 arrays.
for (unsigned int i = 0; i < (msg_ctr / 16); i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
output_log << matrix[i][j][k];
}
output_log << endl;
}
output_log << endl;
}
}
void shift_rows() {
unsigned char temp;
output_log << "\n------------------ Step 4: ShiftRows! -------------------" << endl;
output_log << "\nInput:" << endl;
//A loop for outputting the 4x4 arrays.
for (unsigned int i = 0; i < (msg_ctr / 16); i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
output_log << matrix[i][j][k];
}
output_log << endl;
}
output_log << endl;
}
//Loop through and shift each 4x4 array.
for (unsigned int i = 0; i < (msg_ctr / 16); i++) {
//Circular shift the second row by 1 column to the left.
temp = matrix[i][1][0];
matrix[i][1][0] = matrix[i][1][1];
matrix[i][1][1] = matrix[i][1][2];
matrix[i][1][2] = matrix[i][1][3];
matrix[i][1][3] = temp;
//Circular shift the third row by 2 columns to the left.
temp = matrix[i][2][0];
matrix[i][2][0] = matrix[i][2][2];
matrix[i][2][2] = temp;
temp = matrix[i][2][1];
matrix[i][2][1] = matrix[i][2][3];
matrix[i][2][3] = temp;
//Circular shift the fourth row by 3 columns to the left.
temp = matrix[i][3][0];
matrix[i][3][0] = matrix[i][3][3];
matrix[i][3][3] = matrix[i][3][2];
matrix[i][3][2] = matrix[i][3][1];
matrix[i][3][1] = temp;
}
output_log << "\n\nOutput:" << endl;
//A loop for outputting the 4x4 arrays.
for (unsigned int i = 0; i < (msg_ctr / 16); i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
output_log << matrix[i][j][k];
}
output_log << endl;
}
output_log << endl;
}
}
void parity() {
output_log << "\n------------------ Step 5: Parity Bit! ------------------" << endl;
output_log << "\nInput:" << endl;
//A loop for outputting the 4x4 arrays.
for (unsigned int i = 0; i < (msg_ctr / 16); i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++)
output_log << matrix[i][j][k];
output_log << endl;
}
}
//Loop through each character.
for (unsigned int i = 0; i < (msg_ctr / 16); i++) {
for (int j = 0; j < 4; j++)
for (int k = 0; k < 4; k++)
//Check if even or odd amount of ones.
if (is_odd(matrix[i][j][k])) {
//Set significant bit to 1.
matrix[i][j][k] |= 0x80;
}
}
output_log << "\nOutput (in hexadecimal):" << endl;
//A loop for outputting the 4x4 arrays.
for (unsigned int i = 0; i < (msg_ctr / 16); i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++)
output_log << hex << (int)matrix[i][j][k] << " ";
output_log << endl;
}
}
}
void mix_columns() {
unsigned char temp1, temp2, temp3;
output_log << "\n------------------ Step 6: MixColumns! ------------------" << endl;
output_log << "\nInput:" << endl;
//A loop for outputting the 4x4 arrays.
for (unsigned int i = 0; i < (msg_ctr / 16); i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++)
output_log << hex << (int)matrix[i][j][k] << " ";
output_log << endl;
}
}
//Perform the MixColumns operations!
for (unsigned int i = 0; i < (msg_ctr / 16); i++) {
for (int j = 0; j < 4; j++) {
//Calculate a0.
temp1 = matrix[i][0][j];
matrix[i][0][j] = rgf_multiply(matrix[i][0][j], 2) ^ rgf_multiply(matrix[i][1][j], 3)
^ matrix[i][2][j] ^ matrix[i][3][j];
//Calculate a1.
temp2 = matrix[i][1][j];
matrix[i][1][j] = temp1 ^ rgf_multiply(matrix[i][1][j], 2) ^ rgf_multiply(matrix[i][2][j], 3)
^ matrix[i][3][j];
//Calculate a2.
temp3 = matrix[i][2][j];
matrix[i][2][j] = temp1 ^ temp2 ^ rgf_multiply(matrix[i][2][j], 2) ^ rgf_multiply(matrix[i][3][j], 3);
//Calculate a3.
matrix[i][3][j] = rgf_multiply(temp1, 3) ^ temp2 ^ temp3 ^ rgf_multiply(matrix[i][3][j], 2);
}
}
output_log << "\nOutput (in hexadecimal):" << endl;
//A loop for outputting the 4x4 arrays.
for (unsigned int i = 0; i < (msg_ctr / 16); i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++)
output_log << hex << (int)matrix[i][j][k] << " ";
output_log << endl;
}
}
}
bool is_odd(unsigned char value) {
int bit_cnt = 0;
while (value != 0) {
if (value & 1)
bit_cnt++;
value >>= 1;
}
if (bit_cnt % 2 == 0)
return false;
else
return true;
}
unsigned char rgf_multiply(unsigned char value, int rgf_val) {
bool MSB = false;
if (value & 0x80)
MSB = true;
switch (rgf_val) {
case 2:
value <<= 1;
if (MSB) {
value ^= 0x1b;
}
break;
case 3:
value = (value << 1) ^ value;
if (MSB) {
value ^= 0x1b;
}
break;
default:
break;
}
return value;
}