-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.cpp
More file actions
210 lines (167 loc) · 5.22 KB
/
cipher.cpp
File metadata and controls
210 lines (167 loc) · 5.22 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
202
203
204
205
206
207
208
209
210
# include <errno.h>
# include <getopt.h>
# include <unistd.h>
# include <fcntl.h>
# include <string.h>
# include <iostream>
# include <openssl/evp.h>
# include <openssl/rand.h>
using namespace std;
#define BUFF_SIZE (1024*1024)
#define SALT_SIZE 8
#define AES_256_KEY_SIZE 32
#define AES_256_BLOCK_SIZE 16
void print_usage();
void loadingBar();
void the_magician(int, int, string, int, int);
int main (int argc, char * argv[])
{
int opt = 0;
char * in_path = NULL;
char * out_path = NULL;
int encrypt = 1;
int fd;
int fd2;
int salt_set = 1;
string pass;
static struct option long_option[] ={
{"encrypt", no_argument, 0, 'e'},
{"decrypt", no_argument, 0, 'd'},
{"nosalt", no_argument, 0, 'n'},
{"in", required_argument, 0, 'i'},
{"out", required_argument, 0, 'o'},
{0, 0, 0, 0 }
};
int long_index = 0;
while ((opt = getopt_long_only(argc, argv, "i:o:p:", long_option, &long_index))!=-1){
switch(opt){
case 'e': encrypt = 1; break;
case 'd': encrypt = 0; break;
case 'n': salt_set = 0; break;
case 'i': in_path = optarg; break;
case 'o': out_path = optarg; break;
case 'p': pass = optarg; break;
default : print_usage();
exit(EXIT_FAILURE);
}
}
if (in_path == NULL || out_path == NULL){
print_usage();
exit(EXIT_FAILURE);
}
else{
fd = open (in_path, O_RDWR);
if ( -1 == fd){
printf("\n open() failed with error [%s]\n",strerror(errno));
exit(1);
}
fd2 = open (out_path, O_RDWR|O_CREAT, 0666);
if (-1 == fd2){
printf("\n open() failed with error [%s]\n",strerror(errno));
exit(1);
}
}
if (pass.empty())
{
string pass2;
cout<<"Enter password: ";
cin >> pass;
cout <<"Verify password: ";
cin >>pass2;
if (pass != pass2){
cout << "password don't match. exiting..\n";
exit(EXIT_FAILURE);
}
}
the_magician (fd, fd2, pass, salt_set, encrypt);
}
void the_magician (int fd, int fd2, string password, int salt_set, int enc)
{
unsigned char saltbuff[SALT_SIZE];
unsigned char *salt;
int readSize,
writeSize;
char header[] = "Salted__";
if(enc == 1)
{
printf("\nEncrypting your file...\n");
if (salt_set == 1)
{
printf("Mixing salt for better taste ;)\n");
if(RAND_bytes(saltbuff, sizeof(saltbuff)) == 0)
printf("failed to generate salt\n");
write (fd2, header,sizeof(header)-1);
write (fd2, saltbuff, SALT_SIZE);
salt = saltbuff;
}
else{
//nosalt
salt = NULL;
}
}
else
{
printf("\ndecrypting your file...\n");
if (salt_set == 1)
{
lseek(fd, sizeof(header)-1, SEEK_SET);
if(read(fd, saltbuff,SALT_SIZE) == -1)
printf("failed to read salt from file\n");
salt = saltbuff;
}
else {
salt = NULL;
}
}
loadingBar();
//Setting up the cipher type
const EVP_CIPHER * cipher = EVP_aes_256_cbc();
//digest to use later
const EVP_MD * dgst = EVP_sha1();
unsigned char key[AES_256_KEY_SIZE], iv[AES_256_BLOCK_SIZE];
//taking care of password
char newPass [password.length()];
strcpy (newPass, password.c_str());
//derives a key from a password using a salt and iteration count
EVP_BytesToKey(cipher, dgst, salt,
(unsigned char *)newPass, strlen(newPass), 1, key, iv);
//Creates a cipher context
EVP_CIPHER_CTX ctx;
//initializes cipher contex ctx.
EVP_CIPHER_CTX_init(&ctx);
//set key and IV and other params
EVP_CipherInit_ex(&ctx, cipher, NULL, key, iv, enc);
unsigned char buf_in[BUFF_SIZE], buf_out[BUFF_SIZE + EVP_MAX_BLOCK_LENGTH];
readSize = read(fd, buf_in, BUFF_SIZE);
if(readSize <= 0)
cout<<"readSize <= 0\n"; //debug
// break;
if(EVP_CipherUpdate(&ctx, buf_out, &writeSize, buf_in, readSize) == 0)
{
EVP_CIPHER_CTX_cleanup(&ctx);
printf("Error: EVP_CipherUpdate");
}
write (fd2, buf_out, writeSize);
if(EVP_CipherFinal_ex(&ctx, buf_out, &writeSize) == 0)
{
EVP_CIPHER_CTX_cleanup(&ctx);
printf("Error: EVP_CipherFInal_ex");
}
write(fd2, buf_out, writeSize);
EVP_CIPHER_CTX_cleanup(&ctx);
exit(EXIT_SUCCESS);
}
void print_usage(){
printf("Usage: ./cipher -[e|d] [-nosalt] -in infile -out outfile -p password\n");
}
void loadingBar(){
cout << endl;
for (int i=0; i <= 100; ++i)
{
string progress = "[" + string(i, '*') + string(100-i, ' ') + "]";
cout << "\r\033[F" << "\n" << progress << flush;
usleep(10000);
cout <<" "<< i << "%";
}
cout<<" success!\n";
}