-
Notifications
You must be signed in to change notification settings - Fork 1
/
aes-crypt-util.c
99 lines (90 loc) · 2.01 KB
/
aes-crypt-util.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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "aes-crypt.h"
int main(int argc, char **argv)
{
/* Local vars */
int action = 0;
int ifarg;
int ofarg;
FILE* inFile = NULL;
FILE* outFile = NULL;
char* key_str = NULL;
/* Check General Input */
if(argc < 3){
fprintf(stderr, "usage: %s %s\n", argv[0],
"<type> <opt key phrase> <in path> <out path>");
exit(EXIT_FAILURE);
}
/* Encrypt Case */
if(!strcmp(argv[1], "-e")){
/* Check Args */
if(argc != 5){
fprintf(stderr, "usage: %s %s\n", argv[0],
"-e <key phrase> <in path> <out path>");
exit(EXIT_FAILURE);
}
/* Set Vars */
key_str = argv[2];
ifarg = 3;
ofarg = 4;
action = 1;
}
/* Decrypt Case */
else if(!strcmp(argv[1], "-d")){
/* Check Args */
if(argc != 5){
fprintf(stderr, "usage: %s %s\n", argv[0],
"-d <key phrase> <in path> <out path>");
exit(EXIT_FAILURE);
}
/* Set Vars */
key_str = argv[2];
ifarg = 3;
ofarg = 4;
action = 0;
}
/* Pass-Through (Copy) Case */
else if(!strcmp(argv[1], "-c")){
/* Check Args */
if(argc != 4){
fprintf(stderr, "usage: %s %s\n", argv[0],
"-c <in path> <out path>");
exit(EXIT_FAILURE);
}
/* Set Vars */
key_str = NULL;
ifarg = 2;
ofarg = 3;
action = -1;
}
/* Bad Case */
else {
fprintf(stderr, "Unkown action\n");
exit(EXIT_FAILURE);
}
/* Open Files */
inFile = fopen(argv[ifarg], "rb");
if(!inFile){
perror("infile fopen error");
return EXIT_FAILURE;
}
outFile = fopen(argv[ofarg], "wb+");
if(!outFile){
perror("outfile fopen error");
return EXIT_FAILURE;
}
/* Perform do_crpt action (encrypt, decrypt, copy) */
if(!do_crypt(inFile, outFile, action, key_str)){
fprintf(stderr, "do_crypt failed\n");
}
/* Cleanup */
if(fclose(outFile)){
perror("outFile fclose error\n");
}
if(fclose(inFile)){
perror("inFile fclose error\n");
}
return EXIT_SUCCESS;
}