-
Notifications
You must be signed in to change notification settings - Fork 1
/
k4-five.c
92 lines (78 loc) · 2.02 KB
/
k4-five.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
// K4 analysis
// number known plaintext from A-Z alphabet, corresponding ciphertext from reversed Kryptos alphabet
//
// calculate cipher minus plain, modulo 26
// count the number which are multiples of 5 - the five() function
// in original K4 ciphertext, this is 13 out of 24
// measure how common this is -- around 1 in 1,468
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
int rrand(int m)
{
return (int)((double)m * ( lrand48() / (RAND_MAX+1.0) ));
}
#define BYTE(X) ((unsigned char *)(X))
void
shuffle (void *obj, size_t nmemb, size_t size)
{
void *temp = malloc (size);
size_t n = nmemb;
while (n > 1)
{
size_t k = rrand (n--);
memcpy (temp, BYTE (obj) + n * size, size);
memcpy (BYTE (obj) + n * size, BYTE (obj) + k * size, size);
memcpy (BYTE (obj) + k * size, temp, size);
}
free (temp);
}
int
five (char *t)
{
int x = 0;
int i;
char k[] = "ZXWVUQNMLJIHGFEDCBASOTPYRK";
// loc 21-34 and 63-73
char p1[] = "EASTNORTHEAST";
char p2[] = "BERLINCLOCK";
for (i = 21; i <= 33; i++)
{
int a = strchr(k,t[i]) - k;
int p = p1[i-21] - 'A';
int b = (52+a-p)%26;
if (b % 5 == 0) x++;
}
for (i = 63; i <= 73; i++)
{
int a = strchr(k,t[i]) - k;
int p = p2[i-63] - 'A';
int b = (52+a-p)% 26;
if (b % 5 == 0) x++;
}
return x;
}
int
main (int argc, char **argv)
{
long i, pstat2, pcount = 0, co = 1e9;
char k4[] =
"OBKRUOXOGHULBSOLIFBBWFLRVQQPRNGKSSOTWTQSJQSSEKZZWATJKLUDIAWINFBNYPVTTMZFPKWGDKZXTJCDIGKUHUAUEKCAR";
char k4_perm[98];
struct timeval time;
gettimeofday (&time, NULL);
srand48 ((time.tv_sec * 1000) + (time.tv_usec / 1000));
strcpy (k4_perm, k4);
pstat2 = five (k4);
printf("%d\n",pstat2); fflush(stdout);
pcount = 0;
for (i = 0; i < co; i++)
{
shuffle (k4_perm,97,1);
if (five (k4_perm) >= pstat2)
pcount++;
}
printf ("%d\n", co/pcount);
return 0;
}