-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.c
More file actions
40 lines (31 loc) · 921 Bytes
/
cipher.c
File metadata and controls
40 lines (31 loc) · 921 Bytes
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
/*
* XOR Stream Cipher
* Author: Joseph Bassey
* Description: Encrypts/Decrypts files using a single-byte XOR operation.
* Usage: ./cipher <input_file> <output_file>
*/
#include <stdio.h>
#include <stdlib.h>
#define KEY 'D'
int main(int argc, char *argv[]) {
if (argc !=3) {
printf("Usage: %s <input_file> <output_file>\n", argv[0]);
return 1;
}
FILE *inputFile = fopen(argv[1], "rb");
if (inputFile == NULL) {
perror("Error opening input file");
return 1;
}
FILE *outputFile = fopen(argv[2], "wb");
int atom;
while((atom = fgetc(inputFile)) != EOF) {
// XOR operation is applied. The operation is reversible.
// The original data can be obtained by running this twice.
int encrypted_atom = atom ^ KEY;
fputc(encrypted_atom, outputFile);
}
fclose(inputFile);
fclose(outputFile);
return 0;
}