-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileEncrypror.cpp
130 lines (104 loc) · 4.03 KB
/
FileEncrypror.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
#include <cryptopp/aes.h>
#include <cryptopp/crc.h>
#include <cryptopp/files.h>
#include <cryptopp/filters.h>
#include <cryptopp/hex.h>
#include <cryptopp/modes.h>
#include <bitset>
#include <fstream>
#include <sstream>
#include "FileEncryptor.hpp"
#include "LoggerModule.hpp"
namespace CRCUtils
{
uint32_t calculateCRC(const std::string &data)
{
CryptoPP::CRC32 crc;
crc.Update(reinterpret_cast<const CryptoPP::byte *>(data.c_str()),
data.size());
uint32_t crcValue = 0;
crc.Final(reinterpret_cast<CryptoPP::byte *>(&crcValue));
DEBUG_LOG("Computed CRC value: {}", crcValue);
return crcValue;
}
std::string encryptToString(const std::string &plainText, const byte *key,
const byte *iv)
{
std::string cipherText;
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption encryptor;
encryptor.SetKeyWithIV(key, CryptoPP::AES::DEFAULT_KEYLENGTH, iv);
CryptoPP::StringSource ss(
plainText, true,
new CryptoPP::StreamTransformationFilter(
encryptor, new CryptoPP::StringSink(cipherText)));
std::string snippet = cipherText.substr(
0, std::min(cipherText.size(), static_cast<size_t>(8)));
DEBUG_LOG("Text encrypted successfully (snippet): {}", snippet);
return cipherText;
}
std::string encryptFileToString(const std::string &inputFileName,
const CryptoPP::byte *key,
const CryptoPP::byte *iv)
{
DEBUG_LOG("Reading \"{}\" content and converting to string for encryption.",
inputFileName);
std::ifstream infile(inputFileName, std::ios::binary);
if (!infile.is_open())
{
ERROR_LOG("Failed to open input \"{}\". Does it exist?", inputFileName);
return "";
}
std::string plainText((std::istreambuf_iterator<char>(infile)),
std::istreambuf_iterator<char>());
return encryptToString(plainText, key, iv);
}
std::string decryptToString(const std::string &cipherText, const byte *key,
const byte *iv)
{
std::string decryptedText;
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption decryptor;
decryptor.SetKeyWithIV(key, CryptoPP::AES::DEFAULT_KEYLENGTH, iv);
CryptoPP::StringSource ss(
cipherText, true,
new CryptoPP::StreamTransformationFilter(
decryptor, new CryptoPP::StringSink(decryptedText)));
std::string snippet = decryptedText.substr(
0, std::min(decryptedText.size(), static_cast<size_t>(8)));
DEBUG_LOG("Text decrypted successfully (snippet): {}", snippet);
return decryptedText;
}
} // namespace CRCUtils
FileEncryptor::FileEncryptor(const std::string &keyHex,
const std::string &inputFileName)
: inputFileName_(inputFileName)
{
// Convert key from hex to byte array
CryptoPP::StringSource ss(keyHex, true,
new CryptoPP::HexDecoder(new CryptoPP::ArraySink(
key_, CryptoPP::AES::DEFAULT_KEYLENGTH)));
DEBUG_LOG(
"Initializing FileEncryptor: Converted key from hex to byte array: {}",
keyHex);
// Set the IV to zeros for simplicity (Not recommended for real-world
// applications!)
memset(iv_, 0x00, CryptoPP::AES::BLOCKSIZE);
}
void FileEncryptor::decryptToFile(const std::string &cipherText,
const std::string &outputFileName)
{
std::string decryptedText =
CRCUtils::decryptToString(cipherText, key_, iv_);
// Write decrypted text to file
CryptoPP::FileSink file(outputFileName.c_str());
file.Put(reinterpret_cast<const CryptoPP::byte *>(decryptedText.data()),
decryptedText.size());
}
std::tuple<std::string, uint32_t> FileEncryptor::encryptAndComputeCRC()
{
// Encrypt file content to a string
std::string encryptedData =
CRCUtils::encryptFileToString(inputFileName_, key_, iv_);
// Compute CRC value
uint32_t crcValue = CRCUtils::calculateCRC(encryptedData);
return std::make_tuple(encryptedData, crcValue);
}