-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxor.h
66 lines (63 loc) · 2 KB
/
xor.h
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
58
59
60
61
62
63
64
65
66
void xor_encrypt()
{
char *message_raw = NULL; /* Pointer to message to be encrypted */
size_t size = 0; /* Initialise size of message */
FILE* fp = fopen("EncodedOutput.txt", "r"); /* Open compressed file */
fseek(fp,0,SEEK_END); /* Scan for size of file */
size = ftell(fp);
rewind(fp); /* Reset cursor of scanner */
message_raw = malloc((size + 1) * sizeof(*message_raw));
fread(message_raw,size,1,fp); /*Put file data into char */
message_raw[size] = '\0'; /* Nullcharacter */
printf("%s\n", message_raw); /* Checking */
char key[size]; /* Initialise key with same size as file */
int i;
for (i=0; i<size; i++) /* Random key creation for XOR */
{
key[i] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand() % 26];
}
char message_encrypted[size]; /* Apply XOR */
for (i=0; i<size; i++)
{
message_encrypted[i] = (message_raw[i]) ^ key[i];
}
FILE *encrypted = fopen("encrypted","w"); /* File to store encrypted
data */
fwrite(message_encrypted, 1, size, encrypted);
fclose(encrypted);
fclose(fp);
printf("%s\n", message_encrypted);
}
void xor_decrypt()
{
char *message_raw = NULL; /* Pointer to message to be decrypted */
size_t size = 0; /* Initialise size of message */
FILE* fp = fopen("encrypted", "r"); /* Open file to be decrypted */
fseek(fp,0,SEEK_END); /* Scan for size of file */
size = ftell(fp);
rewind(fp); /* Reset cursor of scanner */
message_raw = malloc((size + 1) * sizeof(*message_raw));
fread(message_raw,size,1,fp); /*Put file data into char */
message_raw[size] = '\0'; /* Nullcharacter */
if (message_raw == NULL)
{
printf("Error allocating memory!");
}
char key[size]; /* Initialise key with same size as file */
int i;
for (i=0; i<size; i++)
{
key[i] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand() % 26];
}
char message_decrypted[size]; /* Apply XOR again for decryption */
for (i=0; i<size; i++)
{
message_decrypted[i] = message_raw[i]^key[i];
}
if (message_decrypted != NULL)
{
printf("Decryption Success");
printf("\n");
}
fclose(fp);
}