forked from mohabouz/CPP-AES-OpenSSL-Encrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
238 lines (182 loc) · 7.04 KB
/
utils.cpp
File metadata and controls
238 lines (182 loc) · 7.04 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
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>
#include "utils.h"
#include <cstring>
#include <stdlib.h>
#include <openssl/sha.h>
#include <base64.h>
#include <iostream>
using namespace std;
/**
* The two functions bellow are taken from the openssl official website :
* https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption
* You can read more there
*/
int encrypt(unsigned char *plaintext,
int plaintext_len,
unsigned char *key,
unsigned char *iv,
unsigned char *ciphertext) {
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new()))
handleErrors();
/*
* Initialise the encryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits
*/
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
handleErrors();
/*
* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len;
/*
* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
handleErrors();
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
int decrypt(unsigned char *ciphertext,
int ciphertext_len,
unsigned char *key,
unsigned char *iv,
unsigned char *plaintext) {
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new()))
handleErrors();
/*
* Initialise the decryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits
*/
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
handleErrors();
/*
* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary.
*/
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleErrors();
plaintext_len = len;
/*
* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
handleErrors();
plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
void handleErrors() {
ERR_print_errors_fp(stderr);
abort();
}
void hashPassword(const char *string, char outputBuffer[65]) {
#if OPENSSL_VERSION_NUMBER < 0x10100000L
# define EVP_MD_CTX_new EVP_MD_CTX_create
# define EVP_MD_CTX_free EVP_MD_CTX_destroy
#endif
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
unsigned char *hash = (unsigned char *) malloc(EVP_MAX_MD_SIZE * sizeof(char));
unsigned int hashSize = 65;
unsigned char * tempOutBuff = (unsigned char *) malloc(hashSize * sizeof(char));
EVP_DigestInit(ctx, EVP_sha256());
EVP_DigestUpdate(ctx, string, strlen(string));
EVP_DigestFinal(ctx, tempOutBuff, &hashSize);
for(int i = 0 ; i < hashSize ; i++) {
sprintf(outputBuffer + (i * 2), "%02x", tempOutBuff[i]);
}
outputBuffer[64] = 0;
}
string myEncrypt(string plainText, string pass) {
// Converting string to unsigned character (bytes) array
unsigned char *plainTextBytes = (unsigned char *) plainText.c_str();
// Allocate memory for hash bytes (chars) array with 65 elements
char *hash = (char *) malloc(sizeof(char) * 65);
// Allocate memory for cipher bytes (chars) array with 1024 elements
unsigned char *cipher = (unsigned char *) malloc(sizeof(char) * 1024);
// Here we hash the plain text password
hashPassword((const char *)pass.c_str(), hash);
// We convert the char array (hash variable) into an std::string
string hashStr(hash);
cout << "SHA256: " << hashStr << endl;
// The hash contains the encrytion key and the initialization vector -> (iv)
// so we divide it between the key and the iv using std::string method substr()
string keyStr = hashStr.substr(0, 16);
cout << "Key: " << keyStr << endl;
string iv = hashStr.substr(16, 16);
cout << "iv: " << iv << endl;
// here we initiate the encryption process
int cipherSize = encrypt(
plainTextBytes,
strlen((char *) plainTextBytes),
(unsigned char *) keyStr.c_str(),
(unsigned char *) iv.c_str(),
cipher
);
// We encode the cipher into a base64 encoded std::string
// string base64Cipher = base64_encode(
// cipher,
// cipherSize);
unsigned char base64Cipher[1024];
EVP_EncodeBlock(base64Cipher, cipher, cipherSize);
// We encode the iv to the base64
string base64Iv = base64_encode(iv);
// Append the base64 encode iv to the end of the base64 encoded cipher with a `:` in between
// string cipherStr((char *) base64Cipher.c_str());
string cipherStr((char *) base64Cipher);
return cipherStr;
}
string myDecrypt(string cipher, string pass) {
// string cipherDecoded = base64_decode(cipher);
// unsigned char * cipherBytes = (unsigned char *) cipherDecoded.c_str();
unsigned char *cipherBytes = (unsigned char *) malloc(sizeof(char) * 1024);
EVP_DecodeBlock(cipherBytes, (unsigned char *) cipher.c_str(), strlen(cipher.c_str()));
// Converting string to unsigned character (bytes) array
unsigned char *plainTextBytes = (unsigned char *) malloc(sizeof(char) * 1024);
// Allocate memory for hash bytes (chars) array with 65 elements
char *hash = (char *) malloc(sizeof(char) * 65);
// Here we hash the plain text password
hashPassword((const char *)pass.c_str(), hash);
// We convert the char array (hash variable) into an std::string
string hashStr(hash);
// The hash contains the encrytion key and the initialization vector -> (iv)
// so we divide it between the key and the iv using std::string method substr()
string keyStr = hashStr.substr(0, 16);
// cout << "Key: " << keyStr << endl;
string iv = hashStr.substr(16, 16);
// cout << "iv: " << iv << endl;
int len = decrypt(
cipherBytes,
strlen((char *)cipherBytes),
(unsigned char *) keyStr.c_str(),
(unsigned char *) iv.c_str(),
plainTextBytes
);
plainTextBytes[len] = '\0';
string plainText((char *) plainTextBytes);
return plainText;
}