1

最近、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;
}
4

2 に答える 2

1

valgrind を使用して、コード内のメモリ エラーを見つけてみてください。

ああ、ヒント: コード自体を投稿すると、より興味深い回答につながる可能性があります。

于 2011-05-07T13:24:23.110 に答える
0

このメソッドに常に同じ初期化ベクトルを渡すと、その理由が考えられます。試す

dec.Resynchronize(iv);
于 2013-10-01T10:30:23.130 に答える