-
Notifications
You must be signed in to change notification settings - Fork 1
/
AESTool_win.cpp
140 lines (118 loc) · 4.3 KB
/
AESTool_win.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
#include <iostream>
#include <fstream>
#include <cstring>
#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <cryptopp/filters.h>
#include <chrono>
using namespace CryptoPP;
void UpdateProgressBar(float progress)
{
std::string bar;
int barWidth = 40;
int pos = static_cast<int>(barWidth * progress);
for (int i = 0; i < barWidth; ++i)
{
if (i < pos)
bar += "=";
else if (i == pos)
bar += ">";
else
bar += " ";
}
std::cout << "[" << bar << "] " << int(progress * 100.0) << "%\r";
std::cout.flush();
}
void Log(const std::string& message)
{
std::cout << message << std::endl;
}
void EncryptFile(const std::string& inputFile, const std::string& outputFile, const std::string& password)
{
Log("Version 1.0");
Log("AES256Tools");
Log("File: " + inputFile);
Log("Request: Encrypt");
// 读取输入文件
std::string plaintext;
std::ifstream ifs(inputFile, std::ios::binary);
if (ifs)
{
ifs.seekg(0, std::ios::end);
plaintext.resize(ifs.tellg());
ifs.seekg(0, std::ios::beg);
ifs.read(&plaintext[0], plaintext.size());
}
else
{
std::cerr << "无法打开输入文件" << std::endl;
return;
}
// 初始化加密器
SecByteBlock key(AES::DEFAULT_KEYLENGTH);
memcpy(key, password.c_str(), AES::DEFAULT_KEYLENGTH);
CBC_Mode<AES>::Encryption encryption;
encryption.SetKeyWithIV(key, key);
// 加密
std::string ciphertext;
auto startTime = std::chrono::high_resolution_clock::now();
StringSource(plaintext, true,
new StreamTransformationFilter(encryption, new StringSink(ciphertext),
new FileSink(outputFile.c_str()),
true, // putMessage
[&](int current, int total)
{
float progress = static_cast<float>(current) / total;
UpdateProgressBar(progress);
}));
auto endTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
Log("加密完成,耗时 " + std::to_string(duration) + " 毫秒");
}
void DecryptFile(const std::string& inputFile, const std::string& outputFile, const std::string& password)
{
Log("Version 1.0");
Log("AES256Tools");
Log("File: " + inputFile);
Log("Request: Decrypt");
// 读取输入文件
std::string ciphertext;
std::ifstream ifs(inputFile, std::ios::binary);
if (ifs)
{
ifs.seekg(0, std::ios::end);
ciphertext.resize(ifs.tellg());
ifs.seekg(0, std::ios::beg);
ifs.read(&ciphertext[0], ciphertext.size());
}
else
{
std::cerr << "无法打开输入文件" << std::endl;
return;
}
// 初始化解密器
SecByteBlock key(AES::DEFAULT_KEYLENGTH);
memcpy(key, password.c_str(), AES::DEFAULT_KEYLENGTH);
CBC_Mode<AES>::Decryption decryption;
decryption.SetKeyWithIV(key, key);
// 解密
std::string plaintext;
auto startTime = std::chrono::high_resolution_clock::now();
StringSource(ciphertext, true,
new StreamTransformationFilter(decryption, new StringSink(plaintext),
new FileSink(outputFile.c_str()),
true, // putMessage
[&](int current, int total)
{
float progress = static_cast<float>(current) / total;
UpdateProgressBar(progress);
}));
auto endTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
Log("解密完成,耗时 " + std::to_string(duration) + " 毫秒");
}
int main(int argc, char* argv[])
{
if (argc != 4)
{
std::cerr << "命令行