-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.cpp
214 lines (175 loc) · 4.23 KB
/
code.cpp
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
//command to compile the file: g++ code.cpp -lssl -lcrypto
//command to run the program: ./a.out input_file encryption_output_file decryption_output_file
#include "openssl/blowfish.h"
#include "openssl/conf.h"
#include "openssl/evp.h"
#include "openssl/aes.h"
#include "openssl/err.h"
#include "openssl/ossl_typ.h"
#include "openssl/crypto.h"
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <strings.h>
#define IP_SIZE 512
#define OP_SIZE 516
unsigned char key[32];
unsigned char iv[8];
int
generate_key ()
{
int i, j, fd;
if ((fd = open ("/dev/random", O_RDONLY)) == -1)
perror ("open error");
if ((read (fd, key, 32)) == -1)
perror ("read key error");
if ((read (fd, iv, 8)) == -1)
perror ("read iv error");
printf("128 bit key:\n");
for (i = 0; i < 16; i++)
printf ("%d \t", key[i]);
printf ("\n ------ \n");
printf("Initialization vector\n");
for (i = 0; i < 8; i++)
printf ("%d \t", iv[i]);
printf ("\n ------ \n");
close (fd);
return 0;
}
int
decrypt (int infd, int outfd)
{
unsigned char outbuf[IP_SIZE];
int olen, tlen, n;
unsigned char inbuff[OP_SIZE];
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
//EVP_CIPHER_CTX_init (ctx);
EVP_DecryptInit (ctx, EVP_bf_cbc (), key, iv);
for (;;)
{
bzero (&inbuff, OP_SIZE);
if ((n = read (infd, inbuff, OP_SIZE)) == -1)
{
perror ("read error");
break;
}
else if (n == 0)
break;
bzero (&outbuf, IP_SIZE);
if (EVP_DecryptUpdate (ctx, outbuf, &olen, inbuff, n) != 1)
{
printf ("error in decrypt update\n");
return 0;
}
if (EVP_DecryptFinal (ctx, outbuf + olen, &tlen) != 1)
{
printf ("error in decrypt final\n");
return 0;
}
olen += tlen;
if ((n = write (outfd, outbuf, olen)) == -1)
perror ("write error");
}
//EVP_CIPHER_CTX_cleanup (ctx);
return 1;
}
int
encrypt (int infd, int outfd)
{
unsigned char outbuf[OP_SIZE];
int olen, tlen, n;
unsigned char inbuff[IP_SIZE];
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
//EVP_CIPHER_CTX_init (ctx);
EVP_EncryptInit (ctx, EVP_bf_cbc (), key, iv);
for (;;)
{
bzero (&inbuff, IP_SIZE);
if ((n = read (infd, inbuff, IP_SIZE)) == -1)
{
perror ("read error");
break;
}
else if (n == 0)
break;
if (EVP_EncryptUpdate (ctx, outbuf, &olen, inbuff, n) != 1)
{
printf ("error in encrypt update\n");
return 0;
}
if (EVP_EncryptFinal (ctx, outbuf + olen, &tlen) != 1)
{
printf ("error in encrypt final\n");
return 0;
}
olen += tlen;
if ((n = write (outfd, outbuf, olen)) == -1)
perror ("write error");
}
//EVP_CIPHER_CTX_cleanup (ctx);
return 1;
}
int
main (int argc, char *argv[])
{
int flags1 = 0, flags2 = 0, outfd, infd, decfd;
mode_t mode;
char choice, temp;
int done = 0, n, olen;
bzero (&key, 16);
bzero (&iv, 8);
bzero (&mode, sizeof (mode));
flags1 = flags1 | O_RDONLY;
flags2 = flags2 | O_RDONLY;
flags2 = flags2 | O_WRONLY;
flags2 = flags2 | O_CREAT;
mode = mode | S_IRUSR;
mode = mode | S_IWUSR;
while (!done)
{
printf ("E - Encrypt a file\n");
printf ("D - Decrypt a file\n");
printf ("G - Generate a key\n");
printf ("Q - Quit\n");
choice = getchar ();
temp = getchar ();
switch (choice)
{
case 'e':
case 'E':
if ((infd = open (argv[1], flags1, mode)) == -1)
perror ("open input file error");
if ((outfd = open (argv[2], flags2, mode)) == -1)
perror ("open output file error");
encrypt (infd, outfd);
close (infd);
close (outfd);
break;
case 'd':
case 'D':
if ((outfd = open (argv[2], flags1, mode)) == -1)
perror ("open output file error");
if ((decfd = open (argv[3], flags2, mode)) == -1)
perror ("open output file error");
decrypt (outfd, decfd);
close (outfd);
fsync (decfd);
close (decfd);
break;
case 'g':
case 'G':
generate_key ();
break;
case 'Q':
case 'q':
done = 1;
break;
default:
printf ("ERROR: Unrecognized command. Try again.\n");
break;
}
}
return 0;
}