-
Notifications
You must be signed in to change notification settings - Fork 2
/
DES_ECB_Dec.c
231 lines (172 loc) · 3.85 KB
/
DES_ECB_Dec.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
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
226
227
228
229
230
231
/*
* DES_ECB Decryption
*
* Author : Omar Zairi
*
* Gets command line arguments
* -k Key File
* -v IV File
* -i Input binary file
* -o Ouput text File
*
* Reads the input file, which should be ciphered text
* Decrypts the plaintext using a key
* Writes the decrypted ciphertext to the output file
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>
/*
* Returns a char array filled with the file contents
*/
unsigned char *stringFromFile( char filename[] , char type[] )
{
unsigned char *str;
FILE *fp;
fp = fopen(filename, type);
if ( !fp )
{
printf("ERROR. Couldn't read file.\n");
exit(1);
}
// Get the end position of the file
fseek (fp , 0 , SEEK_END);
int length = ftell (fp);
fseek (fp , 0 , SEEK_SET);
// Allocate memory for the string
str = malloc(length + 1);
int c , n = 0;
// Read in each char from file
while ( (c = fgetc(fp)) != EOF )
{
str[n++] = (unsigned char)c;
}
str[n] = '\0';
fclose(fp);
return str;
}
/*
* Decrypts a string containing ciphertext
*/
char *DES_decrypt(EVP_CIPHER_CTX *de, unsigned char *ciphertext, int *cipher_len)
{
/* Allocate memory for plaintext from size of ciphertext */
int plain_len = *cipher_len;
int tmp;
unsigned char *plaintext = malloc(plain_len);
/* Decrypt the ciphertext */
EVP_DecryptUpdate(de, plaintext, &plain_len, ciphertext, *cipher_len);
EVP_DecryptFinal_ex(de, plaintext + plain_len, &tmp);
*cipher_len = plain_len + tmp;
return (char *)plaintext;
}
int main(int argc, char *argv[])
{
char *keyFile = NULL;
char *inputFile = NULL;
char *outputFile = NULL;
char *key = NULL;
char *plaintext;
unsigned char *cipher;
int cipher_len;
int tmp;
/* Check arguments */
int i;
for (i = 1; i < argc; i++)
{
// Get key
if (strcmp( "-k" , argv[i] ) == 0)
{
// try to get argument after option
if (argv[i + 1] != NULL)
{
// Get key file name
keyFile = argv[i + 1];
key = stringFromFile(keyFile , "r");
// Check if key is 8 bytes long
if ( strlen(key)-1 < 8 )
{
printf("ERROR. Key is not long enough.\n");
exit(1);
}
}
else
{
printf("No key file specified\n");
exit(1);
}
}
// Get the input file
else if (strcmp( "-i" , argv[i] ) == 0)
{
if (argv[i + 1] != NULL)
{
// Get input file
inputFile = argv[i + 1];
printf("Input file: %s\n", inputFile);
}
else
{
printf("No input file specified\n");
exit(1);
}
}
// Get output file
else if (strcmp( "-o" , argv[i] ) == 0)
{
if (argv[i + 1] != NULL)
{
// Get output file name
outputFile = argv[i + 1];
printf("Output file: %s\n", outputFile);
}
else
{
printf("No output file specified\n");
exit(1);
}
}
}
/* Check that input and output files have been set */
if( !inputFile || !outputFile)
{
printf("No input or output file specified.\n");
exit(1);
}
/* Decryption Process */
if ( key )
{
/* Get cipher text from file*/
cipher = stringFromFile(inputFile,"rb");
/* get ciphertext length and put in length */
FILE *outf;
outf = fopen("ciphersize.txt","r");
fscanf(outf,"%d",&cipher_len);
fclose(outf);
/* Initialize the EVP key */
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
/* Initialize key and iv */
EVP_DecryptInit_ex(&ctx, EVP_des_ecb(), NULL, key, NULL);
/* Decrypt the ciphertext into plaintext */
plaintext = DES_decrypt(&ctx, cipher, &cipher_len);
/* Write plaintext to file */
FILE *wf = fopen(outputFile , "w");
for(int x = 0; plaintext[x] ; x++)
fputc(plaintext[x],wf);
fclose(wf);
/* Free the evp key */
EVP_CIPHER_CTX_cleanup(&ctx);
free(key);
free(plaintext);
free(cipher);
}
/* ERRORs */
else if( !key )
{
printf("No key file specified.\n");
exit(1);
}
return 0;
}