-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcryptopp_aes_algorithm.cpp
More file actions
197 lines (175 loc) · 5.32 KB
/
Copy pathcryptopp_aes_algorithm.cpp
File metadata and controls
197 lines (175 loc) · 5.32 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
#include "cryptopp_aes_algorithm.h"
AES_256_CFB_Algorithm::AES_256_CFB_Algorithm()
{
m_key.New(AES_256_CFB_MAX_KEY_SIZE);
m_iv.New(AES_256_CFB_MAX_IV_SIZE);
m_rand_engine.GenerateBlock(m_key.BytePtr(), m_key.size());
m_rand_engine.GenerateBlock(m_iv.BytePtr(), m_iv.size());
}
AES_256_CFB_Algorithm::~AES_256_CFB_Algorithm()
{
}
void AES_256_CFB_Algorithm::GenerateIv()
{
m_rand_engine.GenerateBlock(m_iv.BytePtr(), m_iv.size());
}
void AES_256_CFB_Algorithm::GenerateKey()
{
m_rand_engine.GenerateBlock(m_key.BytePtr(), m_key.size());
}
void AES_256_CFB_Algorithm::SetKey(const std::string &input_key_data)
{
std::string key_data = input_key_data;
key_data.resize(AES_256_CFB_MAX_KEY_SIZE);
CryptoPP::byte* ptr_byte_key = m_key.BytePtr();
memcpy(ptr_byte_key,key_data.data(),key_data.size());
}
void AES_256_CFB_Algorithm::SetIv(const std::string &input_iv_data)
{
std::string iv_data = input_iv_data;
iv_data.resize(AES_256_CFB_MAX_IV_SIZE);
CryptoPP::byte* ptr_byte_key = m_iv.BytePtr();
memcpy(ptr_byte_key,iv_data.data(),iv_data.size());
}
std::string AES_256_CFB_Algorithm::GetKey()
{
std::string str_return(m_key.begin(),m_key.end());
return str_return;
}
std::string AES_256_CFB_Algorithm::GetIv()
{
std::string str_return(m_iv.begin(),m_iv.end());
return str_return;
}
bool AES_256_CFB_Algorithm::EncryptString(const std::string &input_string, std::string &output_string)
{
try
{
m_encrypt_engine.SetKeyWithIV(m_key.BytePtr(),m_key.size(),m_iv.BytePtr(),m_iv.size());
CryptoPP::StringSource(input_string, true,
new CryptoPP::StreamTransformationFilter(m_encrypt_engine,
new CryptoPP::StringSink(output_string)
) // StreamTransformationFilter
); // StringSource
}
catch(const CryptoPP::Exception& e)
{
//cerr << e.what() << endl;
//exit(1);
UNUSED_VARIANT(e);
return false;
}
catch(...)
{
return false;
}
return true;
}
bool AES_256_CFB_Algorithm::DecryptString(const std::string &input_string, std::string &output_string)
{
try
{
m_decrypt_engine.SetKeyWithIV(m_key.BytePtr(),m_key.size(),m_iv.BytePtr(),m_iv.size());
CryptoPP::StringSource(input_string, true,
new CryptoPP::StreamTransformationFilter(m_decrypt_engine,
new CryptoPP::StringSink(output_string)
) // StreamTransformationFilter
); // StringSource
}
catch(const CryptoPP::Exception& e)
{
//cerr << e.what() << endl;
//exit(1);
UNUSED_VARIANT(e);
return false;
}
catch(...)
{
return false;
}
return true;
}
bool AES_256_CFB_Algorithm::EncryptFile(const std::string &input_string_path, const std::string &output_string_path)
{
try
{
m_encrypt_engine.SetKeyWithIV(m_key.BytePtr(),m_key.size(),m_iv.BytePtr(),m_iv.size());
CryptoPP::FileSource(input_string_path.c_str(), true,
new CryptoPP::StreamTransformationFilter(m_encrypt_engine,
new CryptoPP::FileSink(output_string_path.c_str())
) // StreamTransformationFilter
); // StringSource
}
catch(const CryptoPP::Exception& e)
{
//cerr << e.what() << endl;
//exit(1);
UNUSED_VARIANT(e);
return false;
}
catch(...)
{
return false;
}
return true;
}
bool AES_256_CFB_Algorithm::DecryptFile(const std::string &input_string_path, const std::string &output_string_path)
{
try
{
m_decrypt_engine.SetKeyWithIV(m_key.BytePtr(),m_key.size(),m_iv.BytePtr(),m_iv.size());
CryptoPP::FileSource(input_string_path.c_str(), true,
new CryptoPP::StreamTransformationFilter(m_decrypt_engine,
new CryptoPP::FileSink(output_string_path.c_str())
) // StreamTransformationFilter
); // StringSource
}
catch(const CryptoPP::Exception& e)
{
//cerr << e.what() << endl;
//exit(1);
UNUSED_VARIANT(e);
return false;
}
catch(...)
{
return false;
}
return true;
}
bool AES_256_CFB_Algorithm::EncryptInit()
{
m_encrypt_engine.SetKeyWithIV(m_key.BytePtr(),m_key.size(),m_iv.BytePtr(),m_iv.size());
return true;
}
bool AES_256_CFB_Algorithm::EncryptProcessData(char *outString,size_t OutLength, const char *inString, size_t InLength)
{
//AES-256-CFB是流式算法,不需要进行块填充
if(OutLength !=InLength)
{
return false;
}
m_encrypt_engine.ProcessData(
reinterpret_cast<CryptoPP::byte*>(outString),
reinterpret_cast<const CryptoPP::byte*>(inString),
OutLength);
return true;
}
bool AES_256_CFB_Algorithm::DecryptInit()
{
m_decrypt_engine.SetKeyWithIV(m_key.BytePtr(),m_key.size(),m_iv.BytePtr(),m_iv.size());
return true;
}
bool AES_256_CFB_Algorithm::DecryptProcessData(char *outString, size_t OutLength, const char *inString, size_t InLength)
{
//AES-256-CFB是流式算法,不需要进行块填充
if(OutLength !=InLength)
{
return false;
}
m_decrypt_engine.ProcessData(
reinterpret_cast<CryptoPP::byte*>(outString),
reinterpret_cast<const CryptoPP::byte*>(inString),
OutLength);
return true;
}