-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc_dec.c
158 lines (131 loc) · 4.39 KB
/
enc_dec.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#define MAX_FILENAME_LEN 256
#define ENCRYPTED_EXTENSION ".rasel"
#define DECRYPTED_EXTENSION ".decrypted"
void encrypt_file(char *filename, char *password);
void decrypt_file(char *filename, char *password);
//enc_dir, dec_dir
void encrypt_directory(char *directory_path, char *password);
void decrypt_directory(char *directory_path, char *password);
int main(int argc, char **argv) {
if (argc < 4) {
printf("Usage: %s <encrypt|decrypt> <directory_path> <password>\n", argv[0]);
exit(1);
}
char *action = argv[1];
char *directory_path = argv[2];
char *password = argv[3];
if (strcmp(action, "encrypt") == 0) {
encrypt_directory(directory_path, password);
printf("Encryption complete.\n");
} else if (strcmp(action, "decrypt") == 0) {
decrypt_directory(directory_path, password);
printf("Decryption complete.\n");
} else {
printf("Error: invalid action %s. Must be \"encrypt\" or \"decrypt\".\n", action);
exit(1);
}
return 0;
}
void encrypt_file(char *filename, char *password) {
FILE *fp, *fp_out;
int c, i;
char out_filename[MAX_FILENAME_LEN];
//Constructing output filename by appending encrypted extension
strncpy(out_filename, filename, MAX_FILENAME_LEN - strlen(ENCRYPTED_EXTENSION) - 1);
strcat(out_filename, ENCRYPTED_EXTENSION);
//Opening input and output files
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("Error: could not open input file %s.\n", filename);
exit(1);
}
fp_out = fopen(out_filename, "wb");
if (fp_out == NULL) {
printf("Error: could not open output file %s.\n", out_filename);
fclose(fp);
exit(1);
}
//Encrypting file contents by XOR-ing with password
i = 0;
while ((c = fgetc(fp)) != EOF) {
fputc(c ^ password[i], fp_out);
i = (i + 1) % strlen(password);
}
//Closing files
fclose(fp);
fclose(fp_out);
}
void decrypt_file(char *filename, char *password) {
FILE *fp, *fp_out;
int c, i;
char out_filename[MAX_FILENAME_LEN];
//Constructing output filename by appending decrypted extension
strncpy(out_filename, filename, MAX_FILENAME_LEN - strlen(DECRYPTED_EXTENSION) - 1);
strcat(out_filename, DECRYPTED_EXTENSION);
//Opening input and output files
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("Error: could not open input file %s.\n", filename);
exit(1);
}
fp_out = fopen(out_filename, "wb");
if (fp_out == NULL) {
printf("Error: could not open output file %s.\n", out_filename);
fclose(fp);
exit(1);
}
//Decrypting file contents by XOR-ing with password
i = 0;
while ((c = fgetc(fp)) != EOF) {
fputc(c ^ password[i], fp_out);
i = (i + 1) % strlen(password);
}
//Closing files
fclose(fp);
fclose(fp_out);
}
void encrypt_directory(char *directory_path, char *password) {
DIR *dir;
struct dirent *ent;
struct stat st;
//Opening directory and iterate over files
dir = opendir(directory_path);
if (dir == NULL) {
printf("Error: could not open directory %s.\n", directory_path);
exit(1);
}
while ((ent = readdir(dir)) != NULL) {
char filename[MAX_FILENAME_LEN];
snprintf(filename, MAX_FILENAME_LEN, "%s/%s", directory_path, ent->d_name);
//Checking if file is a regular file (not a directory or other special file)
if (stat(filename, &st) == 0 && S_ISREG(st.st_mode)) {
encrypt_file(filename, password);
}
}
closedir(dir);
}
void decrypt_directory(char *directory_path, char *password) {
DIR *dir;
struct dirent *ent;
struct stat st;
//Opening directory and iterate over files
dir = opendir(directory_path);
if (dir == NULL) {
printf("Error: could not open directory %s.\n", directory_path);
exit(1);
}
while ((ent = readdir(dir)) != NULL) {
char filename[MAX_FILENAME_LEN];
snprintf(filename, MAX_FILENAME_LEN, "%s/%s", directory_path, ent->d_name);
//Checking if file is a regular file (not a directory or other special file)
if (stat(filename, &st) == 0 && S_ISREG(st.st_mode)) {
decrypt_file(filename, password);
}
}
closedir(dir);
}