-
Notifications
You must be signed in to change notification settings - Fork 2
/
DES_ECB_Enc.c
240 lines (177 loc) · 4.01 KB
/
DES_ECB_Enc.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
232
233
234
235
236
237
238
239
240
/*
* DES_ECB Encryption
*
* Author : Omar Zairi
*
* Gets command line arguments
* -k Key File
* -i Input text file
* -o Ouput binary File
*
* Reads the input file, which should be plain text
* Encrypts the plaintext using a key
* Writes the encrypted 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
*/
char *stringFromFile( char filename[] , char type[] )
{
unsigned char *str;
/* Open file */
FILE *fp;
fp = fopen(filename, type);
/* Check if file is readable */
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;
}
/* Encrypts a given string of plaintext
* Returns a ciphertext string
*/
unsigned char *DES_encrypt(EVP_CIPHER_CTX *en, unsigned char *plaintext, int *plain_len)
{
/* Allocate memory from size of plaintext */
int cipher_len = *plain_len + 8;
int tmp;
unsigned char *ciphertext = malloc(cipher_len);
/* Encrypt the plaintext to ciphertext */
EVP_EncryptUpdate(en, ciphertext, &cipher_len, plaintext, *plain_len);
EVP_EncryptFinal_ex(en, ciphertext+cipher_len, &tmp);
*plain_len = cipher_len + tmp;
return ciphertext;
}
int main(int argc, char *argv[])
{
char *keyFile = NULL;
char *inputFile = NULL;
char *outputFile = NULL;
char *key = NULL;
char *plain;
unsigned char *ciphertext;
int plain_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 name
inputFile = argv[i + 1];
// get plaintext from input file
plain = stringFromFile(inputFile , "r");
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);
}
/* Encyption Process */
// if iv is set, encrypt with iv and key and write to file
if ( key )
{
/* Get the length of plaintext */
int input_len = strlen(plain)+1;
/* Initialize the EVP key */
EVP_CIPHER_CTX en;
EVP_CIPHER_CTX_init(&en);
EVP_EncryptInit_ex(&en, EVP_des_ecb(), NULL, key, NULL);
/* Encrypt */
ciphertext = DES_encrypt(&en, (unsigned char *)plain, &input_len);
/* Write ciphertext to file */
FILE *inf = fopen(outputFile,"wb");
int fl = input_len;
int i = 0;
while(i < fl)
fputc(ciphertext[i++],inf);
fclose(inf);
/* Write cipher size to file */
inf = fopen("ciphersize.txt","w");
fprintf(inf, "%d", input_len);
fclose(inf);
/* Free the EVP key */
EVP_CIPHER_CTX_cleanup(&en);
/* Free variables */
free(key);
free(plain);
free(ciphertext);
}
/* ERRORs */
else if( !key )
{
printf("No key file specified.\n");
exit(1);
}
return 0;
}