解密内存问题

时间:2011-05-07 13:13:38

标签: c++ crypto++

我最近一直在制作一台使用AES256加密/解密数据的服务器,需要一段时间才能正确发送。然而,现在我遇到了一个问题,我认为是记忆,如果我发送“你好”这个词它会解密好,如果我再发送“helloo”,它也会解密很好,但如果我发送任何更短的内容比之后的“helloo”,它在解密过程中会出错,如果你打印出它收到的加密字符串,它就会得到它应该有的东西以及旧字符串的额外长度。

e.g

hello:  ####################
helloo: ##############################
hi:     #####(#########################) //has the additional length made up from the encrypted string of "helloo" minus the first however many characters "hi" is

代码:

std::string decryptString(std::string ciphertext, byte *key, byte *iv)
{
    std::string decodedtext;
    CryptoPP::StringSource(ciphertext, true, 
                           new CryptoPP::HexDecoder(new CryptoPP::StringSink(decodedtext)));

    std::string plaintext;

    CryptoPP::GCM<CryptoPP::AES>::Decryption dec;
    dec.SetKeyWithIV((const byte *)key, CryptoPP::AES::MAX_KEYLENGTH, 
                     (const byte *)iv, CryptoPP::AES::BLOCKSIZE);

    CryptoPP::AuthenticatedDecryptionFilter adf(dec, new CryptoPP::StringSink(plaintext));

    adf.Put((const byte *)decodedtext.data(), decodedtext.size());
    adf.MessageEnd();

    return plaintext;
}

2 个答案:

答案 0 :(得分:1)

尝试使用valgrind在代码中查找内存错误。

哦,还有一个提示:发布代码本身,它可能会带来更有趣的答案。

答案 1 :(得分:0)

如果你总是将相同的初始化向量传递给这个方法,可能就是其中的原因。尝试

dec.Resynchronize(iv);