-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathopenssl_aes.cc
More file actions
403 lines (328 loc) · 11.9 KB
/
openssl_aes.cc
File metadata and controls
403 lines (328 loc) · 11.9 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "openssl_common.hpp"
#include <gtest/gtest.h>
#include <stdexcept>
AesKey generateAesKey()
{
unsigned char key[32]; // 256-bit key
unsigned char iv[16]; // 128-bit IV
if (RAND_bytes(key, sizeof(key)) != 1 || RAND_bytes(iv, sizeof(iv)) != 1) {
handleOpenSSLError();
}
AesKey aesKey;
aesKey.key = std::string(reinterpret_cast<char *>(key), sizeof(key));
aesKey.iv = std::string(reinterpret_cast<char *>(iv), sizeof(iv));
return aesKey;
}
std::string aesEncryptWithIV(const std::string &key,
const std::string &iv,
const std::string &plaintext)
{
if (key.length() != 32) {
throw std::invalid_argument("AES key must be 32 bytes (256-bit), got: "
+ std::to_string(key.length()));
}
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx)
handleOpenSSLError();
// 使用提供的IV或生成随机IV
unsigned char actual_iv[16];
if (iv.empty()) {
if (RAND_bytes(actual_iv, sizeof(actual_iv)) != 1) {
EVP_CIPHER_CTX_free(ctx);
handleOpenSSLError();
}
} else {
if (iv.length() != 16) {
EVP_CIPHER_CTX_free(ctx);
throw std::invalid_argument("IV must be 16 bytes (128-bit)");
}
memcpy(actual_iv, iv.data(), 16);
}
std::string encrypted;
try {
if (EVP_EncryptInit_ex(ctx,
EVP_aes_256_cbc(),
nullptr,
reinterpret_cast<const unsigned char *>(key.data()),
actual_iv)
!= 1) {
throw std::runtime_error("EVP_EncryptInit_ex failed");
}
int len;
encrypted.resize(plaintext.size() + AES_BLOCK_SIZE);
if (EVP_EncryptUpdate(ctx,
reinterpret_cast<unsigned char *>(encrypted.data()),
&len,
reinterpret_cast<const unsigned char *>(plaintext.data()),
plaintext.size())
!= 1) {
throw std::runtime_error("EVP_EncryptUpdate failed");
}
int encrypted_len = len;
if (EVP_EncryptFinal_ex(ctx, reinterpret_cast<unsigned char *>(encrypted.data()) + len, &len)
!= 1) {
throw std::runtime_error("EVP_EncryptFinal_ex failed");
}
encrypted_len += len;
encrypted.resize(encrypted_len);
// 将IV添加到加密数据前面
std::string result(reinterpret_cast<char *>(actual_iv), sizeof(actual_iv));
result += encrypted;
encrypted = result;
EVP_CIPHER_CTX_free(ctx);
} catch (...) {
EVP_CIPHER_CTX_free(ctx);
throw;
}
return encrypted;
}
std::string aesEncrypt(const std::string &key, const std::string &plaintext)
{
return aesEncryptWithIV(key, "", plaintext); // 使用随机IV
}
std::string aesDecrypt(const std::string &key, const std::string &ciphertext)
{
if (key.length() != 32) {
throw std::invalid_argument("AES key must be 32 bytes (256-bit), got: "
+ std::to_string(key.length()));
}
if (ciphertext.length() <= 16) {
throw std::invalid_argument("Ciphertext too short, must contain IV and encrypted data");
}
// 提取IV(前16字节)
std::string iv_str = ciphertext.substr(0, 16);
std::string actual_ciphertext = ciphertext.substr(16);
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx)
handleOpenSSLError();
std::string decrypted;
try {
if (EVP_DecryptInit_ex(ctx,
EVP_aes_256_cbc(),
nullptr,
reinterpret_cast<const unsigned char *>(key.data()),
reinterpret_cast<const unsigned char *>(iv_str.data()))
!= 1) {
throw std::runtime_error("EVP_DecryptInit_ex failed");
}
int len;
decrypted.resize(actual_ciphertext.size() + AES_BLOCK_SIZE);
if (EVP_DecryptUpdate(ctx,
reinterpret_cast<unsigned char *>(decrypted.data()),
&len,
reinterpret_cast<const unsigned char *>(actual_ciphertext.data()),
actual_ciphertext.size())
!= 1) {
throw std::runtime_error("EVP_DecryptUpdate failed");
}
int decrypted_len = len;
if (EVP_DecryptFinal_ex(ctx, reinterpret_cast<unsigned char *>(decrypted.data()) + len, &len)
!= 1) {
throw std::runtime_error("EVP_DecryptFinal_ex failed");
}
decrypted_len += len;
decrypted.resize(decrypted_len);
EVP_CIPHER_CTX_free(ctx);
} catch (...) {
EVP_CIPHER_CTX_free(ctx);
throw;
}
return decrypted;
}
class AesTest : public ::testing::Test
{
protected:
void SetUp() override
{
// 初始化OpenSSL
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
// 生成测试用的AES密钥
aesKey = generateAesKey();
testPlaintext = "Hello, AES Testing World! This is a test message.";
shortText = "Short";
emptyText = "";
}
void TearDown() override
{
// 清理OpenSSL
EVP_cleanup();
ERR_free_strings();
}
AesKey aesKey;
std::string testPlaintext;
std::string shortText;
std::string emptyText;
};
// 测试AES密钥生成
TEST_F(AesTest, KeyGeneration)
{
AesKey keys = generateAesKey();
// 验证密钥长度应为32字节(256位)
EXPECT_EQ(keys.key.length(), 32);
// 验证IV长度应为16字节(128位)
EXPECT_EQ(keys.iv.length(), 16);
// 验证生成的密钥和IV不为空
EXPECT_FALSE(keys.key.empty());
EXPECT_FALSE(keys.iv.empty());
// 验证两次生成的密钥不同(随机性)
AesKey keys2 = generateAesKey();
EXPECT_NE(keys.key, keys2.key);
EXPECT_NE(keys.iv, keys2.iv);
}
// 测试AES加密解密基本功能
TEST_F(AesTest, EncryptDecryptBasic)
{
std::string encrypted = aesEncrypt(aesKey.key, testPlaintext);
std::string decrypted = aesDecrypt(aesKey.key, encrypted);
// 验证解密后文本与原始文本一致
EXPECT_EQ(decrypted, testPlaintext);
// 验证加密后的文本不为空且与原文不同
EXPECT_FALSE(encrypted.empty());
EXPECT_NE(encrypted, testPlaintext);
// 验证加密后长度至少包含IV(16字节)
EXPECT_GE(encrypted.length(), 16);
}
// 测试短文本加密解密
TEST_F(AesTest, EncryptDecryptShortText)
{
std::string encrypted = aesEncrypt(aesKey.key, shortText);
std::string decrypted = aesDecrypt(aesKey.key, encrypted);
EXPECT_EQ(decrypted, shortText);
}
// 测试空文本加密解密
TEST_F(AesTest, EncryptDecryptEmptyText)
{
std::string encrypted = aesEncrypt(aesKey.key, emptyText);
std::string decrypted = aesDecrypt(aesKey.key, encrypted);
EXPECT_EQ(decrypted, emptyText);
}
// 测试长文本加密解密
TEST_F(AesTest, EncryptDecryptLongText)
{
// 生成长文本
std::string longText(10000, 'A');
longText += "MiddlePart";
longText += std::string(10000, 'Z');
std::string encrypted = aesEncrypt(aesKey.key, longText);
std::string decrypted = aesDecrypt(aesKey.key, encrypted);
EXPECT_EQ(decrypted, longText);
}
// 测试特殊字符文本加密解密
TEST_F(AesTest, EncryptDecryptSpecialCharacters)
{
std::string specialText = "Special!@#$%^&*()_+{}\":?><,./;'[]\\|-=~`";
std::string encrypted = aesEncrypt(aesKey.key, specialText);
std::string decrypted = aesDecrypt(aesKey.key, encrypted);
EXPECT_EQ(decrypted, specialText);
}
// 测试二进制数据加密解密
TEST_F(AesTest, EncryptDecryptBinaryData)
{
// 创建包含二进制数据的字符串(可能包含空字符等)
std::vector<unsigned char> binaryData = {0x00, 0x01, 0x02, 0x03, 0xFF, 0xFE, 0xFD, 0x00};
std::string binaryText(binaryData.begin(), binaryData.end());
std::string encrypted = aesEncrypt(aesKey.key, binaryText);
std::string decrypted = aesDecrypt(aesKey.key, encrypted);
EXPECT_EQ(decrypted, binaryText);
}
// 测试相同明文不同加密结果(由于随机IV)
TEST_F(AesTest, DifferentEncryptionResults)
{
std::string encrypted1 = aesEncrypt(aesKey.key, testPlaintext);
std::string encrypted2 = aesEncrypt(aesKey.key, testPlaintext);
// 由于使用随机IV,两次加密结果应该不同
EXPECT_NE(encrypted1, encrypted2);
// 但解密后应该得到相同原文
std::string decrypted1 = aesDecrypt(aesKey.key, encrypted1);
std::string decrypted2 = aesDecrypt(aesKey.key, encrypted2);
EXPECT_EQ(decrypted1, testPlaintext);
EXPECT_EQ(decrypted2, testPlaintext);
EXPECT_EQ(decrypted1, decrypted2);
}
// 测试错误密钥解密
TEST_F(AesTest, DecryptWithWrongKey)
{
std::string encrypted = aesEncrypt(aesKey.key, testPlaintext);
// 生成错误的密钥
AesKey wrongKeys = generateAesKey();
// 使用错误密钥解密应该失败或得到错误结果
try {
std::string decrypted = aesDecrypt(wrongKeys.key, encrypted);
// 如果解密"成功",结果应该与原文不同
EXPECT_NE(decrypted, testPlaintext);
} catch (const std::exception &e) {
// 抛出异常也是预期的行为
SUCCEED();
}
}
// 测试损坏的密文解密
TEST_F(AesTest, DecryptCorruptedCiphertext)
{
std::string encrypted = aesEncrypt(aesKey.key, testPlaintext);
// 损坏密文(修改一些字节)
if (encrypted.length() > 20) {
std::string corrupted = encrypted;
corrupted[10] = ~corrupted[10]; // 翻转一个字节
try {
std::string decrypted = aesDecrypt(aesKey.key, corrupted);
// 如果解密"成功",结果应该与原文不同
EXPECT_NE(decrypted, testPlaintext);
} catch (const std::exception &e) {
// 抛出异常也是可以接受的行为
SUCCEED();
}
}
}
// 测试损坏的IV解密
TEST_F(AesTest, DecryptCorruptedIV)
{
std::string encrypted = aesEncrypt(aesKey.key, testPlaintext);
// 损坏IV部分(前16字节)
if (encrypted.length() > 16) {
std::string corrupted = encrypted;
corrupted[5] = ~corrupted[5]; // 翻转IV中的一个字节
try {
std::string decrypted = aesDecrypt(aesKey.key, corrupted);
// 如果解密"成功",结果应该与原文不同
EXPECT_NE(decrypted, testPlaintext);
} catch (const std::exception &e) {
// 抛出异常也是可以接受的行为
SUCCEED();
}
}
}
// 测试密钥长度验证
TEST_F(AesTest, InvalidKeyLength)
{
std::string shortKey(16, 'K'); // 16字节密钥(应该是32字节)
std::string longKey(64, 'K'); // 64字节密钥
// 测试过短密钥
EXPECT_THROW({ aesEncrypt(shortKey, testPlaintext); }, std::invalid_argument);
// 测试过长密钥
EXPECT_THROW({ aesEncrypt(longKey, testPlaintext); }, std::invalid_argument);
}
// 测试空密文解密
TEST_F(AesTest, DecryptEmptyCiphertext)
{
EXPECT_THROW({ aesDecrypt(aesKey.key, ""); }, std::invalid_argument);
}
// 测试十六进制转换配合AES
TEST_F(AesTest, WithHexConversion)
{
std::string encrypted = aesEncrypt(aesKey.key, testPlaintext);
// 转换为十六进制
std::string hexEncrypted = toHex(encrypted);
// 从十六进制转换回
std::string fromHexEncrypted = fromHex(hexEncrypted);
// 验证转换前后一致
EXPECT_EQ(encrypted, fromHexEncrypted);
// 解密应该仍然工作
std::string decrypted = aesDecrypt(aesKey.key, fromHexEncrypted);
EXPECT_EQ(decrypted, testPlaintext);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}