-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
challenge_03.c
74 lines (67 loc) · 2.05 KB
/
challenge_03.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
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "hex.h"
#include "xor.h"
#include "dec.h"
#include "bin.h"
#include "freq.h"
/*
* Challenge: https://cryptopals.com/sets/1/challenges/3
*
* Frequency analysis for English:
* http://pi.math.cornell.edu/~mec/2003-2004/cryptography/subs/frequencies.html
*/
int main(int argc, char** argv) {
printf("Cryptopals Set 1, Challenge 3\n");
uint8_t ciphertext_hex[] = "1B37373331363F78151B7F2B783431333D78397828372D363C78373E783A393B3736";
printf("Input: ");
print_hex(ciphertext_hex, strlen(ciphertext_hex));
printf("\n");
// Convert the ciphertext to binary
uint8_t* ciphertext_bin = hex_to_binary(ciphertext_hex);
int bin_len = binary_len_of_hex(ciphertext_hex);
printf("Input as binary:\n");
print_binary(ciphertext_bin, bin_len);
printf("\n");
uint8_t* key_bin = malloc(bin_len);
struct score_t score = {3.0, 0.0, NULL};
for (int i = 0; i <= 255; i++) {
uint8_t b[8] = {0};
dec_to_binary(i, b);
for (int y = 0; y < bin_len; y+=8) {
int idx = y;
key_bin[idx] = b[0];
key_bin[idx+1] = b[1];
key_bin[idx+2] = b[2];
key_bin[idx+3] = b[3];
key_bin[idx+4] = b[4];
key_bin[idx+5] = b[5];
key_bin[idx+6] = b[6];
key_bin[idx+7] = b[7];
}
//print_binary(key_bin, bin_len);
//printf("\n");
// So we xor the ciphertext and the current key
uint8_t* plaintext_bin = xor_binary(ciphertext_bin, key_bin, bin_len);
double s = score_plaintext(plaintext_bin, bin_len);
if (s < score.s) {
printf("New score: %f, score.s: %f\n", s, score.s);
score.s = s;
score.key = i;
if (score.binary != NULL) {
free(score.binary);
}
score.binary = plaintext_bin;
printf("Potential solution: "), print_binary_as_chars(score.binary, bin_len);
printf("\n");
} else {
free(plaintext_bin);
}
}
printf("Solution with lowest score (closest match): %f\n", score.s);
print_binary_as_chars(score.binary, bin_len);
free(score.binary);
free(ciphertext_bin);
return 0;
}