On Thu, 19 Sep 2013 20:14:06 -0700 (PDT)
Heiko <[email protected]> wrote:

> Hi there,
> 
> trying to encrypt something with crypto++ and aes and i have a weired 
> problem.
> 
> When i encrypt and then decrypt, the decrypted texts first 16 bytes
> are scrambled.

[snip]

> QString Encrypter::encryptMessage(const QString &msg, const 
> CryptoPP::SecByteBlock &key)
> {
>     CryptoPP::AutoSeededRandomPool rng;
> 
>     byte iv[CryptoPP::AES::BLOCKSIZE];
>     rng.GenerateBlock(iv, CryptoPP::AES::BLOCKSIZE);
> 
>     char buf[msg.length()];
> 
>     CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption cfbEncryption(key, 
> key.size(), iv);
>     cfbEncryption.ProcessData((byte *)buf, (byte 
> *)msg.toStdString().c_str(), msg.length());
> 
>     QByteArray ret(buf, msg.length());
>     return ret.toBase64();
> }
> 
> // decrypting string from base 64
> QString Encrypter::decryptMessage(const QString &msg64, const 
> CryptoPP::SecByteBlock &key)
> {
>     CryptoPP::AutoSeededRandomPool rng;
> 
>     byte iv[CryptoPP::AES::BLOCKSIZE];
>     rng.GenerateBlock(iv, CryptoPP::AES::BLOCKSIZE);
> 
>     QByteArray msg = QByteArray::fromBase64(msg64.toUtf8());
>     char buf[msg.length()];
> 
>     CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption cfbDecryption(key, 
> key.size(), iv);
>     cfbDecryption.ProcessData((byte*)buf, (byte*)msg.data(),
> msg.length());
> 
>     QByteArray ret(buf, msg.length());
>     return QString(ret);
> }
[snip]

You’re generating a random value for “iv” in the decryption function.
Because you’re using CFB mode, that destroys only the first block of
plaintext. You’re supposed to generate a random IV while encrypting,
then transport that IV to the decryption routine and use the same IV to
decrypt that you used to encrypt. You don’t have to keep the IV secret.
-- 
Christopher Head

Attachment: signature.asc
Description: PGP signature

Reply via email to