cryptopals_c

cryptopals crypto challenges solutions in pure c
git clone git://git.superpozycja.net/cryptopals_c
Log | Files | Refs | README

c4.c (800B)


      1 #include <stdio.h>
      2 #include <ctype.h>
      3 #include <ba.h>
      4 #include <analysis.h>
      5 
      6 #define MAXLEN 1000
      7 
      8 void preprocess_string(char (*l)[MAXLEN])
      9 {
     10 	int i;
     11 
     12 	while (isspace(**l))
     13 		*l++;
     14 	for (i = 0; i < strlen(*l); i++) {
     15 		if ((*l)[i] == '\n') {
     16 			(*l)[i] = '\0';
     17 			break;
     18 		}
     19 	}
     20 }
     21 
     22 int main(int argc, char* argv[])
     23 {
     24 	unsigned long maxscore;
     25 	char l[MAXLEN];
     26 	ba *best;
     27 	FILE *p;
     28 	ba *ct;
     29 
     30 	if (argc != 2)
     31 		return -1;
     32 
     33 	p = fopen(argv[1], "r");
     34 	maxscore = 0;
     35 	best = (ba *) malloc(sizeof(ba));
     36 
     37 	while (fgets(l, sizeof(l), p)) {
     38 		unsigned long score;
     39 
     40 		preprocess_string(&l);
     41 		ct = ba_from_hex(l);
     42 		ct = decrypt_scxor(ct);
     43 		score = score_english(ct);
     44 		if (score > maxscore) {
     45 			maxscore = score;
     46 			ba_copy(best, ct);
     47 		}
     48 	}
     49 	ba_fprint_ascii(best, stdout, 0);
     50 	printf("\n");
     51 
     52 	ba_free(ct);
     53 }