我最近一直在使用AES256来加密/解密数据的服务器,它需要一段时间才能正确发送。然而,现在我遇到了一个我认为是记忆的问题,如果我发送单词“hello”,它会解密,如果我发送“hello”,它也会解密,但如果我发送更短的内容比之后的“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;
}
我猜你不重新初始化缓冲区加密输出,但没有代码......很难说 – DarkSquirrel42 2011-05-07 13:32:52
现在张贴是否**加密代码**。 – QuantumMechanic 2011-05-07 13:44:27
你能告诉我们调用decryptString(...)的代码吗?我认为'cyphertext'仍然包含旧的字符串,当你把新的字符串放在...尝试在新的字符串的字符后面写一个\ 0 – DarkSquirrel42 2011-05-07 13:47:30