-
Notifications
You must be signed in to change notification settings - Fork 0
/
set1_5_enc_rep_xor.c
41 lines (36 loc) · 1.05 KB
/
set1_5_enc_rep_xor.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
/* Set 1. Challenge 5. Reads a line from stdin. Performs a XOR with the key
* given as an argument and outputs this to stdout. Is not reversible because
* outputs in ASCII, but that would be a quick fix.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "set1_utils.h"
#define MAX_BUFF 256
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "enc_rep_xor [KEY]\n");
exit(EXIT_FAILURE);
}
char *input = malloc(sizeof(char) * MAX_BUFF);
char *output = malloc(sizeof(char) * MAX_BUFF);
if (!input || !output) {
fprintf(stderr, "Out of Memory\n");
exit(EXIT_FAILURE);
}
ssize_t read_size;
int key_index = 0;
int key_len = strlen(argv[1]);
while ((read_size = read(STDIN_FILENO, input, MAX_BUFF)) > 0) {
for (int i = 0; i < read_size; i++) {
printf("%02x", input[i] ^ argv[1][key_index++]);
if (key_index == key_len)
key_index = 0;
}
}
free(output);
free(input);
return 0;
}