cryptopals_c

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

c7.c (758B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <ba.h>
      5 #include <util.h>
      6 #include <aes.h>
      7 #include <block.h>
      8 
      9 int main(int argc, char *argv[])
     10 {
     11 	char *key = "YELLOW SUBMARINE";
     12 	char line[256];
     13 	int pt_len;
     14 	char *b64;
     15 	char *hex;
     16 	FILE *f;
     17 	ba *key_ba;
     18 	ba *ct;
     19 	ba *pt;
     20 	int sz;
     21 
     22 	b64 = (char *) malloc(sizeof(char));
     23 
     24 	f = fopen(argv[1], "r");
     25 	sz = 0;
     26 	while (fgets(line, sizeof(line), f)) {
     27 		sz += strlen(line) - 1;
     28 		b64 = (char *) realloc(b64, sz);
     29 		strncat(b64, line, strlen(line) - 1);
     30 	}
     31 
     32 	base64_to_hex(&hex, b64);
     33 
     34 	ct = ba_from_hex(hex);
     35 	key_ba = ba_from_string(key);
     36 
     37 	decrypt_ecb(ct, key_ba, &pt, 16, aes_128_decrypt);
     38 
     39 	printf("decrypted successfully\n");
     40 	ba_fprint_ascii(pt, stdout, 0);
     41 	printf("\n");
     42 
     43 	return 0;
     44 }