-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptit.c
162 lines (98 loc) · 2.76 KB
/
cryptit.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>
#define TAILLE_MAX 1000
void clean(const char *buffer, FILE *fp);
void encryptDecrypt(char *input, char *output);
int main(int argc, char *argv[]){
DIR *dir = NULL;
struct dirent *envelopDir = NULL;
dir = opendir("/media/external/");
if(dir == NULL){
printf("Error. Cannot open /media/external.\n");
exit(1);
}
printf("The directory /media/external has been properly opened.\n\n");
FILE *f1 = NULL;
FILE *f2 = NULL;
char baseStr[TAILLE_MAX] = "";
char encryptedString[TAILLE_MAX] = "";
f1 = fopen("/media/external/test.txt", "r");
if(f1 == NULL)
printf("Error. Failed to oppen f1.\n");
else{
f2 = fopen("/media/external/encryptedFile.xor", "w");
if(f2 == NULL)
printf("Error. Failed to open f2.\n");
while(fgets(baseStr, TAILLE_MAX, f1) != NULL){
encryptDecrypt(baseStr, encryptedString);
fputs(encryptedString, f2);
}
fclose(f1);
fclose(f2);
}
close(dir);
/*
while(keepRunning){
while((envelopDir = readdir(dir)) != NULL){
printf("The directory barj contains : %s\n", envelopDir->d_name);
}
printf("\nIs there a directory that you would like to encrypt? (n for no, y for yes)\n");
choice1 = getchar();
while(getchar() != '\n');
switch(choice1){
case 'n':
keepRunning = 0;
break;
case 'y':
printf("\nType down the name of the directory that you want to encrypt: ");
fgets(filesName, sizeof(filesName), stdin);
clean(filesName, stdin);
FILE *f1 = NULL;
FILE *f2 = NULL;
char baseStr[TAILLE_MAX] = "";
char encryptedString[TAILLE_MAX] = "";
//f1 = fopen(filesName, "r");
if(f1 == NULL)
printf("Error. Failed to oppen f1.\n");
else{
f2 = fopen("blablaPasClair", "w");
if(f2 == NULL)
printf("Error. Failed to open f2.\n");
while(fgets(baseStr, TAILLE_MAX, f1) != NULL){
encryptDecrypt(baseStr, encryptedString);
fputs(encryptedString, f2);
}
fclose(f1);
fclose(f2);
}
break;
default:
printf("Please type a n for no or a y for yes");
}
//fgets(filesName, sizeof(filesName), stdin);
}
if(closedir(dir) != 0){
printf("Error. Cannot close barj.\n");
exit(1);
}
printf("Succesful closing of barj.\n");*/
return 0;
}
void clean(const char *buffer, FILE *fp){
char *p = strchr(buffer,'\n');
if (p != NULL)
*p = 0;
else{
int c;
while ((c = fgetc(fp)) != '\n' && c != EOF);
}
}
void encryptDecrypt(char *input, char *output){
char key[] = {'K', 'C', 'Q'}; //Can be any chars, and any size array
int i;
for(i = 0; i < strlen(input); i++) {
output[i] = input[i] ^ key[i % (sizeof(key)/sizeof(char))];
}
}