-
Notifications
You must be signed in to change notification settings - Fork 4
/
xor.c
45 lines (35 loc) 路 910 Bytes
/
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
42
43
44
45
#include <stdio.h>
#include <string.h>
// XOR decryption for the encrypted file using the key
// usage: ./xor <encrypted_file> <key> <output_file>
// thx github copilot
long fsize(FILE *file) {
long size;
fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);
return size;
}
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Usage: %s <encrypted_file> <key> <output_file>\n", argv[0]);
return 1;
}
FILE *file = fopen(argv[1], "rb");
FILE *output = fopen(argv[3], "wb");
if (file == NULL) {
printf("Error opening file %s\n", argv[1]);
return 1;
}
char *key = argv[2];
int key_len = strlen(key);
int c;
int i = 0;
while ((c = fgetc(file)) != EOF) {
fputc(c ^ key[i], output);
i = (i + 1) % key_len;
}
fclose(file);
fclose(output);
return 0;
}