-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaes.cpp
More file actions
138 lines (109 loc) · 4.07 KB
/
aes.cpp
File metadata and controls
138 lines (109 loc) · 4.07 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
#include "aes.h"
#include "encoding_utils.h"
std::string aes_128_ecb_encrypt(const std::string& data, const std::string& key) {
if (key.size() != 16) {
throw std::runtime_error("Key must be 16 bytes for AES-128");
}
std::string paddedData = padPKCS7(data, 16);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
throw std::runtime_error("Failed to create EVP context");
}
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), nullptr, reinterpret_cast<const unsigned char*>(key.data()), nullptr)) {
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("Encryption initialization failed.");
}
EVP_CIPHER_CTX_set_padding(ctx, 0);
std::string ciphertext;
ciphertext.resize(paddedData.size() + EVP_CIPHER_block_size(EVP_aes_128_ecb()));
int len = 0;
int ciphertextLen = 0;
if (1 != EVP_EncryptUpdate(ctx, reinterpret_cast<unsigned char*>(&ciphertext[0]), &len,
reinterpret_cast<const unsigned char*>(paddedData.data()),
static_cast<int>(paddedData.size()))) {
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("Encryption update failed");
}
ciphertextLen = len;
if (1 != EVP_EncryptFinal(ctx, reinterpret_cast<unsigned char*>(&ciphertext[0]) + len, &len)) {
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("Encryption finalization failed");
}
ciphertextLen += len;
EVP_CIPHER_CTX_free(ctx);
ciphertext.resize(ciphertextLen);
return ciphertext;
}
std::string aes_128_ecb_decrypt(const std::string& data, const std::string& key) {
if (key.size() != 16) {
throw std::runtime_error("Key must be 16 bytes for AES-128");
}
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
throw std::runtime_error("Failed to create EVP context");
}
if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), nullptr, reinterpret_cast<const unsigned char*>(key.data()), nullptr)) {
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("Decryption initialization failed.");
}
EVP_CIPHER_CTX_set_padding(ctx, 0);
std::string plaintext;
plaintext.resize(data.size() + EVP_CIPHER_block_size(EVP_aes_128_ecb()));
int len = 0;
int plaintextLen = 0;
if (1 != EVP_DecryptUpdate(ctx, reinterpret_cast<unsigned char*>(&plaintext[0]), &len,
reinterpret_cast<const unsigned char*>(data.data()),
static_cast<int>(data.size()))) {
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("Decryption update failed");
}
plaintextLen = len;
if (1 != EVP_DecryptFinal(ctx, reinterpret_cast<unsigned char*>(&plaintext[0]) + len, &len)) {
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("Decryption finalization failed");
}
plaintextLen += len;
EVP_CIPHER_CTX_free(ctx);
plaintext = unpadPKCS7(plaintext);
plaintext.resize(plaintextLen);
return plaintext;
}
std::string aes_128_cbc_encrypt(const std::string& data, const std::string& key, const std::string& iv, size_t blockSize) {
std::string paddedData = padPKCS7(data, blockSize);
std::vector<std::string> blocks = splitBlocks(data, blockSize);
std::string prevBlock = iv;
std::string ciphertext;
ciphertext.reserve(data.size());
for (const auto& block : blocks) {
std::string xored = fixedXor(block, prevBlock);
std::string encrypt = aes_128_ecb_encrypt(xored, key);
ciphertext += encrypt;
prevBlock = encrypt;
}
return ciphertext;
}
std::string aes_128_cbc_decrypt(const std::string& data, const std::string& key, std::string iv, size_t blockSize) {
std::vector<std::string> blocks = splitBlocks(data, blockSize);
std::string paddedPlaintext;
for (const auto& block : blocks) {
std::string decrypt = aes_128_ecb_decrypt(block, key);
decrypt = fixedXor(decrypt, iv);
paddedPlaintext += decrypt;
iv = block;
}
return unpadPKCS7(paddedPlaintext);
}
int findECBBlockSize(BlackBoxEncryptionFunc BlackBoxEncryption) {
std::string plaintext = "";
int len1 = BlackBoxEncryption(plaintext).length();
int len2 = len1;
while (len1 == len2) {
plaintext += "A";
len2 = BlackBoxEncryption(plaintext).length();
}
return len2 - len1;
}
bool isECBMode(int blockSize, BlackBoxEncryptionFunc BlackBoxEncryption) {
std::string plaintext(blockSize * 4, 'A');
return hasRepeatingBlocks(BlackBoxEncryption(plaintext), blockSize);
}