-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquagmire.c
2565 lines (2046 loc) · 75.3 KB
/
quagmire.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Polyalphabetic cipher solver
//
// A stochastic, shotgun-restarted hill climber with backtracking for solving
// Vigenere, Beaufort, and Quagmire I - IV with variants.
// Written by Sam Blake, started 14 July 2023.
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
// TODO: - remove decryption from state_score.
// Reference for n-gram data: http://practicalcryptography.com/cryptanalysis/letter-frequencies-various-languages/english-letter-frequencies/
#include "quagmire.h"
/* Program syntax:
$ ./quagmire \
-nhillclimbs /number of hillclimbing steps/ \
-nrestarts /number of restarts/ \
-type /cipher type (0, 1, 2, 3, 4, or 5)/ \
-cipher /ciphertext file/ \
-crib /crib file/ \
-ngramsize /n-gram size in n-gram statistics file/ \
-ngramfile /n-gram statistics file/ \
-maxkeywordlen /max length of the keyword/ \
-maxcyclewordlen /max length of the cycleword/ \
-plaintextkeywordlen /user defined length of the plaintext keyword/ \
-ciphertextkeywordlen /user defined length of the ciphertext keyword/ \
-cyclewordlen /user defined length of the cycleword/ \
-nsigmathreshold /n sigma threshold for candidate keyword length/ \
-backtrackprob /probability of backtracking to the best
solution instead of a random initial solution/ \
-keywordpermprob /probability of permuting the keyword instead of the cycleword/ \
-slipprob /probability of slipping to a worse score/ \
-iocthreshold /lower limit for ioc/ \
-dictionary /dictionary file, a text file containing one word per line/ \
-weightngram /weight used in the hillclimber score for the ngram score/ \
-weightcrib /weight used in the hillclimber score for the crib matches/ \
-weightioc /weight used in the hillclimber score for the IoC/ \
-weightentropy /weight used in the hillclimber score for the plaintext entropy/ \
-verbose
Notes:
/quagmire cipher type (0,1,2,3, or 4)/ -- type 0 is a Vigenere cipher, then 1-4 are Quagmire types
1 to 4 as defined by the ACA (https://www.cryptogram.org/resource-area/cipher-types/), type 5
is the Beaufort cipher as defined by the ACA (https://www.cryptogram.org/downloads/aca.info/ciphers/Beaufort.pdf)
/ciphertext file/ -- the entire cipher should be on the first line of the file. Subsequent
lines will not be read.
/crib file/ -- uses "_" for unknown chars. Just a single line of the same length
as the ciphers contained in the cipher file. For the Kryptos K4 cipher (assuming Sanborn has not
made any enciphering and/or spelling and/or positional mistakes) it should contain
_____________________EASTNORTHEAST_____________________________BERLINCLOCK_______________________
Performance:
This program is designed for attacks on the final unsolved Kryptos cipher (K4), which is only of
length 97. For longer ciphers a far better approach is to use frequency analysis on each simple
substitution ciphers (once the period has been estimated). Furthermore, if we knew for certain that
the cribs given for K4 were correct we could make additional performance improvements.
*/
int main(int argc, char **argv) {
int i, j, k, cipher_type = 3, cipher_len, cycleword_len, ngram_size = 0,
ciphertext_keyword_len = 5, plaintext_keyword_len = 5, ciphertext_max_keyword_len = 12,
min_keyword_len = 5, plaintext_max_keyword_len = 12, max_cycleword_len = 20, n_restarts = 1,
n_cycleword_lengths, n_hill_climbs = 1000, n_cribs, best_cycleword_length,
best_plaintext_keyword_length, best_ciphertext_keyword_length, n_words_found,
cipher_indices[MAX_CIPHER_LENGTH], crib_positions[MAX_CIPHER_LENGTH],
crib_indices[MAX_CIPHER_LENGTH], cycleword_lengths[MAX_CIPHER_LENGTH],
decrypted[MAX_CIPHER_LENGTH], best_decrypted[MAX_CIPHER_LENGTH],
plaintext_keyword[ALPHABET_SIZE], ciphertext_keyword[ALPHABET_SIZE], cycleword[ALPHABET_SIZE],
best_plaintext_keyword[ALPHABET_SIZE], best_ciphertext_keyword[ALPHABET_SIZE], best_cycleword[ALPHABET_SIZE];
double n_sigma_threshold = 1., ioc_threshold = 0.047, backtracking_probability = 0.01,
keyword_permutation_probability = 0.01, slip_probability = 0.0005, score, best_score;
float weight_ngram = 12., weight_crib = 36., weight_ioc = 1., weight_entropy = 1.;
char ciphertext_file[MAX_FILENAME_LEN], crib_file[MAX_FILENAME_LEN], dictionary_file[MAX_FILENAME_LEN],
ngram_file[MAX_FILENAME_LEN], ciphertext[MAX_CIPHER_LENGTH],
cribtext[MAX_CIPHER_LENGTH];
bool verbose = false, cipher_present = false, crib_present = false, plaintext_keyword_len_present = false,
cycleword_len_present = false, ciphertext_keyword_len_present = false, dictionary_present_p = false,
variant = false, beaufort = false;
FILE *fp;
float *ngram_data;
// Read command line args.
for(i = 1; i < argc; i++) {
if (strcmp(argv[i], "-type") == 0) {
cipher_type = atoi(argv[++i]);
printf("\n-type %d", cipher_type);
} else if (strcmp(argv[i], "-cipher") == 0) {
cipher_present = true;
strcpy(ciphertext_file, argv[++i]);
printf("\n-cipher %s", ciphertext_file);
} else if (strcmp(argv[i], "-crib") == 0) {
crib_present = true;
strcpy(crib_file, argv[++i]);
printf("\n-crib %s", crib_file);
} else if (strcmp(argv[i], "-ngramsize") == 0) {
ngram_size = atoi(argv[++i]);
printf("\n-ngram_size %d", ngram_size);
} else if (strcmp(argv[i], "-ngramfile") == 0) {
strcpy(ngram_file, argv[++i]);
printf("\n-ngramfile %s", ngram_file);
} else if (strcmp(argv[i], "-maxkeywordlen") == 0) {
plaintext_keyword_len = atoi(argv[++i]);
ciphertext_keyword_len = plaintext_keyword_len;
printf("\n-maxkeywordlen %d", plaintext_keyword_len);
} else if (strcmp(argv[i], "-keywordlen") == 0) {
plaintext_keyword_len_present = true;
ciphertext_keyword_len_present = true;
plaintext_keyword_len = atoi(argv[++i]);
ciphertext_keyword_len = plaintext_keyword_len;
plaintext_max_keyword_len = max(plaintext_max_keyword_len, 1 + plaintext_keyword_len);
ciphertext_max_keyword_len = max(ciphertext_max_keyword_len, 1 + ciphertext_keyword_len);
min_keyword_len = plaintext_keyword_len;
printf("\n-keywordlen %d", plaintext_keyword_len);
} else if (strcmp(argv[i], "-plaintextkeywordlen") == 0) {
plaintext_keyword_len_present = true;
plaintext_keyword_len = atoi(argv[++i]);
plaintext_max_keyword_len = max(plaintext_max_keyword_len, 1 + plaintext_keyword_len);
min_keyword_len = plaintext_keyword_len;
printf("\n-plaintextkeywordlen %d", plaintext_keyword_len);
} else if (strcmp(argv[i], "-ciphertextkeywordlen") == 0) {
ciphertext_keyword_len_present = true;
ciphertext_keyword_len = atoi(argv[++i]);
ciphertext_max_keyword_len = max(ciphertext_max_keyword_len, 1 + ciphertext_keyword_len);
min_keyword_len = ciphertext_keyword_len;
printf("\n-ciphertextkeywordlen %d", ciphertext_keyword_len);
} else if (strcmp(argv[i], "-maxcyclewordlen") == 0) {
max_cycleword_len = atoi(argv[++i]);
printf("\n-maxcyclewordlen %d", max_cycleword_len);
} else if (strcmp(argv[i], "-cyclewordlen") == 0) {
cycleword_len_present = true;
cycleword_len = atoi(argv[++i]);
if (cycleword_len == 0) {
cycleword_len_present = false;
}
max_cycleword_len = max(max_cycleword_len, 1 + cycleword_len);
printf("\n-cyclewordlen %d", cycleword_len);
} else if (strcmp(argv[i], "-nsigmathreshold") == 0) {
n_sigma_threshold = atof(argv[++i]);
printf("\n-nsigmathreshold %.2f", n_sigma_threshold);
} else if (strcmp(argv[i], "-nlocal") == 0) {
// TODO: remove me and update all tests/scripts etc.
} else if (strcmp(argv[i], "-nhillclimbs") == 0) {
n_hill_climbs = atoi(argv[++i]);
printf("\n-nhillclimbs %d", n_hill_climbs);
} else if (strcmp(argv[i], "-nrestarts") == 0) {
n_restarts = atoi(argv[++i]);
printf("\n-nrestarts %d", n_restarts);
} else if (strcmp(argv[i], "-backtrackprob") == 0) {
backtracking_probability = atof(argv[++i]);
printf("\n-backtrackprob %.4f", backtracking_probability);
} else if (strcmp(argv[i], "-keywordpermprob") == 0) {
backtracking_probability = atof(argv[++i]);
printf("\n-keywordpermprob %.4f", keyword_permutation_probability);
} else if (strcmp(argv[i], "-slipprob") == 0) {
slip_probability = atof(argv[++i]);
printf("\n-slipprob %.4f", slip_probability);
} else if (strcmp(argv[i], "-iocthreshold") == 0) {
ioc_threshold = atof(argv[++i]);
printf("\n-iocthreshold %.4f", ioc_threshold);
} else if (strcmp(argv[i], "-dictionary") == 0 || strcmp(argv[i], "-dict") == 0) {
dictionary_present_p = true;
strcpy(dictionary_file, argv[++i]);
printf("\n-dictionary %s", dictionary_file);
} else if (strcmp(argv[i], "-weightngram") == 0) {
weight_ngram = atof(argv[++i]);
printf("\n-weightngram %.4f", weight_ngram);
} else if (strcmp(argv[i], "-weightcrib") == 0) {
weight_crib = atof(argv[++i]);
printf("\n-weightcrib %.4f", weight_crib);
} else if (strcmp(argv[i], "-weightioc") == 0) {
weight_ioc = atof(argv[++i]);
printf("\n-weightioc %.4f", weight_ioc);
} else if (strcmp(argv[i], "-weightentropy") == 0) {
weight_entropy = atof(argv[++i]);
printf("\n-weightentropy %.4f", weight_entropy);
} else if (strcmp(argv[i], "-variant") == 0) {
variant = true;
printf("\n-variant");
} else if (strcmp(argv[i], "-verbose") == 0) {
verbose = true;
printf("\n-verbose ");
} else {
printf("\n\nERROR: unknown arg '%s'\n\n", argv[i]);
return 0;
}
}
printf("\n\n");
if (cipher_type == BEAUFORT) {
beaufort = true;
}
// Print cipher type.
char variant_display[10], variant_str[] = "variant";
if (variant) {
strcpy(variant_display, variant_str);
} else {
strcpy(variant_display, "");
}
if (cipher_type == VIGENERE) {
printf("\n\nSolving a %s Vigenere cipher.\n\n", variant_display);
} else if (cipher_type == BEAUFORT) {
printf("\n\nSolving a %s Beaufort cipher.\n\n", variant_display);
} else if (cipher_type == QUAGMIRE_1) {
printf("\n\nSolving a %s Quagmire I cipher.\n\n", variant_display);
} else if (cipher_type == QUAGMIRE_2) {
printf("\n\nSolving a %s Quagmire II cipher.\n\n", variant_display);
} else if (cipher_type == QUAGMIRE_3) {
printf("\n\nSolving a %s Quagmire III cipher.\n\n", variant_display);
} else if (cipher_type == QUAGMIRE_4) {
printf("\n\nSolving a %s Quagmire IV cipher.\n\n", variant_display);
}
// Sense check command line inputs.
if (! cipher_present) {
printf("\n\nERROR: cipher file not present.\n\n");
return 0;
}
if (ngram_size == 0) {
printf("\n\nERROR: -ngramsize missing.\n\n");
return 0;
}
if (! file_exists(ciphertext_file)) {
printf("\nERROR: missing file '%s'\n", ciphertext_file);
return 0;
}
if (! file_exists(ngram_file)) {
printf("\nERROR: missing file '%s'\n", ngram_file);
return 0;
}
if (crib_present && ! file_exists(crib_file)) {
printf("\nERROR: missing file '%s'\n", crib_file);
return 0;
}
// Check if OxfordEnglishWords.txt is present.
char oxford_english_words[] = "OxfordEnglishWords.txt";
if (! dictionary_present_p && file_exists(oxford_english_words)) {
dictionary_present_p = true;
strcpy(dictionary_file, oxford_english_words);
if (verbose) {
printf("\ndictionary = %s\n\n", dictionary_file);
}
}
// Read ciphertext. Only the first line of the ciphertext file is read (leaving
// further lines for explanation/derivation etc.)
fp = fopen(ciphertext_file, "r");
fscanf(fp, "%s", ciphertext);
fclose(fp);
if (verbose) {
printf("ciphertext = \n\'%s\'\n\n", ciphertext);
}
cipher_len = (int) strlen(ciphertext);
// Read crib.
if (file_exists(crib_file)) {
fp = fopen(crib_file, "r");
fscanf(fp, "%s", cribtext);
fclose(fp);
if (verbose) {
printf("cribtext = \n\'%s\'\n\n", cribtext);
}
// Check ciphertext and cribtext are of the same length.
if (cipher_len != strlen(cribtext)) {
printf("\n\nERROR: strlen(ciphertext) = %d, strlen(cribtext) = %lu.\n\n",
cipher_len, strlen(cribtext));
return 0;
}
// Extract crib positions and corresponding plaintext.
if (verbose) {
printf("\ncrib indices = \n\n");
}
n_cribs = 0;
for (i = 0; i < cipher_len; i++) {
if (cribtext[i] != '_') {
crib_positions[n_cribs] = i;
crib_indices[n_cribs] = cribtext[i] - 'A';
n_cribs++;
if (verbose) {
printf("%d, %c, %d\n", i, cribtext[i], cribtext[i] - 'A');
}
}
}
if (verbose) {
printf("\n");
}
} else {
// No cribs present.
n_cribs = 0;
}
// Compute ciphertext indices. A -> 0, B -> 1, ..., Z -> 25 (Assuming ALPHABET_SIZE = 26)
ord(ciphertext, cipher_indices);
// Estimate cycleword length.
estimate_cycleword_lengths(
cipher_indices,
cipher_len,
max_cycleword_len,
n_sigma_threshold,
ioc_threshold,
&n_cycleword_lengths,
cycleword_lengths,
verbose);
// Load n-gram file.
ngram_data = load_ngrams(ngram_file, ngram_size, verbose);
// Set random seed.
srand(time(NULL));
// User-defined cycleword length.
if (cycleword_len_present) {
n_cycleword_lengths = 1;
cycleword_lengths[0] = cycleword_len;
}
// Vigenere cipher case.
if (cipher_type == VIGENERE) {
min_keyword_len = 1;
}
// Beaufort cipher case.
if (cipher_type == BEAUFORT) {
min_keyword_len = 1;
plaintext_max_keyword_len = 2;
plaintext_max_keyword_len = 2;
}
// For each cycleword length and keyword length combination, run the 'shotgun' hill-climber.
best_score = 0.;
for (i = 0; i < n_cycleword_lengths; i++) {
for (j = min(min_keyword_len, plaintext_keyword_len); j < plaintext_max_keyword_len; j++) {
for (k = min(min_keyword_len, ciphertext_keyword_len); k < ciphertext_max_keyword_len; k++) {
// printf("i,j,k = %d, %d, %d", i, j, k);
// User-specified plaintext keyword length.
if (plaintext_keyword_len_present && j != plaintext_keyword_len) {
continue ;
}
// User-specified ciphertext keyword length.
if (ciphertext_keyword_len_present && k != ciphertext_keyword_len) {
continue ;
}
// Both Vigenere and Quagmire 3 use the same ciphertext and plaintext keywords.
if ((cipher_type == VIGENERE || cipher_type == QUAGMIRE_3) && j != k) continue ;
// Vigenere cipher uses same ciphertext, plaintext, and cycleword lengths.
if (cipher_type == VIGENERE && ! (cycleword_lengths[i] == j && cycleword_lengths[i] == k)) continue ;
// Beaufort cipher uses a plaintext and ciphertext keyword of 'A'.
if (cipher_type == BEAUFORT && ! (j == 1 && k == 1)) continue ;
if (verbose) {
printf("\nplaintext, ciphertext, cycleword lengths = %d, %d, %d\n", j, k, cycleword_lengths[i]);
}
// Check the cipher satisfies the cribs for the cycleword length.
if (! cribs_satisfied_p(cipher_indices, cipher_len, crib_indices, crib_positions, n_cribs, cycleword_lengths[i], verbose)) {
if (verbose) {
printf("\n\nCiphertext does not satisfy the cribs for cycleword length %d. \n\n", cycleword_lengths[i]);
}
#if CRIB_CHECK
continue ;
#endif
}
// Run the hill-climber.
score = quagmire_shotgun_hill_climber(
cipher_type,
cipher_indices,
cipher_len,
crib_indices,
crib_positions,
n_cribs,
cycleword_lengths[i],
j,
k,
n_hill_climbs,
n_restarts,
ngram_data,
ngram_size,
decrypted,
plaintext_keyword,
ciphertext_keyword,
cycleword,
backtracking_probability,
keyword_permutation_probability,
slip_probability,
weight_ngram,
weight_crib,
weight_ioc,
weight_entropy,
variant,
beaufort,
verbose);
// Keep the best solution.
if (score > best_score) {
best_score = score;
best_cycleword_length = cycleword_lengths[i];
best_plaintext_keyword_length = j;
best_ciphertext_keyword_length = k;
vec_copy(decrypted, best_decrypted, cipher_len);
vec_copy(plaintext_keyword, best_plaintext_keyword, ALPHABET_SIZE);
vec_copy(ciphertext_keyword, best_ciphertext_keyword, ALPHABET_SIZE);
vec_copy(cycleword, best_cycleword, ALPHABET_SIZE);
}
}
}
}
// Find dictionary words.
char plaintext_string[MAX_CIPHER_LENGTH];
for (int i = 0; i < cipher_len; i++) {
plaintext_string[i] = best_decrypted[i] + 'A';
}
plaintext_string[cipher_len] = '\0';
#if DICTIONARY
if (dictionary_present_p) {
char **dict = NULL;
int n_dict_words, max_dict_word_len;
load_dictionary(dictionary_file, &dict, &n_dict_words, &max_dict_word_len, verbose);
if (verbose) {
printf("\nDictionary words = \n");
}
n_words_found = find_dictionary_words(plaintext_string, dict, n_dict_words, max_dict_word_len);
printf("\n%d words found.\n", n_words_found);
free_dictionary(dict, n_dict_words);
}
#endif
printf("\n\n%.2f\n", best_score);
if (dictionary_present_p) {
printf("%d\n", n_words_found);
}
print_text(cipher_indices, cipher_len);
printf("\n");
print_text(best_plaintext_keyword, ALPHABET_SIZE);
printf("\n");
print_text(best_ciphertext_keyword, ALPHABET_SIZE);
printf("\n");
print_text(best_cycleword, best_cycleword_length);
printf("\n");
print_text(best_decrypted, cipher_len);
printf("\n\n");
// K4-specific checks for BERLIN, CLOCK, EAST, NORTH, BERLINCLOCK and EASTNORTHEAST.
#if KRYPTOS
bool berlin_present = false, clock_present = false, east_present = false, north_present = false,
berlinclock_present = false, eastnortheast_present = false;
if (strstr(plaintext_string, "BERLIN") != NULL) {
berlin_present = true;
printf("**** \'BERLIN\' PRESENT!!! ****\n");
}
if (strstr(plaintext_string, "CLOCK") != NULL) {
clock_present = true;
printf("**** \'CLOCK\' PRESENT!!! ****\n");
}
if (strstr(plaintext_string, "EAST") != NULL) {
east_present = true;
printf("**** \'EAST\' PRESENT!!! ****\n");
}
if (strstr(plaintext_string, "NORTH") != NULL) {
north_present = true;
printf("**** \'NORTH\' PRESENT!!! ****\n");
}
if (strstr(plaintext_string, "BERLINCLOCK") != NULL) {
berlinclock_present = true;
for (i = 0; i < 1000; i++) {
printf("**** \'BERLINCLOCK\' PRESENT!!! ****");
}
}
if (strstr(plaintext_string, "EASTNORTHEAST") != NULL) {
eastnortheast_present = true;
for (i = 0; i < 1000; i++) {
printf("**** \'EASTNORTHEAST\' PRESENT!!! ****");
}
}
printf("\n\n");
#endif
// Single line summary of results for subsequent filtering and analysis.
if (dictionary_present_p) {
printf("\n\n>>> %.2f, %d, %d, %s, ", best_score, n_words_found, cipher_type, ciphertext_file);
} else {
printf("\n\n>>> %.2f, %d, %s, ", best_score, cipher_type, ciphertext_file);
}
print_text(cipher_indices, cipher_len);
printf(", ");
print_text(best_plaintext_keyword, ALPHABET_SIZE);
printf(", ");
print_text(best_ciphertext_keyword, ALPHABET_SIZE);
printf(", ");
print_text(best_cycleword, best_cycleword_length);
printf(", ");
print_text(best_decrypted, cipher_len);
#if KRYPTOS
if (berlin_present) {
printf(", BERLIN");
}
if (clock_present) {
printf(", CLOCK");
}
if (east_present) {
printf(", EAST");
}
if (north_present) {
printf(", NORTH");
}
if (berlinclock_present) {
printf(", BERLINCLOCK");
}
if (eastnortheast_present) {
printf(", EASTNORTHEAST");
}
printf("\n\n");
#endif
free(ngram_data);
return 1;
}
// Slippery stochastic shotgun restarted hill climber for Quagmire ciphers.
double quagmire_shotgun_hill_climber(
int cipher_type,
int cipher_indices[], int cipher_len,
int crib_indices[], int crib_positions[], int n_cribs,
int cycleword_len, int plaintext_keyword_len, int ciphertext_keyword_len,
int n_hill_climbs, int n_restarts,
float *ngram_data, int ngram_size,
int decrypted[MAX_CIPHER_LENGTH], int plaintext_keyword[ALPHABET_SIZE],
int ciphertext_keyword[ALPHABET_SIZE], int cycleword[ALPHABET_SIZE],
double backtracking_probability, double keyword_permutation_probability, double slip_probability,
float weight_ngram, float weight_crib, float weight_ioc, float weight_entropy,
bool variant, bool beaufort, bool verbose) {
int i, j, n, indx, n_iterations, n_backtracks, n_explore, n_contradictions,
local_plaintext_keyword_state[ALPHABET_SIZE], current_plaintext_keyword_state[ALPHABET_SIZE],
local_ciphertext_keyword_state[ALPHABET_SIZE], current_ciphertext_keyword_state[ALPHABET_SIZE],
best_plaintext_keyword_state[ALPHABET_SIZE], best_ciphertext_keyword_state[ALPHABET_SIZE],
local_cycleword_state[MAX_CYCLEWORD_LEN], current_cycleword_state[MAX_CYCLEWORD_LEN],
best_cycleword_state[MAX_CYCLEWORD_LEN];
double start_time, elapsed, n_iter_per_sec, best_score, local_score, current_score, ioc, chi,
entropy_score;
bool perturbate_keyword_p, contradiction;
if (cipher_type == VIGENERE) {
cycleword_len = ALPHABET_SIZE;
}
n_iterations = 0;
n_backtracks = 0;
n_explore = 0;
n_contradictions = 0;
start_time = clock();
best_score = 0.;
for (n = 0; n < n_restarts; n++) {
if (best_score > 0. && frand() < backtracking_probability) {
// Backtrack to best state.
n_backtracks += 1;
current_score = best_score;
vec_copy(best_plaintext_keyword_state, current_plaintext_keyword_state, ALPHABET_SIZE);
vec_copy(best_ciphertext_keyword_state, current_ciphertext_keyword_state, ALPHABET_SIZE);
vec_copy(best_cycleword_state, current_cycleword_state, cycleword_len);
} else {
// Initialise random state.
switch (cipher_type) {
case VIGENERE:
random_keyword(current_plaintext_keyword_state, ALPHABET_SIZE, plaintext_keyword_len);
vec_copy(current_plaintext_keyword_state, current_ciphertext_keyword_state, ALPHABET_SIZE);
vec_copy(current_plaintext_keyword_state, current_cycleword_state, ALPHABET_SIZE);
break ;
case QUAGMIRE_1:
random_keyword(current_plaintext_keyword_state, ALPHABET_SIZE, plaintext_keyword_len);
straight_alphabet(current_ciphertext_keyword_state, ALPHABET_SIZE);
random_cycleword(current_cycleword_state, ALPHABET_SIZE, cycleword_len);
break ;
case QUAGMIRE_2:
straight_alphabet(current_plaintext_keyword_state, ALPHABET_SIZE);
random_keyword(current_ciphertext_keyword_state, ALPHABET_SIZE, ciphertext_keyword_len);
random_cycleword(current_cycleword_state, ALPHABET_SIZE, cycleword_len);
break ;
case QUAGMIRE_3:
random_keyword(current_plaintext_keyword_state, ALPHABET_SIZE, plaintext_keyword_len);
vec_copy(current_plaintext_keyword_state, current_ciphertext_keyword_state, ALPHABET_SIZE);
random_cycleword(current_cycleword_state, ALPHABET_SIZE, cycleword_len);
break ;
case QUAGMIRE_4:
random_keyword(current_plaintext_keyword_state, ALPHABET_SIZE, plaintext_keyword_len);
random_keyword(current_ciphertext_keyword_state, ALPHABET_SIZE, ciphertext_keyword_len);
random_cycleword(current_cycleword_state, ALPHABET_SIZE, cycleword_len);
break ;
case BEAUFORT:
plaintext_keyword_len = ALPHABET_SIZE;
ciphertext_keyword_len = ALPHABET_SIZE;
for (i = 0; i < ALPHABET_SIZE; i++) current_plaintext_keyword_state[i] = i;
vec_copy(current_plaintext_keyword_state, current_ciphertext_keyword_state, ALPHABET_SIZE);
random_cycleword(current_cycleword_state, ALPHABET_SIZE, cycleword_len);
break ;
}
current_score = state_score(cipher_indices, cipher_len,
crib_indices, crib_positions, n_cribs,
current_plaintext_keyword_state, current_ciphertext_keyword_state,
current_cycleword_state, cycleword_len,
variant, beaufort,
decrypted, ngram_data, ngram_size,
weight_ngram, weight_crib, weight_ioc, weight_entropy);
}
// The following are K4-specific hacks to manually set the ciphertext and plaintext keywords to KRYPTOS and/or KOMITET.
#if 0
current_plaintext_keyword_state[0] = 0;
current_plaintext_keyword_state[1] = 13;
current_plaintext_keyword_state[2] = 6;
current_plaintext_keyword_state[3] = 11;
current_plaintext_keyword_state[4] = 4;
current_plaintext_keyword_state[5] = 18;
current_plaintext_keyword_state[6] = 1;
current_plaintext_keyword_state[7] = 2;
current_plaintext_keyword_state[8] = 3;
current_plaintext_keyword_state[9] = 5;
current_plaintext_keyword_state[10] = 7;
current_plaintext_keyword_state[11] = 8;
current_plaintext_keyword_state[12] = 9;
current_plaintext_keyword_state[13] = 10;
current_plaintext_keyword_state[14] = 12;
current_plaintext_keyword_state[15] = 14;
current_plaintext_keyword_state[16] = 15;
current_plaintext_keyword_state[17] = 16;
current_plaintext_keyword_state[18] = 17;
current_plaintext_keyword_state[19] = 19;
current_plaintext_keyword_state[20] = 20;
current_plaintext_keyword_state[21] = 21;
current_plaintext_keyword_state[22] = 22;
current_plaintext_keyword_state[23] = 23;
current_plaintext_keyword_state[24] = 24;
current_plaintext_keyword_state[25] = 25;
if (cipher_type == VIGENERE || cipher_type == BEAUFORT || cipher_type == QUAGMIRE_3) {
vec_copy(current_plaintext_keyword_state, current_ciphertext_keyword_state, ALPHABET_SIZE);
}
#endif
#if 0
current_ciphertext_keyword_state[0] = 19;
current_ciphertext_keyword_state[1] = 8;
current_ciphertext_keyword_state[2] = 13;
current_ciphertext_keyword_state[3] = 3;
current_ciphertext_keyword_state[4] = 4;
current_ciphertext_keyword_state[5] = 17;
current_ciphertext_keyword_state[6] = 0;
current_ciphertext_keyword_state[7] = 1;
current_ciphertext_keyword_state[8] = 2;
current_ciphertext_keyword_state[9] = 5;
current_ciphertext_keyword_state[10] = 6;
current_ciphertext_keyword_state[11] = 7;
current_ciphertext_keyword_state[12] = 9;
current_ciphertext_keyword_state[13] = 10;
current_ciphertext_keyword_state[14] = 11;
current_ciphertext_keyword_state[15] = 12;
current_ciphertext_keyword_state[16] = 14;
current_ciphertext_keyword_state[17] = 15;
current_ciphertext_keyword_state[18] = 16;
current_ciphertext_keyword_state[19] = 18;
current_ciphertext_keyword_state[20] = 20;
current_ciphertext_keyword_state[21] = 21;
current_ciphertext_keyword_state[22] = 22;
current_ciphertext_keyword_state[23] = 23;
current_ciphertext_keyword_state[24] = 24;
current_ciphertext_keyword_state[25] = 25;
if (cipher_type == VIGENERE || cipher_type == BEAUFORT || cipher_type == QUAGMIRE_3) {
vec_copy(current_ciphertext_keyword_state, current_plaintext_keyword_state, ALPHABET_SIZE);
}
#endif
#if KOMITET_PT
// KOMITE[T]
current_plaintext_keyword_state[0] = 10;
current_plaintext_keyword_state[1] = 14;
current_plaintext_keyword_state[2] = 12;
current_plaintext_keyword_state[3] = 8;
current_plaintext_keyword_state[4] = 19;
current_plaintext_keyword_state[5] = 4;
current_plaintext_keyword_state[6] = 0;
current_plaintext_keyword_state[7] = 1;
current_plaintext_keyword_state[8] = 2;
current_plaintext_keyword_state[9] = 3;
current_plaintext_keyword_state[10] = 5;
current_plaintext_keyword_state[11] = 6;
current_plaintext_keyword_state[12] = 7;
current_plaintext_keyword_state[13] = 9;
current_plaintext_keyword_state[14] = 11;
current_plaintext_keyword_state[15] = 13;
current_plaintext_keyword_state[16] = 15;
current_plaintext_keyword_state[17] = 16;
current_plaintext_keyword_state[18] = 17;
current_plaintext_keyword_state[19] = 18;
current_plaintext_keyword_state[20] = 20;
current_plaintext_keyword_state[21] = 21;
current_plaintext_keyword_state[22] = 22;
current_plaintext_keyword_state[23] = 23;
current_plaintext_keyword_state[24] = 24;
current_plaintext_keyword_state[25] = 25;
if (cipher_type == VIGENERE || cipher_type == BEAUFORT || cipher_type == QUAGMIRE_3) {
vec_copy(current_plaintext_keyword_state, current_ciphertext_keyword_state, ALPHABET_SIZE);
}
#endif
#if KOMITET_CT
// KOMITE[T]
current_ciphertext_keyword_state[0] = 10;
current_ciphertext_keyword_state[1] = 14;
current_ciphertext_keyword_state[2] = 12;
current_ciphertext_keyword_state[3] = 8;
current_ciphertext_keyword_state[4] = 19;
current_ciphertext_keyword_state[5] = 4;
current_ciphertext_keyword_state[6] = 0;
current_ciphertext_keyword_state[7] = 1;
current_ciphertext_keyword_state[8] = 2;
current_ciphertext_keyword_state[9] = 3;
current_ciphertext_keyword_state[10] = 5;
current_ciphertext_keyword_state[11] = 6;
current_ciphertext_keyword_state[12] = 7;
current_ciphertext_keyword_state[13] = 9;
current_ciphertext_keyword_state[14] = 11;
current_ciphertext_keyword_state[15] = 13;
current_ciphertext_keyword_state[16] = 15;
current_ciphertext_keyword_state[17] = 16;
current_ciphertext_keyword_state[18] = 17;
current_ciphertext_keyword_state[19] = 18;
current_ciphertext_keyword_state[20] = 20;
current_ciphertext_keyword_state[21] = 21;
current_ciphertext_keyword_state[22] = 22;
current_ciphertext_keyword_state[23] = 23;
current_ciphertext_keyword_state[24] = 24;
current_ciphertext_keyword_state[25] = 25;
if (cipher_type == VIGENERE || cipher_type == BEAUFORT || cipher_type == QUAGMIRE_3) {
vec_copy(current_ciphertext_keyword_state, current_plaintext_keyword_state, ALPHABET_SIZE);
}
#endif
#if KRYPTOS_PT || KRYPTOS_PT_SCRAMBLE
// KRYPTOS
current_plaintext_keyword_state[0] = 10;
current_plaintext_keyword_state[1] = 17;
current_plaintext_keyword_state[2] = 24;
current_plaintext_keyword_state[3] = 15;
current_plaintext_keyword_state[4] = 19;
current_plaintext_keyword_state[5] = 14;
current_plaintext_keyword_state[6] = 18;
current_plaintext_keyword_state[7] = 0;
current_plaintext_keyword_state[8] = 1;
current_plaintext_keyword_state[9] = 2;
current_plaintext_keyword_state[10] = 3;
current_plaintext_keyword_state[11] = 4;
current_plaintext_keyword_state[12] = 5;
current_plaintext_keyword_state[13] = 6;
current_plaintext_keyword_state[14] = 7;
current_plaintext_keyword_state[15] = 8;
current_plaintext_keyword_state[16] = 9;
current_plaintext_keyword_state[17] = 11;
current_plaintext_keyword_state[18] = 12;
current_plaintext_keyword_state[19] = 13;
current_plaintext_keyword_state[20] = 16;
current_plaintext_keyword_state[21] = 20;
current_plaintext_keyword_state[22] = 21;
current_plaintext_keyword_state[23] = 22;
current_plaintext_keyword_state[24] = 23;
current_plaintext_keyword_state[25] = 25;
if (cipher_type == VIGENERE || cipher_type == BEAUFORT || cipher_type == QUAGMIRE_3) {
vec_copy(current_plaintext_keyword_state, current_ciphertext_keyword_state, ALPHABET_SIZE);
}
#endif
#if KRYPTOS_CT
// KRYPTOS
current_ciphertext_keyword_state[0] = 10;
current_ciphertext_keyword_state[1] = 17;
current_ciphertext_keyword_state[2] = 24;
current_ciphertext_keyword_state[3] = 15;
current_ciphertext_keyword_state[4] = 19;
current_ciphertext_keyword_state[5] = 14;
current_ciphertext_keyword_state[6] = 18;
current_ciphertext_keyword_state[7] = 0;
current_ciphertext_keyword_state[8] = 1;
current_ciphertext_keyword_state[9] = 2;
current_ciphertext_keyword_state[10] = 3;
current_ciphertext_keyword_state[11] = 4;
current_ciphertext_keyword_state[12] = 5;
current_ciphertext_keyword_state[13] = 6;
current_ciphertext_keyword_state[14] = 7;
current_ciphertext_keyword_state[15] = 8;
current_ciphertext_keyword_state[16] = 9;
current_ciphertext_keyword_state[17] = 11;
current_ciphertext_keyword_state[18] = 12;
current_ciphertext_keyword_state[19] = 13;
current_ciphertext_keyword_state[20] = 16;
current_ciphertext_keyword_state[21] = 20;
current_ciphertext_keyword_state[22] = 21;
current_ciphertext_keyword_state[23] = 22;
current_ciphertext_keyword_state[24] = 23;
current_ciphertext_keyword_state[25] = 25;
if (cipher_type == VIGENERE || cipher_type == BEAUFORT || cipher_type == QUAGMIRE_3) {
vec_copy(current_ciphertext_keyword_state, current_plaintext_keyword_state, ALPHABET_SIZE);
}
#endif
#if SOLUBLE_PT
// SOLUB[L]E
current_plaintext_keyword_state[0] = 18;
current_plaintext_keyword_state[1] = 14;
current_plaintext_keyword_state[2] = 11;
current_plaintext_keyword_state[3] = 20;
current_plaintext_keyword_state[4] = 1;
current_plaintext_keyword_state[5] = 4;
current_plaintext_keyword_state[6] = 0;
current_plaintext_keyword_state[7] = 2;
current_plaintext_keyword_state[8] = 3;
current_plaintext_keyword_state[9] = 5;
current_plaintext_keyword_state[10] = 6;
current_plaintext_keyword_state[11] = 7;
current_plaintext_keyword_state[12] = 8;
current_plaintext_keyword_state[13] = 9;
current_plaintext_keyword_state[14] = 10;
current_plaintext_keyword_state[15] = 12;
current_plaintext_keyword_state[16] = 13;
current_plaintext_keyword_state[17] = 15;
current_plaintext_keyword_state[18] = 16;
current_plaintext_keyword_state[19] = 17;
current_plaintext_keyword_state[20] = 19;
current_plaintext_keyword_state[21] = 21;
current_plaintext_keyword_state[22] = 22;
current_plaintext_keyword_state[23] = 23;
current_plaintext_keyword_state[24] = 24;
current_plaintext_keyword_state[25] = 25;
if (cipher_type == VIGENERE || cipher_type == BEAUFORT || cipher_type == QUAGMIRE_3) {
vec_copy(current_plaintext_keyword_state, current_ciphertext_keyword_state, ALPHABET_SIZE);
}
#endif
#if SOLUBLE_CT
// SOLUB[L]E
current_ciphertext_keyword_state[0] = 18;
current_ciphertext_keyword_state[1] = 14;
current_ciphertext_keyword_state[2] = 11;
current_ciphertext_keyword_state[3] = 20;
current_ciphertext_keyword_state[4] = 1;
current_ciphertext_keyword_state[5] = 4;
current_ciphertext_keyword_state[6] = 0;
current_ciphertext_keyword_state[7] = 2;
current_ciphertext_keyword_state[8] = 3;
current_ciphertext_keyword_state[9] = 5;
current_ciphertext_keyword_state[10] = 6;
current_ciphertext_keyword_state[11] = 7;
current_ciphertext_keyword_state[12] = 8;
current_ciphertext_keyword_state[13] = 9;
current_ciphertext_keyword_state[14] = 10;
current_ciphertext_keyword_state[15] = 12;
current_ciphertext_keyword_state[16] = 13;
current_ciphertext_keyword_state[17] = 15;
current_ciphertext_keyword_state[18] = 16;
current_ciphertext_keyword_state[19] = 17;
current_ciphertext_keyword_state[20] = 19;
current_ciphertext_keyword_state[21] = 21;
current_ciphertext_keyword_state[22] = 22;
current_ciphertext_keyword_state[23] = 23;
current_ciphertext_keyword_state[24] = 24;
current_ciphertext_keyword_state[25] = 25;
if (cipher_type == VIGENERE || cipher_type == BEAUFORT || cipher_type == QUAGMIRE_3) {
vec_copy(current_ciphertext_keyword_state, current_plaintext_keyword_state, ALPHABET_SIZE);
}
#endif
perturbate_keyword_p = true;
for (i = 0; i < n_hill_climbs; i++) {
n_iterations += 1;
// perturbate.
vec_copy(current_plaintext_keyword_state, local_plaintext_keyword_state, ALPHABET_SIZE);
vec_copy(current_ciphertext_keyword_state, local_ciphertext_keyword_state, ALPHABET_SIZE);
vec_copy(current_cycleword_state, local_cycleword_state, cycleword_len);
if (cipher_type != BEAUFORT && (perturbate_keyword_p || cipher_type == VIGENERE || frand() < keyword_permutation_probability)) {
switch (cipher_type) {
case VIGENERE:
perturbate_keyword(local_plaintext_keyword_state, ALPHABET_SIZE, plaintext_keyword_len);
vec_copy(local_plaintext_keyword_state, local_ciphertext_keyword_state, ALPHABET_SIZE);
vec_copy(local_plaintext_keyword_state, local_cycleword_state, ALPHABET_SIZE);
break ;
case QUAGMIRE_1:
perturbate_keyword(local_plaintext_keyword_state, ALPHABET_SIZE, plaintext_keyword_len);
break ;
case QUAGMIRE_2:
perturbate_keyword(local_ciphertext_keyword_state, ALPHABET_SIZE, ciphertext_keyword_len);
break ;
case QUAGMIRE_3:
perturbate_keyword(local_plaintext_keyword_state, ALPHABET_SIZE, plaintext_keyword_len);
vec_copy(local_plaintext_keyword_state, local_ciphertext_keyword_state, ALPHABET_SIZE);
break ;
case QUAGMIRE_4:
if (frand() < 0.5) {
perturbate_keyword(local_plaintext_keyword_state, ALPHABET_SIZE, plaintext_keyword_len);
} else {
perturbate_keyword(local_ciphertext_keyword_state, ALPHABET_SIZE, ciphertext_keyword_len);
}