-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.5.c
34 lines (31 loc) · 897 Bytes
/
1.5.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
/* https://cryptopals.com/sets/1/challenges/5
** This expects an option flag, plaintext file, and key as arguments, e.g.
** ./a.out -b 5.txt ICE
** -b ouputs base64 and -h outputs hex
*/
#include "cryptopals.h"
int main(int argc, char **argv)
{
FILE *fp = fopen(argv[2], "r");
unsigned char *key = argv[3];
int key_size = strlen(argv[3]);
int counter = 0;
int c;
unsigned char bytes[4096];
while ((c = fgetc(fp)) != EOF) {
bytes[counter] = ((unsigned char) c) ^ key[counter % key_size];
counter++;
}
fclose(fp);
if (strcmp(argv[1], "-b") == 0) {
unsigned char *base64 = bytes_to_base64(bytes, counter);
printf("%s", base64);
free(base64);
}
if (strcmp(argv[1], "-h") == 0) {
unsigned char *hex = bytes_to_hex(bytes, counter);
printf("%s", hex);
free(hex);
}
printf("\n");
}