forked from ismeettkaur/Pigpen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.c
407 lines (366 loc) · 9.12 KB
/
Test.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
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
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int count = 0;
//char **allKeys;
char allKeys[4096][7];
char cipherText[]="UHLEQFVTCPPWMPDOREERRIODIWLTUAREREHTAAFBAENO"
"FERAEYHYOZIPVCYOSFKOYTESGHQPOTQNMDAPSTMGYIBO"
"QRPFESTTLPAAFEEUHTSBFTNKTSVVSUIWTOGYSTEFMHFRO"
"WOFZHERERPEUHTSBFTNKTSLHTUOTDLHTLPHSYQTGGVT";
char line[16];
long dscore[676];
long long totalScore = 0;
char *bigramPattern[676];
char key[] = "0123"; // considering length = 6, total combinations = 4^6 = 4096
// int rotationKeyLength = 0;
char originalAlphabet[] ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
double score=0;
int scoreSet=0; // 1 initial score is set and 0 not set
char *originalMessage;
char pairUW[]="UW";
char pairLA[]="LA";
char pairET[]="ET";
char rotate[6][5]={"????","????","????","????","????","????"};//{"UW??","LA??","ET??","????","????","????"}; // taken as anti clock wise move backword
char rotate1[2]={'?','?'};
int keyLength = 6;
/*************************************************************
GLOBAL VARIABLES END
*************************************************************/
void swap (char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
int checkForKey(char (*rotate)[5], char * hpair)
{
int k;
char *m, *n;
for(k = 0; k < 6; k++) // for 6 rotat key - combination
{
m = strchr(rotate[k], hpair[0]);
n = strchr(rotate[k], hpair[1]);
if(m != NULL && n != NULL)
{
return 1;
}
}
return 0;
}
long searchForScore(char txt[], char* pattern[], long* dscore)
{
int i;
for (i = 0; i < 676; i++)
{
if(strcmp(pattern[i], txt) == 0)
{
return dscore[i];
}
}
return 0;
}
double getScore(char *text, long long total, char* bPattern[], long * dscore)
{
long textLen=0, j;
int i;
char ptrn[3];
long double prob;
double intscore=0;
textLen = strlen(text);
for( i = 0; i < textLen; i++)
{
if( i + 1 < textLen)
{
ptrn[0] = text[i];
ptrn[1] = text[i+1];
ptrn[2] = '\0';
//printf("%s",ptrn);
j= searchForScore(ptrn, bPattern, dscore);
if(j != 0)
{
prob = 0;
prob =((double)j)/total;
intscore = intscore + (log10(prob));
}
}
}
return intscore;
}
void setOriginalMessage(char *dMessage)
{
int len = strlen(cipherText);
int i = 0;
originalMessage = NULL;
originalMessage = (char *)malloc((strlen(cipherText)) + 1) ;
for ( i=0; i < len; i++ )
{
originalMessage[i]=dMessage[i];
}
originalMessage[len]='\0';
}
int fillValidateRotateKey(char (*rotate)[5], char *alphabets)
{
//set up the key
int i, k , t = 0;
int flag = 0;
for(i = 0; i < 6; i++)
{
for(k = 0; k < 4; k++)
{
rotate[i][k] = alphabets[t];
t++;
}
rotate[i][k]='\0';
}
//Now match the key group if that map our criteria
if(checkForKey(rotate, pairUW) == 1 && checkForKey(rotate, pairLA) == 1 && checkForKey(rotate, pairET) == 1)
{
return 1;
} else {
return 0;
}
}
int identifyDefaultKey(char (*rotate)[5],char * hpair)
{
int k, key = 0;
int flag = 0;
char *m, *p;
for(k = 0; k < 6; k++)
{
m = strchr(rotate[k], hpair[0]); // positon of u
p = strchr(rotate[k], hpair[1]); // position of w
if(m != NULL && p != NULL)
{
if(p > m)
{
key = p - m;
}
else
{
key = 4 - (m - p);
}
break;
}
}
return 4-key; // why 4 - key? shouldn't it be key?
}
char* decryptPigPen(char (*rotate)[5],char (*rotate1),char key[5], char* cipherText, int keyLength)
{
char oMessage[177];
int i = 0, k = 0, pos = 0, ky, newPos, flag;
unsigned int msgLength = strlen(cipherText);
int ch;
char *m;
int txtLen = strlen(cipherText);
for(i = 0; i < txtLen; i++) // for each text character
{
ch = cipherText[i];
flag = 0;
for(k = 0; k < 6; k++) // for 6 rotat key - combination
{
m = strchr(rotate[k], ch);
if(m != NULL)
{
flag = 1;
pos = m - rotate[k]; // get the position of the character
ky = key[i % keyLength];
newPos = pos-ky;
if(newPos == 0)
{
oMessage[i] = rotate[k][newPos];
}
else
{
if(newPos > 0)
{
oMessage[i] = rotate[k][newPos];
}
else
{
oMessage[i] = rotate[k][ 4 + newPos];
}
}
//printf("char %c" ,*m);
break;
}
}
if(flag == 0 )
{
for(k = 0; k < 2; k++)
{
if(ch == rotate1[k])
oMessage[i] = ch;
}
}
m = NULL;
}
oMessage[msgLength] = '\0';
return oMessage;
}
// once the key is set up validate the score
void verifyThePermutation(char *alpha,char **keyPermuted)
{
int flag = 0;
int k = 0;
int i;
FILE *fptxt;
//int key[]={0,0,0,0,0,0};
//char Rotat[6][5]; // taken as anti clock wise
//char Rotat1[2] ; //={'E','N'};9
double parentScore;
char *dMessage;
char tempMsg[177];
flag = fillValidateRotateKey(rotate, alpha); // validating that pair are in correct group
rotate1[0]= alpha[24]; //18
rotate1[1]= alpha[25]; //19
if(flag == 1)
{
key[0] = identifyDefaultKey(rotate, pairUW); //UW
key[1] = 0; // for H
key[2] = identifyDefaultKey(rotate, pairLA); // LA
key[3] = identifyDefaultKey(rotate, pairET); //ET
for(k = 0; k < 16; k++) // 64 for key length of 7 else 16 for key length of 6
{
key[4] = keyPermuted[k][2] - 48; //k[1]
key[5] = keyPermuted[k][3] - 48; //2
//key[6]=KeyPer[k][3]-48;
// veryfy the score print the best score for that combination
//Get the decoded message once with the current key parrtern
// then calulate the score
// verify the score and repeat the iteration with other pattern
// Decrypt using standard approach
dMessage = decryptPigPen(rotate, rotate1, key, cipherText, keyLength); // move back in the key group to get the correct key
for ( i = 0; i < 177; i++ )
{
tempMsg[i] = dMessage[i];
//free(dMessage);
}
parentScore = getScore(tempMsg, totalScore, bigramPattern, dscore);
if(score == 0 && scoreSet == 0)
{
score = parentScore;
scoreSet = 1; // initial round of score is set
free(originalMessage);
setOriginalMessage(tempMsg);
}
if(score < parentScore)
{
score=parentScore;
free(originalMessage);
setOriginalMessage(tempMsg);
}
printf("\n Key : %d%d%d%d%d%d --- Score : %lf \n", key[0], key[1], key[2], key[3], key[4], key[5], score);
printf("\n Rotation Key %s - %s - %s - %s - %s - %s - %c - %c\n", rotate[0], rotate[1], rotate[2], rotate[3], rotate[4], rotate[5], rotate1[0], rotate1[1]);
printf(" Original message %s \n" , originalMessage);
printf("--------------------------- \n");
//text to file
fptxt=fopen("PigPen.txt","a+");
fprintf( fptxt, "%d%d%d%d%d%d Score : %lf \n", key[0], key[1], key[2], key[3], key[4], key[5], score);
fprintf(fptxt,"%s%s%s%s%s%s", rotate[0], rotate[1], rotate[2], rotate[3], rotate[4], rotate[5]);
fprintf(fptxt,"%c%c \n", rotate1[0], rotate1[1]);
fprintf(fptxt," Original message %s \n", originalMessage);
fprintf(fptxt,"\n--------------------------- %c",'\n');
fflush(stdin);
fclose(fptxt);
}
}
//free(dMessage);
return;
}
void permuteAlphabet(char *alpha, int i, int n,char **keyPermuted)
{
int j;
if (i == n)
{
printf("Verifying with %s\n", alpha);
verifyThePermutation(alpha, keyPermuted); // this is the calling point of the function
}
else
{
for (j = i; j <= n; j++)
{
swap((alpha + i), ( alpha + j));
permuteAlphabet(alpha, i + 1, n, keyPermuted);
swap((alpha + i), (alpha + j)); //backtrack
}
}
}
void knuthShuffle(char *alpha, int size)
{
int i;
int index;
char ch;
if (size == 0 || size == 1)
return;
srand(time(NULL));
for (i = size - 2; i >= 0; i--)
{
index = rand() % (i+1);
//swap(&alpha[index], &alpha[i]);
ch = alpha[index];
alpha[index] = alpha[i];
alpha[i] = ch;
}
}
void allKeyCombinations(char *input, char *output, int n, int i, int k){
int j;
if (i == k){
//printf("%d:\t%c%c%c%c%c%c", count++, output[0], output[1], output[2], output[3], output[4], output[5]);
// printf("%s", *output);
// output[7] = '\0';
//allKeys[count] = (char*)malloc( sizeof(char) * 7);
strcpy(allKeys[count], output);
count++;
printf("%d:\t%s", count, allKeys[count-1]);
printf("\n");
}
else{
for(j = 0; j < n; j++){
output[i] = input[j];
output[i + 1] = '\0';
allKeyCombinations(input, output, n, (i+1), k);
}
}
}
int main(){
int i = 0, n, k = 6, length;
char input[] = {'0','1', '2', '3'}, output[7];
char *p;
char *fileName = "english_bigrams.txt";
FILE *fp = fopen( fileName, "r");
n = sizeof(input)/sizeof(input[0]);
//allKeys = (char **)malloc(sizeof(char) * 4096);
printf("Creating all key combinations...\n");
allKeyCombinations(input, output, n, i ,k);
printf("Done creating all key combinations...\n");
if ( fp == NULL ) {
printf("File not found. Program terminated.\n");
getch();
fclose(fp);
exit(0);
}
printf("Reading file %s\n", fileName);
for(i = 0; i < 676; i++)
{
fscanf(fp, " %s %d\n", line, &dscore[i]);
totalScore += dscore[i];
length = strlen(line);
p = (char *)malloc( length + 1 );
strcpy( p, line );
bigramPattern[i] = p;
}
fclose(fp);
printf("Finished reading %s\n", fileName);
while( 1 ) {
knuthShuffle(originalAlphabet, sizeof(originalAlphabet));
verifyThePermutation(originalAlphabet, allKeys);
}
// permuteAlphabet(originalAlphabet, 0, 25, allKeys);
getch();
return(0);
}