-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
248 lines (197 loc) · 8.85 KB
/
Copy pathmain.cpp
File metadata and controls
248 lines (197 loc) · 8.85 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
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
241
242
243
244
245
246
247
248
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <openssl/evp.h>
#include <iostream>
#include <vector>
#include <fstream>
typedef std::vector<unsigned char> Buffer;
namespace Crypto {
int EncryptAES(const Buffer& plaintext, const Buffer& key, Buffer &ciphertext) {
Buffer iv(AES_BLOCK_SIZE);
if (RAND_bytes(iv.data(), AES_BLOCK_SIZE) != 1) {
std::cerr << "Error generating random IV." << std::endl;
return 1;
}
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
if (ctx == nullptr) {
std::cerr << "Error creating EVP_CIPHER_CTX." << std::endl;
return 2;
}
if (EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key.data(), iv.data()) != 1) {
std::cerr << "Error initializing AES encryption." << std::endl;
EVP_CIPHER_CTX_free(ctx);
return 3;
}
EVP_CIPHER_CTX_set_padding(ctx, EVP_PADDING_PKCS7);
ciphertext.resize(plaintext.size() + AES_BLOCK_SIZE);
int len = 0;
int ciphertext_len = 0;
if (EVP_EncryptUpdate(ctx, ciphertext.data(), &len, plaintext.data(), plaintext.size()) != 1) {
std::cerr << "Error encrypting data." << std::endl;
EVP_CIPHER_CTX_free(ctx);
return 4;
}
ciphertext_len = len;
if (EVP_EncryptFinal_ex(ctx, ciphertext.data() + ciphertext_len, &len) != 1) {
std::cerr << "Error finalizing AES encryption." << std::endl;
EVP_CIPHER_CTX_free(ctx);
return 5;
}
ciphertext_len += len;
ciphertext.resize(ciphertext_len);
ciphertext.insert(ciphertext.begin(), iv.begin(), iv.end());
EVP_CIPHER_CTX_free(ctx);
return 0;
}
int DecryptAES(const Buffer& ciphertext, const Buffer& key, Buffer &plaintext) {
Buffer iv(ciphertext.begin(), ciphertext.begin() + AES_BLOCK_SIZE);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
if (ctx == nullptr) {
std::cerr << "Error creating EVP_CIPHER_CTX." << std::endl;
return 1;
}
if (EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key.data(), iv.data()) != 1) {
std::cerr << "Error initializing AES decryption." << std::endl;
EVP_CIPHER_CTX_free(ctx);
return 2;
}
EVP_CIPHER_CTX_set_padding(ctx, EVP_PADDING_PKCS7);
plaintext.resize(ciphertext.size() - AES_BLOCK_SIZE);
int len = 0;
int plaintext_len = 0;
if (EVP_DecryptUpdate(ctx, plaintext.data(), &len, ciphertext.data() + AES_BLOCK_SIZE, ciphertext.size() - AES_BLOCK_SIZE) != 1) {
std::cerr << "Error decrypting data." << std::endl;
EVP_CIPHER_CTX_free(ctx);
return 3;
}
plaintext_len = len;
if (EVP_DecryptFinal_ex(ctx, plaintext.data() + plaintext_len, &len) != 1) {
std::cerr << "Error finalizing AES decryption." << std::endl;
EVP_CIPHER_CTX_free(ctx);
return 4;
}
plaintext_len += len;
plaintext.resize(plaintext_len);
EVP_CIPHER_CTX_free(ctx);
return 0;
}
int GenerateRandomKey(Buffer &key) {
key.resize(32);
if (RAND_bytes(key.data(), key.size()) != 1) {
return 1;
}
return 0;
}
};
int DumpVector(const Buffer &v, std::ofstream &file) {
for (const auto &byte : v) {
file << byte;
}
return v.size();
}
int LoadVector(Buffer &v, std::ifstream &file) {
file.seekg(0, std::ios::end);
std::streampos fileSize = file.tellg();
file.seekg(0, std::ios::beg);
v.resize(fileSize);
file.read(reinterpret_cast<char *>(v.data()), fileSize);
return v.size();
}
namespace User {
void GenerateRandomKey(const std::string &filename) {
Buffer key;
std::ofstream file(filename);
if (Crypto::GenerateRandomKey(key) != 0) {
std::cerr << "Error generating random key." << std::endl;
return;
}
DumpVector(key, file);
file.close();
}
void EncryptFile(const std::string &key_filename, const std::string &data_filename, const std::string &output_filename) {
std::ifstream key_file(key_filename);
std::ifstream data_file(data_filename);
std::ofstream output_file(output_filename);
Buffer key, data, output;
LoadVector(key, key_file);
LoadVector(data, data_file);
std::cout << "key has " << key.size() << " bytes" << std::endl;
std::cout << "data has " << data.size() << " bytes" << std::endl;
if (Crypto::EncryptAES(data, key, output) != 0) {
std::cerr << "Couldn't encrypt file!" << std::endl;
return;
}
DumpVector(output, output_file);
output_file.close();
std::cout << "Written " << output.size() << " bytes of encrypted data." << std::endl;
}
void DecryptFile(const std::string &key_filename, const std::string &data_filename, const std::string &output_filename) {
std::ifstream key_file(key_filename);
std::ifstream encrypted_data_file(data_filename);
std::ofstream output_file(output_filename);
Buffer key, encrypted_data, output;
LoadVector(key, key_file);
LoadVector(encrypted_data, encrypted_data_file);
std::cout << "key has " << key.size() << " bytes" << std::endl;
std::cout << "encrypted data has " << encrypted_data.size() << " bytes" << std::endl;
if (Crypto::DecryptAES(encrypted_data, key, output) != 0) {
std::cerr << "Couldn't decrypt file!" << std::endl;
return;
}
DumpVector(output, output_file);
output_file.close();
std::cout << "Written " << output.size() << " bytes of decrypted data." << std::endl;
}
};
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "Insufficient arguments provided!" << std::endl;
std::cerr << "See `" << argv[0] << " h` for help" << std::endl;
return 1;
}
if (argv[1][0] == 'h') {
std::cout << "This is Vault. The software that keeps your data 100\% safe and encrypted," << std::endl\
<< "where you are the one and only with access to your keys and data." << std::endl << std::endl;
std::cout << "Usage" << std::endl\
<< "-----" << std::endl\
<< " - Generation:" << std::endl\
<< " " << argv[0] << " g <output file>" << std::endl\
<< " Generates a random and safe key saved in <output file>" << std::endl << std::endl\
<< " Example: " << argv[0] << " g master.key" << std::endl\
<< " This command generates a random and secure key and saves it in `master.key` file" << std::endl << std::endl\
<< " - Encryption:" << std::endl\
<< " " << argv[0] << " e <key file> <input file> <output file>" << std::endl\
<< " Encrypts <input file> with key from <key file> and saves in <output file>" << std::endl << std::endl\
<< " Example: " << argv[0] << " e master.key data.txt" << std::endl\
<< " This command encrypts the contents of the `data.txt` file using the key stored in" <<std::endl\
<< " `master.key` and saves the encrypted data in `encrypted.bin`." << std::endl << std::endl\
<< " - Decryption:" << std::endl\
<< " " << argv[0] << " d <key file> <input file> <output file>" << std::endl\
<< " Decrypts <input file> with key from <key file> and saves in <output file>" << std::endl << std::endl\
<< " Example: " << argv[0] << " d master.key encrypted.bin data.dec" << std::endl\
<< " This command decrypts the contents of the `encrypted.bin` file using the key stored in" <<std::endl\
<< " `master.key` and saves the encrypted data in `data.dec`." << std::endl << std::endl\
<< "Note: The <key file> should be a text/binary file containing the encryption key." << std::endl\
<< " The <input file> and <output file> can be any file type." << std::endl;
return 0;
}
if ((argc < 3 and argv[1][0] == 'g') or (argc < 4 and argv[1][0] != 'g')) {
std::cerr << "Insufficient arguments provided!" << std::endl;
return 1;
}
switch (argv[1][0]) {
case 'g':
User::GenerateRandomKey(argv[2]);
break;
case 'e':
User::EncryptFile(argv[2], argv[3], argv[4]);
break;
case 'd':
User::DecryptFile(argv[2], argv[3], argv[4]);
break;
default:
std::cerr << "Invalid instruction (" << argv[1] << ")\n";
break;
}
return 0;
}