summaryrefslogtreecommitdiff
path: root/set1/task4.c
blob: c95e21fde1223d6fda2c6c6aaaf53a82e1a62bd7 (plain)
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
#include "../lib/lib.h"

int main() {
	/** read the file */
	FILE *fp;
	int malloc_size = 62;
	int line_number = 0;
	int j;
	char key;
	char *string = malloc(malloc_size);
	char *string2 = malloc(malloc_size);
	char *cleartext = malloc(malloc_size);
	struct key_and_freq tmp;

	fp = fopen("4.txt", "r");

	if (fp == NULL) {
		printf("Error open file\n");
		exit(1);
	}
	
	while (fscanf(fp, "%61c", string) != EOF) {
		tmp.hits = 0;
		j = decode_hex_string(string, string2);
		key = brute_force_single_byte_xor(string2, j, &tmp);
		xor_string(string2, &key, cleartext, 1, j);
		if ((!isprintable(cleartext, j)) || (tmp.hits < 10))
			continue;
		
		printf("%s", cleartext);
		line_number++;
	}

	fclose(fp);
	return 0;
}