diff --git a/src/streams/SymmetricCipherStream.cpp b/src/streams/SymmetricCipherStream.cpp index 30963bd7a..be3536331 100644 --- a/src/streams/SymmetricCipherStream.cpp +++ b/src/streams/SymmetricCipherStream.cpp @@ -29,11 +29,18 @@ SymmetricCipherStream::SymmetricCipherStream(QIODevice* baseDevice, SymmetricCip bool SymmetricCipherStream::reset() { + if (isWritable()) { + if (!writeBlock()) { + return false; + } + } + m_buffer.clear(); m_bufferPos = 0; m_bufferFilling = false; m_error = false; m_cipher->reset(); + return true; } @@ -133,6 +140,7 @@ bool SymmetricCipherStream::writeBlock() return true; } else if (m_buffer.size() != m_cipher->blockSize()) { + // PKCS7 padding int padLen = m_cipher->blockSize() - m_buffer.size(); for (int i=m_buffer.size(); iblockSize(); i++) { m_buffer.append(static_cast(padLen)); diff --git a/tests/TestSymmetricCipher.cpp b/tests/TestSymmetricCipher.cpp index 70c6b6e42..044199794 100644 --- a/tests/TestSymmetricCipher.cpp +++ b/tests/TestSymmetricCipher.cpp @@ -59,21 +59,21 @@ void TestSymmetricCipher::testAes256CbcEncryption() buffer.open(QIODevice::WriteOnly); stream.open(QIODevice::WriteOnly); + QVERIFY(stream.reset()); buffer.reset(); buffer.buffer().clear(); - stream.reset(); stream.write(plainText.left(16)); QCOMPARE(QString(buffer.data().toHex()), QString(cipherText.left(16).toHex())); + QVERIFY(stream.reset()); buffer.reset(); buffer.buffer().clear(); - stream.reset(); stream.write(plainText.left(10)); QCOMPARE(QString(buffer.data().toHex()), QString()); + QVERIFY(stream.reset()); buffer.reset(); buffer.buffer().clear(); - stream.reset(); stream.write(plainText.left(10)); stream.close(); QCOMPARE(buffer.data().size(), 16);