-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdcrypt.c
More file actions
201 lines (171 loc) · 5.17 KB
/
dcrypt.c
File metadata and controls
201 lines (171 loc) · 5.17 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
#include <stdio.h>
#include <gcrypt.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <termios.h>
#include <string.h>
#define VERSION "0.1"
static void
usage(const char *myname, FILE *out)
{
fputs("\n", out);
fputs("Usage:\n", out);
fprintf(out, " %s [Options] file.ext\n", myname);
fputs("\n", out);
fputs("Options:\n", out);
fputs(" -e, --encrypt encrypt a file\n", out);
fputs(" -d, --decrypt decrypt a file\n", out);
fputs(" -v, --version print version information\n", out);
fputs(" -h, --help print help information\n", out);
fputs("\n", out);
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
int main(int argc, char** argv)
{
/* check parameters */
if (argc - optind == 0) {
fprintf(stderr, "%s: no arguments provided %d, expected >=%d\n", argv[0], argc - optind, 1);
usage(argv[0], stderr);
}
/* file privided */
if (argc < 3)
{
fprintf(stderr, "%s: no input file provided\n", argv[0]);
usage(argv[0], stderr);
}
/* does file exist */
FILE *file;
if (file = fopen(argv[2], "r")) {
fclose(file);
printf("-> File %s accepted\n", argv[2]);
} else {
fprintf(stderr, "%s: file %s does not exist \n", argv[0], argv[2]);
usage(argv[0], stderr);
}
/* parse parameters */
static const struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{"encrypt", no_argument, NULL, 'e'},
{"decrypt", no_argument, NULL, 'd'},
{NULL, 0, NULL, 0}
};
static int cmd = 0; static char mymode = 0;
while ((cmd = getopt_long(argc, argv, "hved", longopts, NULL)) != -1) {
switch (cmd) {
case 'h':
usage(argv[0], stdout);
case 'v':
puts(VERSION);
return EXIT_SUCCESS;
case 'e':
printf("-> Running in encryption mode\n");
mymode = 1;
break;
case 'd':
printf("-> Running in decryption mode\n");
mymode = 2;
break;
default:
usage(argv[0], stderr);
}
}
/* get encryption key */
struct termios oflags, nflags;
char s[64];
/* disabling echo */
tcgetattr(fileno(stdin), &oflags);
nflags = oflags;
nflags.c_lflag &= ~ECHO;
nflags.c_lflag |= ECHONL;
if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
perror("tcsetattr");
return EXIT_FAILURE;
}
printf("-> Enter encryption key: ");
fgets(s, sizeof(s), stdin);
s[strlen(s) - 1] = 0;
/* printf("entered '%s'\n", s); */
char d[64];
printf("-> Confirm encryption key: ");
fgets(d, sizeof(d), stdin);
d[strlen(d) - 1] = 0;
/* printf("entered '%s'\n", d); */
/* restore terminal */
if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
perror("tcsetattr");
return EXIT_FAILURE;
}
if ( strcmp(s, d) != 0 ) {
printf("-> Encryption key did not match!\n");
return EXIT_FAILURE;
}
/* gcrypt init */
unsigned char *x;
unsigned i;
unsigned int l = gcry_md_get_algo_dlen(GCRY_MD_SHA256);
gcry_md_hd_t h;
gcry_md_open(&h, GCRY_MD_SHA256, GCRY_MD_FLAG_SECURE);
gcry_md_write(h, s, strlen(s));
x = gcry_md_read(h, GCRY_MD_SHA256);
/* Print SHA256
for (i = 0; i < l; i++) { printf("%02x", x[i]); }
printf("\n");
*/
/* Encrypt file */
char iniVector[16];
char *encBuffer = NULL;
FILE *fp, *fpout;
char *key = s;
gcry_cipher_hd_t hd;
int bufSize = 16, bytes, algo = GCRY_CIPHER_AES128, keyLength = 16, blkLength = 16;
memset(iniVector, 0, 16);
encBuffer = malloc(bufSize);
if ( mymode == 1 ) {
char outfile[200] = "encrypted-";
strcat(outfile,argv[2]);
if( access(outfile, F_OK ) != -1 ) { remove(outfile); }
fp = fopen(argv[2], "r");
fpout = fopen(outfile, "w");
printf("-> Encrypting %s to %s\n", argv[2], outfile);
gcry_cipher_open(&hd, algo, GCRY_CIPHER_MODE_CBC, 0);
gcry_cipher_setkey(hd, key, keyLength);
gcry_cipher_setiv(hd, iniVector, blkLength);
while(!feof(fp))
{
bytes = fread(encBuffer, 1, bufSize, fp);
if (!bytes) break;
while(bytes < bufSize)
encBuffer[bytes++] = 0x0;
gcry_cipher_encrypt(hd, encBuffer, bufSize, NULL, 0);
bytes = fwrite(encBuffer, 1, bufSize, fpout);
}
gcry_cipher_close(hd);
fclose(fp);
fclose(fpout);
}
/* Decrypt file */
if ( mymode == 2 ) {
char outfile[200] = "decrypted-";
strcat(outfile,argv[2]);
gcry_cipher_open(&hd, algo, GCRY_CIPHER_MODE_CBC, 0);
gcry_cipher_setkey(hd, key, keyLength);
gcry_cipher_setiv(hd, iniVector, blkLength);
if( access( outfile, F_OK ) != -1 ) { remove(outfile); }
fp = fopen(argv[2], "r");
fpout = fopen(outfile, "w");
printf("-> Decrypting %s to %s \n", argv[2], outfile);
while(!feof(fp))
{
bytes = fread(encBuffer, 1, bufSize, fp);
if (!bytes) break;
gcry_cipher_decrypt(hd, encBuffer, bufSize, NULL, 0);
bytes = fwrite(encBuffer, 1, bufSize, fpout);
}
gcry_cipher_close(hd);
free(encBuffer); encBuffer = NULL;
}
printf("-> Completed\n");
return 0;
}