-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptionManager.cpp
More file actions
213 lines (179 loc) · 7.09 KB
/
Copy pathEncryptionManager.cpp
File metadata and controls
213 lines (179 loc) · 7.09 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
#include <string>
#include <stdexcept>
#include "EncryptionManager.h"
#include <cstring>
#include "SHA256/include/SHA256.h"
#define DEBUG_ENCRYPTION 0
#define DEBUG_DECRYPTION 0
#if DEBUG_ENCRYPTION == 1 || DEBUG_DECRYPTION == 1
#include <iostream>
#endif
EncryptionManager::EncryptionManager(shared_ptr<unsigned char[]> &salt,
shared_ptr<unsigned char[]> &initializationVector,
string &password) {
this->salt = salt;
this->initializationVector = initializationVector;
this->password = password;
string s;
char* saltChars = (char*)salt.get();
for (int i = 0; i < SALT_SIZE; ++i) {
s += saltChars[i];
}
string saltedPassword = password + s;
// hash the salted password
SHA256 sha;
sha.update(saltedPassword);
this->key = shared_ptr<unsigned char[]>((unsigned char*)sha.digest());
}
void EncryptionManager::decrypt(unsigned char* cipherText, size_t cipherTextLength) {
#if DEBUG_DECRYPTION == 1
string str((char*)cipherText, cipherTextLength);
std::cout << "\nCiphertext: [" << str << "]\n";
#endif
for (int i = cipherTextLength - BLOCK_SIZE; i >= 0; i-= BLOCK_SIZE) {
unsigned char* block = cipherText + i;
const unsigned char* previousBlock;
if (i != 0) {
previousBlock = block - BLOCK_SIZE;
} else {
previousBlock = initializationVector.get();
}
# if DEBUG_DECRYPTION == 1
string blockStr((char*)block, BLOCK_SIZE);
string previousStr((char*)previousBlock, BLOCK_SIZE);
std::cout << "Selected block: [" << blockStr << "]\n"
<< "Selected previousBlock: [" << previousStr << "]\n";
#endif
this->decryptBlock(block, previousBlock);
#if DEBUG_DECRYPTION == 1
blockStr = string((char*)block, BLOCK_SIZE);
std::cout << "Block after decryption: [" << blockStr << "]\n";
#endif
}
}
void EncryptionManager::encrypt(unsigned char* plainText, size_t length) {
#if DEBUG_ENCRYPTION == 1
string str((char*)plainText, length);
std::cout << "\nPlaintext: [" << str << "]\n";
#endif
for (int i = 0; i < length; i += BLOCK_SIZE) {
unsigned char* block = plainText + i;
const unsigned char* previousBlock;
if (i != 0) {
previousBlock = block - BLOCK_SIZE;
} else {
previousBlock = initializationVector.get();
}
# if DEBUG_ENCRYPTION == 1
string blockStr((char*)block, BLOCK_SIZE);
string previousStr((char*)previousBlock, BLOCK_SIZE);
std::cout << "Selected block: [" << blockStr << "]\n"
<< "Selected previousBlock: [" << previousStr << "]\n";
#endif
this->encryptBlock(block, previousBlock);
#if DEBUG_ENCRYPTION == 1
blockStr = string((char*)block, BLOCK_SIZE);
std::cout << "Block after decryption: [" << blockStr << "]\n";
#endif
}
}
void EncryptionManager::decryptBlock(unsigned char* block, const unsigned char* previousBlock) {
#if DEBUG_DECRYPTION == 1
string keyStr((char*)this->key.get(), KEY_SIZE);
std::cout << "Key used: [" << keyStr << "]\n";
#endif
for (int i = 0; i < BLOCK_SIZE; ++i) {
block[i] = block[i] ^ this->key[i];
}
// for (int i = 0; i < BLOCK_SIZE; ++i) {
// block[i] = block[i] ^ previousBlock[i];
// }
}
void EncryptionManager::encryptBlock(unsigned char* block, const unsigned char* previousBlock) {
#if DEBUG_ENCRYPTION == 1
string keyStr((char*)this->key.get(), KEY_SIZE);
std::cout << "Key used: [" << keyStr << "]\n";
#endif
// for (int i = 0; i < BLOCK_SIZE; ++i) {
// block[i] = block[i] ^ previousBlock[i];
// }
for (int i = 0; i < BLOCK_SIZE; ++i) {
block[i] = block[i] ^ this->key[i];
}
}
void EncryptionManager::encrypt(vector<unsigned char>& plainText) {
this->addPadding(plainText);
this->encrypt(plainText.data(), plainText.size());
}
void EncryptionManager::decrypt(vector<unsigned char>& cipherText) {
if (cipherText.size() % BLOCK_SIZE != 0) {
throw std::invalid_argument("Ciphertext is of invalid size. It must be a product of BLOCK_SIZE * AMOUNT_OF_BLOCKS");
}
this->decrypt(cipherText.data(), cipherText.size());
this->removePadding(cipherText);
}
void EncryptionManager::addPadding(vector<unsigned char> &plainText) {
// Example:
// Ciphertext: 2f2f2f2f2f2f2f2f3e
// The ciphertext will be split into blocks: '2f2f2f2f2f2f2f2f' and '3e'.
// There is only 1 byte in the second block. The second block has 7 missing bytes.
// Add 7 missing bytes. Each byte will store amount of missing bytes, in this case 7.
//
// In case the ciphertext is of size x * BLOCK_SIZE, add a whole block of padding bytes
#if DEBUG_ENCRYPTION == 1
std::cout << "Plaintext size before padding: " << plainText.size() << ". ";
#endif
int numberOfBytesOutsideOfABlock = plainText.size() % BLOCK_SIZE;
int numberOfPaddingBytesToAdd;
if (numberOfBytesOutsideOfABlock != 0) {
numberOfPaddingBytesToAdd = BLOCK_SIZE - numberOfBytesOutsideOfABlock;
} else {
numberOfPaddingBytesToAdd = BLOCK_SIZE;
}
for (int i = 0; i < numberOfPaddingBytesToAdd; ++i) {
unsigned char c = numberOfPaddingBytesToAdd;
plainText.push_back(c);
}
#if DEBUG_ENCRYPTION == 1
std::cout << "Added " << numberOfPaddingBytesToAdd << " padding bytes Size of plainText: " << plainText.size() << "\n";
#endif
}
void EncryptionManager::removePadding(vector<unsigned char> &plainText) {
// Take value of the last byte. It will indicate how many padding blocks were added.
// Iterate over plainText backwards and count if there are as many padding blocks, as the value of the last byte indicates.
// Throw an exception if it's not the case.
// Remove padding bytes
#if DEBUG_DECRYPTION == 1
std::cout << "Size of plaintext before removal of padding: " << plainText.size() << ". ";
#endif
int numberOfPaddingBytes = plainText[plainText.size() - 1];
for (int i = plainText.size()-1; i > plainText.size() - numberOfPaddingBytes; --i) {
int c = plainText[i];
if (c != numberOfPaddingBytes) {
throw std::invalid_argument("Decryption error. Invalid number of padding bytes.");
}
}
plainText.resize(plainText.size() - numberOfPaddingBytes);
#if DEBUG_DECRYPTION == 1
std::cout << "Removed " << numberOfPaddingBytes << " padding bytes. Size of plaintext: " << plainText.size() << std::endl;
#endif
}
// get password
// hash + salt the password. Use different salts
// pad the message - append 0s
// compress the message - not required
// CBC mode of operation - Cipher block chaining
// create an initialization vector
// initialization vector XOR first plaintext block then encrypt = ciphertext
// previous ciphertext XOR current plaintext -> encrypt
// where do we derive salt from?
/*
* - could be written inside the file
*/
// can I use salt as initialization vector?
// the file content:
//<salt><initialization vector><cipher text>
// timestamp format
// store every part of data in unsigned char
// year = current year - 1970
// month, day, hours, minutes, seconds