-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
120 lines (96 loc) · 2.95 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
116
117
118
119
120
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "headers/aes/aes.h"
#include "headers/utils/utils.h"
#define ENCRYPT 1
#define DECRYPT 2
int main(){
int option, bufferIndex = 0;
char *filename = (char*)calloc(128, sizeof(char));//There is no reason to pick specifically 128, but I did it.
char *compareFilename1 = (char*)calloc(128, sizeof(char));
char *compareFilename2 = (char*)calloc(128, sizeof(char));
char *path = (char*)calloc(128, sizeof(char));
char charRead;
char *bufferRead = (char *)calloc(32, sizeof(char));
int *bufferResult;
char *key = (char *)calloc(16, sizeof(char));
FILE *fileRead, *fileWrite;
fileRead = fileWrite = NULL;
strcpy(path, "assets/");
printf("%d - Encrypt\n", ENCRYPT);
printf("%d - Decrypt\n", DECRYPT);
printf("\nEnter option: ");
scanf("%d", &option);
printf("Enter the key (16 bytes): ");
scanf("%s", key);
if(!isKey16Bytes(key)){
printf("The KEY size MUST be 16 bytes.\n\n");
return 0;
}
printf("Enter the filename (must be at /assets): ");
scanf("%s", filename);
strcat(path, filename);
//Open Files
fileRead = fopen(path, "r");
if(fileRead == NULL){
printf("Erro ao abrir o arquivo: %s\n", filename);
return 0;
}
if(option == ENCRYPT){
replaceExtension(path, ".enc");
} else {
replaceExtension(path, ".dec");
}
remove(path);//Delete file if it exists
fileWrite = fopen(path, "a");
if(fileWrite == NULL){
printf("Erro ao criar arquivo\n");
return 0;
}
printf("\n*** Starting ***\n\n");
// Read File
while((charRead = fgetc(fileRead)) != EOF || isBufferIndexBelowBlockSize(bufferIndex, option)){
if(isBufferWithSpace(bufferIndex, option)){
bufferRead[bufferIndex] = charRead;
bufferIndex++;
}
if(!isBufferWithSpace(bufferIndex, option) || charRead == EOF){
if(option == ENCRYPT){
if(charRead == EOF && isBufferIndexBelowBlockSize(bufferIndex, option)){
bufferRead[bufferIndex-1] = '\0';//This indicates where we need start the padding
}
printf("Encrypting block...\n");
bufferResult = encrypt(bufferRead, key);
writeOnFile(fileWrite, bufferResult, "%02x", ENCRYPT);
free(bufferResult);
} else {
printf("Decrypting block...\n");
bufferResult = decrypt(bufferRead, key);
writeOnFile(fileWrite, bufferResult, "%c", DECRYPT);
free(bufferResult);
}
bufferIndex = 0;
}
if(charRead == EOF){
break;
}
}
fclose(fileRead);
fclose(fileWrite);
if(option == DECRYPT){
printf("\n*** Compare Files ***\n\n");
printf("Filename 1: ");
scanf("%s", compareFilename1);
printf("Filename 2: ");
scanf("%s", compareFilename2);
if(compareFiles(compareFilename1, compareFilename2)){
printf("\nOs arquivos são idênticos.\n");
} else {
printf("\nOs arquivos não são idênticos.\n");
}
}
printf("\nEND\n");
return 0;
}