-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypt.c
160 lines (125 loc) · 3.97 KB
/
crypt.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
#include "crypt.h"
//********************************************************************************************
//**-------------------------------------FILE CRYPTION--------------------------------------**
//********************************************************************************************
int crypt_file(const char *path, const char *fileName, unsigned char *key) {
int outlen, inlen;
FILE *file, *temp;
file = fopen(fileName,"r+b");
if(file == NULL) {
return -2;
}
char tempName[PATH_SIZE];
cnct(path,"TemP.TemP",tempName);
temp = fopen(tempName,"wb");
unsigned char iv[8] = "DAJ-7l2"; /* вектор инициализации */
unsigned char inbuf[BUF_SIZE], outbuf[BUF_SIZE];
EVP_CIPHER_CTX ctx;
const EVP_CIPHER * cipher;
EVP_CIPHER_CTX_init(&ctx);
cipher = EVP_bf_cbc();
EVP_EncryptInit(&ctx, cipher, key, iv);
while(1) {
inlen = fread(inbuf, 1, BUF_SIZE, file);
if(inlen <= 0) break;
if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, inbuf, inlen))
return 1;
fwrite(outbuf, 1, outlen, temp);
}
if(!EVP_EncryptFinal(&ctx, outbuf, &outlen))
return 1;
fwrite(outbuf, 1, outlen, temp);
EVP_CIPHER_CTX_cleanup(&ctx);
fclose(file);
fclose(temp);
remove(fileName);
rename(tempName,fileName);
return 0;
}
int decrypt_file(const char *path, const char *fileName, unsigned char *key) {
int outlen, inlen;
FILE *file, *temp;
file = fopen(fileName,"r+b");
if(file == NULL) {
return -2;
}
char tempName[PATH_SIZE];
cnct(path,"TemP.TemP",tempName);
temp = fopen(tempName,"wb");
unsigned char iv[8] = "DAJ-7l2"; /* вектор инициализации */
unsigned char inbuf[BUF_SIZE], outbuf[BUF_SIZE];
EVP_CIPHER_CTX ctx;
const EVP_CIPHER * cipher;
EVP_CIPHER_CTX_init(&ctx);
cipher = EVP_bf_cbc();
EVP_DecryptInit(&ctx, cipher, key, iv);
while(1) {
inlen = fread(inbuf, 1, BUF_SIZE, file);
if(inlen <= 0) break;
if(!EVP_DecryptUpdate(&ctx, outbuf, &outlen, inbuf, inlen))
return 1;
fwrite(outbuf, 1, outlen, temp);
}
if(!EVP_DecryptFinal(&ctx, outbuf, &outlen))
return 1;
fwrite(outbuf, 1, outlen, temp);
EVP_CIPHER_CTX_cleanup(&ctx);
fclose(file);
fclose(temp);
remove(fileName);
rename(tempName,fileName);
return 0;
}
int cnct(const char *dirName, const char *fileName, char buffer[]) {
strcpy(buffer,dirName);
strncat(buffer,"/",2);
strncat(buffer,fileName,strlen(fileName)+2);
return 0;
}
//*********************************************************************************************
//**------------------------------------DIR CRYPTION-----------------------------------------**
//*********************************************************************************************
int crypt_dir(const char *dirName, unsigned char *key) {
char buffer[PATH_SIZE];
DIR *dir;
struct dirent *entry;
dir = opendir(dirName);
if (!dir) {
perror("diropen");
exit(1);
};
while ( (entry = readdir(dir)) != NULL) {
if(entry->d_type != DT_DIR && strcmp(entry->d_name,".DS_Store")) {
cnct(dirName,entry->d_name,buffer);
crypt_file(dirName, buffer,key);
}
if(entry->d_type == DT_DIR && strcmp(entry->d_name,"..") && strcmp(entry->d_name,".")) {
cnct(dirName,entry->d_name,buffer);
crypt_dir(buffer, key);
}
}
closedir(dir);
return 0;
}
int decrypt_dir(const char *dirName, unsigned char *key) {
char buffer[PATH_SIZE];
DIR *dir;
struct dirent *entry;
dir = opendir(dirName);
if (!dir) {
perror("diropen");
exit(1);
};
while ( (entry = readdir(dir)) != NULL) {
if(entry->d_type != DT_DIR && strcmp(entry->d_name,".DS_Store")) {
cnct(dirName,entry->d_name,buffer);
decrypt_file(dirName,buffer,key);
}
if(entry->d_type == DT_DIR && strcmp(entry->d_name,"..") && strcmp(entry->d_name,".")) {
cnct(dirName,entry->d_name,buffer);
decrypt_dir(buffer, key);
}
}
closedir(dir);
return 0;
}