-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneTypePadCipher_FileEncryption.c
More file actions
101 lines (84 loc) · 2.55 KB
/
Copy pathOneTypePadCipher_FileEncryption.c
File metadata and controls
101 lines (84 loc) · 2.55 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
#include <sys/stat.h>
#include <errno.h>
struct OneTimePad {
char *key;
size_t file_size;
};
void handle_error(const char *message) {
perror(message);
exit(EXIT_FAILURE);
}
size_t calculate_file_size(const char *file_path) {
struct stat stat_buf;
if (stat(file_path, &stat_buf) == -1) {
handle_error("Error getting file size");
}
return stat_buf.st_size;
}
void set_key(struct OneTimePad *otp, const char *key) {
if (key == NULL || strlen(key) == 0) {
fprintf(stderr, "Key cannot be empty\n");
exit(EXIT_FAILURE);
}
otp->key = strdup(key);
if (otp->key == NULL) {
handle_error("Memory allocation for key failed");
}
}
char *generate_full_key(struct OneTimePad *otp) {
if (otp->key == NULL) {
fprintf(stderr, "Key has not been set\n");
exit(EXIT_FAILURE);
}
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, otp->key, strlen(otp->key));
SHA256_Final(hash, &sha256);
char *full_key = malloc(otp->file_size);
if (full_key == NULL) {
handle_error("Memory allocation for full key failed");
}
for (size_t i = 0; i < otp->file_size; i++) {
full_key[i] = hash[i % SHA256_DIGEST_LENGTH];
}
return full_key;
}
void encrypt_decrypt_file(const char *input_file_path, const char *output_file_path, struct OneTimePad *otp) {
otp->file_size = calculate_file_size(input_file_path);
char *full_key = generate_full_key(otp);
FILE *input_file = fopen(input_file_path, "rb");
if (input_file == NULL) {
handle_error("Error opening input file");
}
FILE *output_file = fopen(output_file_path, "wb");
if (output_file == NULL) {
handle_error("Error opening output file");
}
char buffer;
size_t index = 0;
while (fread(&buffer, 1, 1, input_file) == 1) {
char encrypted_char = buffer ^ full_key[index++];
fwrite(&encrypted_char, 1, 1, output_file);
}
fclose(input_file);
fclose(output_file);
free(full_key);
// Optional: Remove the input file after encryption
remove(input_file_path);
}
int main(int argc, char **argv) {
if (argc != 4) {
fprintf(stderr, "Usage: %s <key> <input file> <output file>\n", argv[0]);
return EXIT_FAILURE;
}
struct OneTimePad otp;
set_key(&otp, argv[1]);
encrypt_decrypt_file(argv[2], argv[3], &otp);
free(otp.key);
return EXIT_SUCCESS;
}