通过https://cryptopp.com/cryptopp840.zip链接下载源码,利用生成的动态库进行string的aes的解密(通过链AES加密 C++调用Crypto++加密库 样例 - mengfanrong - 博客园 的方式进行解密),当解密的文件非常大时,解密耗时非常久,耗时可以到达一百四十多秒,对程序来说是非常不可接受的,同时同样的文件在python下解密速度非常快,所以需要进行一定的加速,以下是本人的解密加速代码
#define USE_OPENMP 1
#define THREAD_NUM std::min(omp_get_num_procs(), 8)
std::map<char, int> hex_idex_0{ {'0',0},{'1',1 * 16},{'2',2 * 16},{'3',3 * 16},{'4',4 * 16},{'5',5 * 16},{'6',6 * 16},{'7',7 * 16},{'8',8 * 16},{'9',9 * 16},{'a',10 * 16},{'b',11 * 16},{'c',12 * 16},{'d',13 * 16},{'e',14 * 16},{'f',15 * 16} };
std::map<char, int> hex_idex_1{ {'0',0},{'1',1},{'2',2},{'3',3},{'4',4},{'5',5},{'6',6},{'7',7},{'8',8},{'9',9},{'a',10},{'b',11},{'c',12},{'d',13},{'e',14},{'f',15} };
int hexmine(string input)
{
int sum = 0;
sum += hex_idex_0[input[0]];
sum += hex_idex_1[input[1]];
return sum;
}
string decrypt(string cipherTextHex)
{
initKV();
clock_t start_time, end_time;
start_time = clock();
string decryptedText;
int cipherText_len = cipherTextHex.length() / 2;
char* ptr_c = new char[cipherText_len];
#if USE_OPENMP
#pragma omp parallel for num_threads(THREAD_NUM)
#endif
for (int i = 0; i < cipherText_len; i++)
{
ptr_c[i] = (char)hexmine(cipherTextHex.substr(2*i, 2));
}
string cipherText(ptr_c, cipherText_len);
delete[] ptr_c;
end_time = clock();
std::cout << "cost time is " << end_time - start_time << " ms" << std::endl;
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv);
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedText));
stfDecryptor.Put(reinterpret_cast<const unsigned char*>(cipherText.c_str()), cipherText.size());
stfDecryptor.MessageEnd();
return decryptedText;
}
通过该函数解密耗时可以缩短到两秒内。