-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_decrypted.txt
More file actions
188 lines (157 loc) · 4.59 KB
/
Copy pathmain_decrypted.txt
File metadata and controls
188 lines (157 loc) · 4.59 KB
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
#include "std_headers.h"
#include "encrypt.h"
#include "decrypt.h"
#include "getChar_input_and_convert_password_characters_to_int.h"
#include "is_valid_command.h"
#include "is_valid_path.h"
#include "print_korify_help_options.h"
#define PASSWORD_INITIAL_SIZE 100
#define KORIFY_CURRENT_VERSION "0.0.0"
// This is not in used
void str_to_int(const char *_password_string, int *_int)
{
for (int i = 0; i < strlen(_password_string); i++)
{
*_int += (int)_password_string[i];
}
}
int main(int argc, char *argv[])
{
// printf("%s\n", *argv++);
if (argc <= 1)
{
printf("No command is passed. Try using kori -ef to encrypt file and -df to decrypt file.\n");
print_korify_help_options();
return 1;
}
for (int x = 1; x < argc; x++)
{
if (!is_valid_command(argv[x]))
{
printf("Invalid Command: %s\n", argv[x]);
break;
}
if (strcmp(argv[x], "-ef") == 0)
{
// Handling encryption command
if (argc < x + 3)
{
printf("-ef (Encrypt file) needs file path and output directory.\n");
break;
}
if (!is_valid_path(argv[x + 1]))
{
printf("Invalid or non-existent file path: %s\n", argv[x + 1]);
break;
}
FILE *file = fopen(argv[x + 1], "r");
if (!file)
{
perror("Error opening file");
return EXIT_FAILURE;
}
char *password;
int integer_value_of_password = 0;
puts("------------------------------");
printf("| File successfully loaded |\n");
puts("------------------------------\n");
printf("Enter your encryption string: ");
int result = getChar_input_and_convert_password_characters_to_int(&password, PASSWORD_INITIAL_SIZE, getchar, &integer_value_of_password);
puts("------------------------------");
if (result == 1)
{
fclose(file);
printf("Error getting encryption string.\n");
return EXIT_FAILURE;
}
FILE *encrypted_output_file = fopen(argv[x + 2], "w");
if (!encrypted_output_file)
{
perror("Error opening output file");
fclose(encrypted_output_file);
fclose(file);
return EXIT_FAILURE;
}
int is_encryption_success = encrypt(file, &integer_value_of_password, encrypted_output_file, "");
if (is_encryption_success == 1)
{
printf("Encryption failed.\n");
fclose(encrypted_output_file);
fclose(file);
return EXIT_FAILURE;
}
printf("| ** Encryption successful ** |\n");
puts("------------------------------");
printf("| Encrypted output file: %s |\n", argv[x + 2]);
puts("------------------------------");
fclose(encrypted_output_file);
fclose(file);
return EXIT_SUCCESS;
}
if (strcmp(argv[x], "-df") == 0)
{
// Handling decryption command
if (argc < x + 3)
{
printf("-df (Decrypt file) needs file path and output directory.\n");
break;
}
if (!is_valid_path(argv[x + 1]))
{
printf("Invalid or non-existent file path: %s\n", argv[x + 1]);
break;
}
FILE *encrypted_source_file = fopen(argv[x + 1], "r");
if (!encrypted_source_file)
{
perror("Error opening encrypted file");
return EXIT_FAILURE;
}
char *password;
int integer_value_of_password = 0;
puts("------------------------------");
printf("| File successfully loaded |\n");
puts("------------------------------\n");
printf("Enter your decryption string: ");
int result = getChar_input_and_convert_password_characters_to_int(&password, PASSWORD_INITIAL_SIZE, getchar, &integer_value_of_password);
puts("------------------------------");
if (result == 1)
{
fclose(encrypted_source_file);
printf("Error getting decryption string.\n");
return EXIT_FAILURE;
}
FILE *decrypted_output_file = fopen(argv[x + 2], "w");
if (!decrypted_output_file)
{
perror("Error opening output file");
fclose(encrypted_source_file);
return EXIT_FAILURE;
}
int is_decryption_success = decrypt(encrypted_source_file, &integer_value_of_password, decrypted_output_file, "");
if (is_decryption_success == 1)
{
printf("Decryption failed.\n");
fclose(decrypted_output_file);
fclose(encrypted_source_file);
return EXIT_FAILURE;
}
printf("| ** Decryption successful ** |\n");
puts("------------------------------");
printf("| Decrypted output file: %s |\n", argv[x + 2]);
puts("------------------------------");
fclose(decrypted_output_file);
fclose(encrypted_source_file);
return EXIT_SUCCESS;
}
if(strcmp(argv[x], "-h") == 0 || strcmp(argv[x], "-help") == 0){
print_korify_help_options();
break;
}
if(strcmp(argv[x], "-v") == 0 || strcmp(argv[x], "-version") == 0){
printf("korify version -> %s\n", KORIFY_CURRENT_VERSION);
break;
}
}
return 0;
}