-
Notifications
You must be signed in to change notification settings - Fork 7
/
analyze.c
201 lines (155 loc) · 3.72 KB
/
analyze.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
#include "analyze.h"
#include "constants.h"
#include "../tools/ifreq.h"
#include "../tools/xor.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
/*
* Loop around files in ctext folder in
* order to find the minimal common length
* of the ciphertexts.
*/
int get_min_ciphertext_len()
{
size_t min_ctext_size = 0;
DIR *d;
struct stat st;
struct dirent *dir_f;
char fn[FILENAME_MAX];
int len = strlen(ciphertext_folder);
strcpy(fn, ciphertext_folder);
fn[len++] = '/';
d = opendir(ciphertext_folder);
if (NULL != d)
{
while ((dir_f = readdir(d)) != NULL)
{
if (!strcmp(dir_f->d_name, ".") || !strcmp(dir_f->d_name, ".."))
continue;
strncpy(fn + len, dir_f->d_name, FILENAME_MAX - len);
stat(fn, &st);
if (!min_ctext_size)
min_ctext_size = st.st_size;
if (min_ctext_size > st.st_size)
min_ctext_size = st.st_size;
}
}
closedir(d);
return min_ctext_size;
}
/*
* Load every ciphertext present (truncated into their common minimal length) into memory
*/
int load_ciphertext_matrix(unsigned char ctext_mat[], unsigned int ctext_len)
{
FILE *fd;
DIR *d;
struct dirent *dir_f;
char fn[FILENAME_MAX];
int len = strlen(ciphertext_folder);
strcpy(fn, ciphertext_folder);
fn[len++] = '/';
d = opendir(ciphertext_folder);
if (NULL == d)
return 0x01;
while ((dir_f = readdir(d)) != NULL)
{
if (!strcmp(dir_f->d_name, ".") || !strcmp(dir_f->d_name, ".."))
continue;
strncpy(fn + len, dir_f->d_name, FILENAME_MAX - len);
fd = fopen(fn, "rb");
if( NULL == fd )
continue;
unsigned int j = atoi(dir_f->d_name);
fread(ctext_mat + j*ctext_len, sizeof(char), ctext_len, fd);
fclose(fd);
}
closedir(d);
return 0x00;
}
/*
* Like for challenge #04 and #06, detect the most common character,
* which is highly likely to be the space char.
*/
char detect_key_single_line(char *lencdata, size_t llen)
{
char ret_char;
int* tmp, i;
tmp = malloc(llen*sizeof(int));
if (NULL == tmp)
return 0x00;
for (i=0; i < llen; i++)
tmp[i] = lencdata[i];
ifreq( tmp, NULL, llen);
ret_char = tmp[0];
free(tmp);
return ret_char;
}
/*
* Analyze the ciphertexts files in order to
* decrypt automatically the content.
*/
int analyze_ciphertexts()
{
unsigned char *ctext_mat, *ctext_mat_t, *key_img, *plain;
size_t i,k, ctxtidx, charidx;
size_t ctext_len = get_min_ciphertext_len();
printf(" Min ciphertext length : %d bytes\n", (int) ctext_len);
ctext_mat = malloc((ctext_len*60)*sizeof(char));
if (NULL == ctext_mat)
return 0x01;
memset(ctext_mat, 0, (ctext_len*60)*sizeof(char));
load_ciphertext_matrix(ctext_mat, ctext_len);
ctext_mat_t = malloc((ctext_len*60)*sizeof(char));
if (NULL == ctext_mat_t)
{
free(ctext_mat);
return 0x01;
}
/*
* Transposition
*/
for(ctxtidx = 0; ctxtidx < 60; ctxtidx++)
for (charidx = 0; charidx < ctext_len; charidx++)
ctext_mat_t[charidx*60 + ctxtidx] = ctext_mat[ctxtidx*(ctext_len)+charidx];
key_img = malloc((ctext_len)*sizeof(char));
if (NULL == key_img)
{
free(ctext_mat);
free(ctext_mat_t);
return 0x01;
}
/*
* Single line detect
*/
for(k = 0; k < ctext_len; k++)
{
key_img[k] = ' ' ^ detect_key_single_line((char*) ctext_mat_t + k*60, 60);
}
plain = malloc((ctext_len+1)*sizeof(char));
if (NULL == plain)
{
free(key_img);
free(ctext_mat);
free(ctext_mat_t);
return 0x01;
}
plain[ctext_len] = 0x00;
/*
* Decryption
*/
for (i = 0; i < 60; i++)
{
xor_decode(plain, ctext_mat + i*ctext_len, ctext_len, key_img, ctext_len );
printf("%zu : %s\n", i, plain );
}
free(plain);
free(key_img);
free(ctext_mat);
free(ctext_mat_t);
return 0x00;
}