forked from ginadivina/polling_booth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxorread.c
57 lines (45 loc) · 991 Bytes
/
xorread.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
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
void xor_decrypt(void);
int main()
{
xor_decrypt();
return 0;
}
void xor_decrypt()
{
int c, i;
FILE* encrypted = fopen("encrypted", "r");
char *message_raw;
size_t n = 0;
if (encrypted == NULL)
{
printf("File can't be opened.");
}
fseek(encrypted, 0, SEEK_END);
long f_size = ftell(encrypted);
fseek(encrypted, 0, SEEK_SET);
message_raw = malloc(f_size);
while ((c = fgetc(encrypted)) != EOF)
{
message_raw[n++] = (char) c;
}
/*message_raw[n] = '\0';*/
int message_length = strlen(message_raw);
char key[message_length];
srand(message_length);
for (i=1; i<message_length; i++)
{
key[i] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"[rand() % 36];
}
int key_length = strlen(key);
char message_decrypted[message_length];
for (i=0; i<message_length; i++)
{
message_decrypted[i] = message_raw[i]^key[i%key_length];
printf("%c", message_decrypted[i]);
}
printf("\n");
}