crypto++ AES解密加速

通过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;
}

通过该函数解密耗时可以缩短到两秒内。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值