最近、AES256を使用してデータを暗号化/復号化するサーバーを作成していますが、正しく送信されるまでに時間がかかりました。しかし、今はメモリに問題があると思います。「hello」という単語を送信すると正常に復号化され、「helloo」を送信すると正常に復号化されますが、それより短いものを送信すると後の「helloo」よりも、復号化中にエラーが発生し、受信した暗号化された文字列を印刷すると、本来あるべきものに加えて古い文字列の追加の長さが得られます。
例えば
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;
}