-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileReader.cpp
230 lines (177 loc) · 7.85 KB
/
FileReader.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//==================================================================================
// Name : FileReader.cpp
// Author : Julien Thibodeau
// Description : Print saved password in C++, Ansi-style
//==================================================================================
#include "FileReader.h"
#include "VariadicTable.h"
FileReader::FileReader()
{
}
FileReader::~FileReader()
{
}
std::string FileReader::aesCTRDecrypt(std::string KEY, std::string IV, std::string infoToDecrypt)
{
CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption aes((unsigned char *)KEY.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH, (unsigned char *)IV.c_str());
std::string decrypted;
CryptoPP::StringSource decrypt(infoToDecrypt, true, new CryptoPP::HexDecoder(new CryptoPP::StreamTransformationFilter(aes, new CryptoPP::StringSink(decrypted))));
return decrypted;
}
std::string FileReader::aesCBCDecrypt(std::string KEY, std::string IV, std::string infoToDecrypt)
{
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption aes((unsigned char *)KEY.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH, (unsigned char *)IV.c_str());
std::string decrypted;
CryptoPP::StringSource decrypt(infoToDecrypt, true, new CryptoPP::HexDecoder(new CryptoPP::StreamTransformationFilter(aes, new CryptoPP::StringSink(decrypted))));
return decrypted;
}
std::string FileReader::DecryptJsonFile(SHA512keysReader sha512keys)
{
std::ifstream file(FileReader::pathFile);
std::string data((std::istreambuf_iterator<char>(file)), (std::istreambuf_iterator<char>()));
data.erase(std::remove_if(data.begin(), data.end(), [](unsigned char x) { return std::isspace(x); }), data.end());
return FileReader::aesCTRDecrypt(sha512keys.key, sha512keys.iv, data);
}
SHA512keysReader FileReader::generateKeyIv(std::string pwdContent)
{
unsigned char iv[CryptoPP::AES::BLOCKSIZE];
CryptoPP::AutoSeededRandomPool generator;
generator.GenerateBlock(iv, CryptoPP::AES::BLOCKSIZE);
unsigned char key[CryptoPP::AES::DEFAULT_KEYLENGTH];
CryptoPP::HKDF<CryptoPP::SHA512> hkdf;
hkdf.DeriveKey(key, CryptoPP::AES::DEFAULT_KEYLENGTH, (unsigned char *)pwdContent.data(), pwdContent.size(), iv, CryptoPP::AES::BLOCKSIZE, NULL, 0);
std::string encodedKey;
CryptoPP::StringSource encodeKey(key, sizeof(key), true, new CryptoPP::HexEncoder(new CryptoPP::StringSink(encodedKey)));
std::string encodedIv;
CryptoPP::StringSource encodeIv(iv, sizeof(iv), true, new CryptoPP::HexEncoder(new CryptoPP::StringSink(encodedIv)));
SHA512keysReader sha512keys;
sha512keys.key = encodedKey;
sha512keys.iv = encodedIv;
return sha512keys;
}
SHA512keysReader FileReader::generateKeyFromPwdAndIv(std::string pwdContent, std::string encodedIv)
{
unsigned char key[CryptoPP::AES::DEFAULT_KEYLENGTH];
std::string decodedIvHex;
CryptoPP::StringSource decodeIvHex(encodedIv, true, new CryptoPP::HexDecoder(new CryptoPP::StringSink(decodedIvHex)));
byte *result = (byte *)decodedIvHex.data();
CryptoPP::HKDF<CryptoPP::SHA512> hkdf;
hkdf.DeriveKey(key, CryptoPP::AES::DEFAULT_KEYLENGTH, (unsigned char *)pwdContent.data(), pwdContent.size(), result, CryptoPP::AES::BLOCKSIZE, NULL, 0);
std::string encodedKey;
CryptoPP::StringSource encodeKey(key, sizeof(key), true, new CryptoPP::HexEncoder(new CryptoPP::StringSink(encodedKey)));
SHA512keysReader sha512keys;
sha512keys.key = encodedKey;
sha512keys.iv = encodedIv;
return sha512keys;
}
bool FileReader::validateJsonContentData(std::string decryptedJson)
{
std::stringstream stream(decryptedJson);
Json::Value root;
stream >> root;
Json::Value infoArray = root["accounts"];
if (infoArray.size() == 0)
{
std::cout << "Need to have data. Use option 1." << std::endl;
return false;
}
return true;
}
bool FileReader::validatepwdFile(std::string pwdFile)
{
if (!isFileExistReader(FileReader::pathFile))
{
std::cout << "Need to have data. Use option 1." << std::endl;
return false;
}
SHA512keysReader sha512keysFile = FileReader::generateKeyFromPwdAndIv(pwdFile, FileReader::ivFile);
std::string decryptedJson = FileReader::DecryptJsonFile(sha512keysFile);
if (decryptedJson[0] != '{')
{
std::cout << "Invalid password for decrypt file" << std::endl;
return false;
}
return FileReader::validateJsonContentData(decryptedJson);
}
bool FileReader::validatepwdContent(std::string pwdFile, std::string pwdContent)
{
if (!isFileExistReader(FileReader::pathFile))
{
std::cout << "Need to have data. Use option 1." << std::endl;
return false;
}
SHA512keysReader sha512keysFile = FileReader::generateKeyFromPwdAndIv(pwdFile, FileReader::ivFile);
std::string decryptedJson = FileReader::DecryptJsonFile(sha512keysFile);
std::stringstream stream(decryptedJson);
Json::Value root;
stream >> root;
Json::Value infoArray = root["accounts"];
if (infoArray.size() == 0)
{
return true;
}
SHA512keysReader sha512keysContent = FileReader::generateKeyFromPwdAndIv(pwdContent, infoArray[0]["iv"].asString());
try
{
FileReader::aesCBCDecrypt(sha512keysContent.key, sha512keysContent.iv, infoArray[0]["user"].asString());
return true;
}
catch (const std::exception &e)
{
std::cout << "Invalid password for data entries" << std::endl;
return false;
}
}
int FileReader::printListEntriesInfosCensored(std::string pwdFile)
{
SHA512keysReader sha512keysFile = FileReader::generateKeyFromPwdAndIv(pwdFile, FileReader::ivFile);
std::string decryptedJson = FileReader::DecryptJsonFile(sha512keysFile);
std::stringstream stream(decryptedJson);
Json::Value root;
stream >> root;
Json::Value infoArray = root["accounts"];
VariadicTable<int, std::string, std::string, std::string> table({"line", "url", "user", "pwd"});
int numberData = 0;
int numberLine = 1;
std::string url;
for (int i = 0; i < infoArray.size(); i++)
{
url = infoArray[i]["url"].toStyledString().substr(1, infoArray[i]["url"].toStyledString().length() - 3);
table.addRow({numberLine, url, "*****", "*****"});
numberLine++;
numberData++;
}
table.print(std::cout);
return numberData;
}
void FileReader::printListEntriesInfosDecrypted(std::string pwdFile, std::string pwdContent)
{
SHA512keysReader sha512keysFile = FileReader::generateKeyFromPwdAndIv(pwdFile, FileReader::ivFile);
std::string decryptedJson = FileReader::DecryptJsonFile(sha512keysFile);
std::stringstream stream(decryptedJson);
Json::Value root;
stream >> root;
Json::Value infoArray = root["accounts"];
VariadicTable<int, std::string, std::string, std::string> table({"line", "url", "user", "pwd"});
int numberLine = 1;
std::string url;
std::string encodedIv;
SHA512keysReader sha512KeysReader;
std::string encodedUser;
std::string encodedPwd;
std::string user;
std::string pwd;
for (int i = 0; i < infoArray.size(); i++)
{
url = infoArray[i]["url"].toStyledString().substr(1, infoArray[i]["url"].toStyledString().length() - 3);
encodedIv = infoArray[i]["iv"].toStyledString().substr(1, infoArray[i]["iv"].toStyledString().length() - 3);
sha512KeysReader = FileReader::generateKeyFromPwdAndIv(pwdContent, encodedIv);
encodedUser = infoArray[i]["user"].toStyledString().substr(1, infoArray[i]["user"].toStyledString().length() - 3);
encodedPwd = infoArray[i]["pwd"].toStyledString().substr(1, infoArray[i]["pwd"].toStyledString().length() - 3);
user = FileReader::aesCBCDecrypt(sha512KeysReader.key, sha512KeysReader.iv, encodedUser);
pwd = FileReader::aesCBCDecrypt(sha512KeysReader.key, sha512KeysReader.iv, encodedPwd);
table.addRow({numberLine, url, user, pwd});
numberLine++;
}
table.print(std::cout);
}