-
Notifications
You must be signed in to change notification settings - Fork 3
/
playfairCrack.c
364 lines (330 loc) · 9.42 KB
/
playfairCrack.c
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
/*
* Written by Nate Stewart
* Program to crack a playfair cipher using simulated annealing with quadgrams
* 03/20/16
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include <time.h>
#include "quadgram.h"
#include "playfairCrack.h"
extern float quadgram[];
int main(int argc, char **argv) {
char key[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
FILE *fin = stdin;
/*
* Get command line arguments. Setup key and input location
*/
switch (argc) {
case 2:
fin = fopen(argv[1], "r");
if (fin == NULL) {
printf("Invalid file: %s. Unable to open for reading.\n", argv[3]);
return -1;
}
case 1:
if (!removeLetter(key, 'J')) {
printf("Could not remove letter J from cipher key.\n");
return -1;
}
break;
case 4:
fin = fopen(argv[3], "r");
if (fin == NULL) {
printf("Invalid file: %s. Unable to open for reading.\n", argv[3]);
return -1;
}
case 3:
if (strcmp(argv[1], "-r")) {
printf("Optional parameter '-r' not found. '%s' found instead.\n", argv[1]);
return -1;
}
if(!removeLetter(key, argv[2][0])) {
printf("Could not remove letter %c from cipher key.\n", argv[2][0]);
return -1;
}
break;
default:
printf("Invalid usage. See below for proper usage.\n");
printf("\t./%s [ -r <character_to_remove> ] [ ciphertext_filepath ]\n", argv[0]);
return -1;
}
/*
* Input cipher and ensure it is valid
*/
char *ciphertext, *plaintext;
int messageLen;
ciphertext = readCipher(fin, INPUT_STEP_SIZE);
messageLen = strlen(ciphertext);
if (validateText(ciphertext, &messageLen) != 0) {
free(ciphertext);
return -1;
}
ciphertext = realloc(ciphertext, sizeof(*ciphertext) * (messageLen + 1));
ciphertext[messageLen] = '\0';
plaintext = calloc(messageLen + 1, sizeof(*plaintext));
strcpy(plaintext, ciphertext);
// close the file as long as it is not stdin
if (fin != stdin)
fclose(fin);
// Output relevant information for error checking
printf("Attempting to crack the following ciphertext with key: %s\n", key);
printf("%s\n", ciphertext);
int iter = 0;
double score = -DBL_MAX, maxScore = -DBL_MAX;
srand(time(NULL)); // randomize seed
// Run until max iteration met
while (iter < MAX_ITERATIONS) {
iter++;
score = simulatedAnnealing(key, ciphertext, plaintext, messageLen);
if (score > maxScore) {
maxScore = score;
decipher(key, ciphertext, plaintext, messageLen);
printf("\nPossible Plaintext found using key:\n");
outputKey(key);
printf("%s\n\n", plaintext);
}
}
free(plaintext);
free(ciphertext);
return 0;
}
double simulatedAnnealing(char *key, char *ciphertext, char *plaintext, int messageLen) {
int count, iter;
float annealStep;
char newKey[26], oldKey[26];
double prob, delta, maxScore, score, bestScore;
// Copy over key so we don't screw up our master copy. Decipher ciphertext using key and score it
strcpy(oldKey,key);
decipher(oldKey, ciphertext, plaintext, messageLen);
maxScore = scoreText(plaintext,messageLen);
bestScore = maxScore;
iter = 0;
// For each step, find our best key
for (annealStep = ANNEALING_TEMP; annealStep >= 0; annealStep -= ANNEALING_STEP_SIZE) {
for (count = 0; count < MAX_ITERATIONS; count++) {
strcpy(newKey, oldKey);
alterKey(newKey);
decipher(newKey, ciphertext, plaintext, messageLen);
score = scoreText(plaintext, messageLen);
// our difference between our current maxScore and step score
delta = score - maxScore;
// We did work in the positive direction (hopefully...)
if (delta >= 0) {
maxScore = score;
strcpy(oldKey, newKey);
} else if (annealStep > 0) {
// the work we did is a side-grade
prob = exp(delta / annealStep);
if (prob > 1.0 * rand() / RAND_MAX) {
maxScore = score;
strcpy(oldKey, newKey);
}
}
// This is our best score so far
if (maxScore > bestScore){
bestScore = maxScore;
strcpy(key, oldKey);
outputStats(iter, bestScore, key);
}
iter++;
}
}
return bestScore;
}
void keySwapRows(char *key, int r1, int r2) {
int i;
char temp;
for (i = 0; i < 5; i++) {
temp = key[r1 * 5 + i];
key[r1 * 5 + i] = key[r2 * 5 + i];
key[r2 * 5 + i] = temp;
}
}
void keySwapCols(char *key, int c1, int c2) {
int i;
char temp;
for (i = 0; i < 5; i++) {
temp = key[i * 5 + c1];
key[i * 5 + c1] = key[i * 5 + c2];
key[i * 5 + c2] = temp;
}
}
void keySwapChars(char *key, int i1, int i2) {
char temp = key[i1];
key[i1] = key[i2];
key[i2] = temp;
}
void keyShuffle(char *key, int num) {
int i;
for (i = 0; i < num; i++) {
keySwapChars(key, rand() % 25, rand() % 25);
}
}
void alterKey(char *key) {
switch(rand() % 100) {
case 1:
case 2:
keyShuffle(key, rand() % 26);
break;
case 3:
case 4:
case 5:
case 6:
keySwapRows(key, rand() % 5, rand() % 5);
break;
case 7:
case 8:
case 9:
case 10:
keySwapCols(key, rand() % 5, rand() % 5);
default:
keySwapChars(key, rand() % 25, rand() % 25);
break;
}
}
void decipher(char *key, char *ciphertext, char *plaintext, int len) {
int i;
// index, row and column of the current digram
char c1, c2;
int c1_ind, c1_row, c1_col, c2_ind, c2_row, c2_col;
for (i = 0; i < len; i += 2) {
c1 = ciphertext[i], c2 = ciphertext[i + 1];
// strchr returns a pointer to the first index of character in key. subtract key from pointer to get index
c1_ind = (int)(strchr(key, c1) - key), c2_ind = (int)(strchr(key, c2) - key);
// Rows have offset 5, columns are mod 5
c1_row = c1_ind / 5, c2_row = c2_ind / 5;
c1_col = c1_ind % 5, c2_col = c2_ind % 5;
if (c1_row == c2_row && c1_col == c2_col) { // Same character
int row = c1_row - 1, col = c1_col - 1;
if (row < 0)
row += 5;
if (col < 0)
col += 5;
int ind = 5 * row + col;
plaintext[i] = ind, plaintext[i + 1] = ind;
} else if (c1_row == c2_row) { // same row
// Determine if wrapping occurred
if (c1_col == 0) {
plaintext[i] = key[c1_ind + 4];
plaintext[i+1] = key[c2_ind - 1];
} else if (c2_col == 0 ) {
plaintext[i] = key[c1_ind - 1];
plaintext[i+1] = key[c2_ind + 4];
} else {
plaintext[i] = key[c1_ind - 1];
plaintext[i+1] = key[c2_ind - 1];
}
} else if (c1_col == c2_col ) { // same column
if (c1_row == 0) {
plaintext[i] = key[c1_ind + 20];
plaintext[i+1] = key[c2_ind - 5];
} else if (c2_row == 0) {
plaintext[i] = key[c1_ind - 5];
plaintext[i+1] = key[c2_ind + 20];
} else {
plaintext[i] = key[c1_ind - 5];
plaintext[i+1] = key[c2_ind - 5];
}
} else { // rectangle rule
plaintext[i] = key[5 * c1_row + c2_col];
plaintext[i+1] = key[5 * c2_row + c1_col];
}
}
}
void outputKey(char *key) {
int i;
for (i = 0; i < 25; i += 5) {
printf("%c %c %c %c %c\n", key[i + 0], key[i + 1], key[i + 2], key[i + 3], key[i + 4]);
}
}
void outputStats(int iteration, double score, char *key) {
printf("Iteration: %8d, \tbest score: %12.4lf, \tCurrent key: %s\n", iteration, score, key);
}
bool removeLetter(char *cipher, char letter) {
char *src, *dst;
if (letter >= 'a' && letter <= 'z') {
letter -= ' '; // make it capital letter
}
bool found; // if the letter was found or not
for (src = dst = cipher; *src != '\0'; src++) {
*dst = *src;
if (*dst != letter)
dst++;
else
found = true;
}
*dst = '\0';
return found;
}
char *readCipher(FILE *fin, size_t size) {
char *str;
int currChar;
size_t len = 0;
str = malloc(sizeof(*str) * size); // Initially allocate str with provided size
if (!str) return str; // If provided size is 0, or unable to allocate memory, return empty string
while (EOF != (currChar = fgetc(fin)) && currChar != '\n') {
str[len++] = currChar;
if (len == size) {
str = realloc(str, sizeof(*str) * (size += INPUT_STEP_SIZE));
if (!str) return str; // If unable to realloc, return what we have so far
}
}
// Add null terminator onto end of string
str = realloc(str, sizeof(*str) * (++len));
str[len - 1] = '\0';
return str;
}
double scoreQuadgram(char *text) {
int index[4];
char output[5];
memset(output, '\0', sizeof(output));
strncpy(output, text, 4);
// Get a number associated with the index of each character
index[0] = (*(text + 0) - 'A') * 17576;
index[1] = (*(text + 1) - 'A') * 676;
index[2] = (*(text + 2) - 'A') * 26;
index[3] = (*(text + 3) - 'A') * 1;
//int totalIndex = index[0] + index[1] + index[2] + index[3];
//printf("Text: %s | ", output);
//printf("total Index:%d | 0:%d | 1:%d | 2:%d | 3:%d\n",totalIndex, index[0], index[1], index[2], index[3]);
return quadgram[index[0] + index[1] + index[2] + index[3]];
}
double scoreText(char *text, int len) {
int i;
double score = 0.0;
// Calculate all quadgrams in the text
for (i = 0; i < len - 3; i++) {
score += scoreQuadgram(text + i);
}
return score;
}
int validateText(char *input, int *len) {
int i;
// Declare an output array and initialize it to all 0's
char *output = calloc(*len, sizeof(*output));
int offset = 0;
// Eliminate spaces and reformat case of text
for (i = 0; i < *len; i++) {
// Convert lower case to upper case where applicable
if (input[i] >= 'a' && input[i] <= 'z') {
output[i - offset] = input[i] - ' ';
} else if (input[i] == ' ') {
offset += 1;
} else if (input[i] >= 'A' && input[i] <= 'Z') {
output[i - offset] = input[i];
} else {
// Invalid character
printf("Invalid character: %c\n", input[i]);
free(output);
return -1;
}
}
strcpy(input, output);
free(output);
*len -= offset; // Shrink length according to how many spaces were filtered out
return 0;
}