-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptography_tool.c
More file actions
225 lines (211 loc) · 6.45 KB
/
cryptography_tool.c
File metadata and controls
225 lines (211 loc) · 6.45 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <unistd.h>
#include "shift_cypher.h"
#include "caesar_cypher.h"
#include "base_change.h"
#include "rsa.h"
const char *usage_msg = "Usage: ctool [options]\n\n-e tells the program you want to encrypt the input file\n-d tells the program you want to decrypt te input file\n-c tells the program that you want to create an encryption key. This causes the key file specified by -k to be the output file of the key and the output file specified by -o to be the public key if needed. If neither are specified than key.txt and public-key.txt will be created\n-i the name of the input file to encrypt/decrypt. This file must always be specified.\n-o the name of the file to output the encrypted/decrypted file. If this option is not specified the output will overwrite the input file\n-k the name of the file to write the key to if encryption is occuring or read the key from if decrytion is occuring. If encrypting this option is optional and if decrypting this option is necessary for all non-standard base changes\n-h prints this message\n";
// Start Create
void create_key(const char *output, const char *key)
{
if (output == NULL)
{
output = "public-key.txt";
}
if (key == NULL)
{
key = "key.txt";
}
while (true)
{
char choice[4];
printf("What algorithm would you like to use to generate your key:\n1 - rsa\nQ - Quit\n");
xfgets(choice, 4, stdin);
switch (choice[0])
{
case '1':
rsa_create_key(output, key);
return;
case 'Q':
return;
}
printf("Invalid choice\n");
}
}
// Start Encrypt
void encrypt(const char *input_file, const char *output_file, const char *key_file)
{
while (true)
{
char choice[4];
printf("What encryption algorithm would you like to use:\n1 - Shift Cypher\n2 - Ceaser Cypher\n3 - To Binary\n4 - To Octal\n5 - To Hex\n6 - To Base32\n7 - To Base64\n8 - To Base\n9 - RSA\nQ - Quit\n");
xfgets(choice, 4, stdin);
switch (choice[0])
{
case '1':
shift_cypher_encrypt(input_file, output_file, key_file);
return;
case '2':
caesar_cypher_encrypt(input_file, output_file, key_file);
return;
case '3':
binary_encrypt(input_file, output_file, key_file);
return;
case '4':
octal_encrypt(input_file, output_file, key_file);
return;
case '5':
hex_encrypt(input_file, output_file, key_file);
return;
case '6':
base32_encrypt(input_file, output_file, key_file);
return;
case '7':
base64_encrypt(input_file, output_file, key_file);
return;
case '8':
base_encrypt(input_file, output_file, key_file);
return;
case '9':
if (key_file == NULL)
{
printf("Can't run rsa encryption without a key\n");
break;
}
rsa_encrypt(input_file, output_file, key_file);
return;
case 'Q':
return;
default:
printf("Invalid choice\n");
}
}
}
// End Encrypt
// Begin Decrypt
void decrypt(const char *input_file, const char *output_file, const char *key_file)
{
if (key_file == NULL)
{
key_file = "key.txt";
}
while (true)
{
char choice[4];
printf("What decryption algorithm would you like to use:\n1 - Shift Cypher\n2 - Ceaser Cypher\n3 - From Binary\n4 - From Octal\n5 - From Hex\n6 - From Base32\n7 - From Base64\n8 - From Base\n9 - RSA\nQ - Quit\n");
xfgets(choice, 4, stdin);
switch (choice[0])
{
case '1':
if (key_file == NULL)
{
printf("Can't decrypt a shift cyper without a key\n");
break;
}
shift_cypher_decrypt(input_file, output_file, key_file);
return;
case '2':
if (key_file == NULL)
{
printf("Can't decrypt a caesar cyper without a key\n");
break;
}
caesar_cypher_decrypt(input_file, output_file, key_file);
return;
case '3':
binary_decrypt(input_file, output_file);
return;
case '4':
octal_decrypt(input_file, output_file);
return;
case '5':
hex_decrypt(input_file, output_file);
return;
case '6':
base32_decrypt(input_file, output_file);
return;
case '7':
base64_decrypt(input_file, output_file);
return;
case '8':
if (key_file == NULL)
{
printf("Can't decrypt from an arbitrary base without a key\n");
break;
}
base_decrypt(input_file, output_file, key_file);
return;
case '9':
if (key_file == NULL)
{
printf("Can't decrypt RSA without a key\n");
break;
}
rsa_decrypt(input_file, output_file, key_file);
return;
case 'Q':
return;
default:
printf("Invalid choice\n");
}
}
}
// End Decrypt
int main(int argc, char *argv[])
{
setbuf(stdin, NULL);
bool encrypt_flag = true;
bool create_flag = false;
const char *input = NULL;
const char *output = NULL;
const char *key = NULL;
for (int opt = getopt(argc, argv, "hedci:o:k:"); opt != -1; opt = getopt(argc, argv, "hedi:o:k:"))
{
switch (opt)
{
case 'e':
encrypt_flag = true;
break;
case 'd':
encrypt_flag = false;
break;
case 'c':
create_flag = true;
break;
case 'i':
input = optarg;
break;
case 'o':
output = optarg;
break;
case 'k':
key = optarg;
break;
default:
printf(usage_msg);
exit(1);
}
}
if (create_flag)
{
create_key(output, key);
return 0;
}
if (input == NULL)
{
printf(usage_msg);
exit(1);
}
if (output == NULL)
{
output = input;
}
if (encrypt_flag)
{
encrypt(input, output, key);
}
else
{
decrypt(input, output, key);
}
return 0;
}