#include "XorEncrypter.h" XorEncrypter::XorEncrypter(const QByteArray &key, QIODevice *parentDevice) : QIODevice(parentDevice), m_key(key), m_keyLength(key.length()), m_parentDevice(parentDevice) { connect(parentDevice, SIGNAL(aboutToClose()), this, SIGNAL(aboutToClose())); connect(parentDevice, SIGNAL(bytesWritten(qint64)), this, SIGNAL(bytesWritten(qint64))); connect(parentDevice, SIGNAL(readChannelFinished()), this, SIGNAL(readChannelFinished())); connect(parentDevice, SIGNAL(readyRead()), this, SIGNAL(readyRead())); if (parentDevice->isOpen()) QIODevice::open(parentDevice->openMode()); } qint64 XorEncrypter::readData(char *data, qint64 maxSize) { qint64 startPos = pos(); qint64 total = m_parentDevice->read(data, maxSize); for (qint64 i = 0; i < total; ++i) data[i] ^= m_key.at((startPos + i) % m_keyLength); return total; } qint64 XorEncrypter::writeData(const char *data, qint64 maxSize) { char *encryptedData = new char[maxSize]; qint64 startPos = pos(); for (qint64 i = 0; i < maxSize; ++i) encryptedData[i] = data[i] ^ m_key.at((startPos + i) % m_keyLength); return m_parentDevice->write(encryptedData, maxSize); } bool XorEncrypter::atEnd() const { return m_parentDevice->atEnd(); } qint64 XorEncrypter::bytesAvailable() const { return m_parentDevice->bytesAvailable(); } qint64 XorEncrypter::bytesToWrite() const { return m_parentDevice->bytesToWrite(); } bool XorEncrypter::canReadLine() const { return m_parentDevice->canReadLine(); } void XorEncrypter::close() { QIODevice::close(); return m_parentDevice->close(); } bool XorEncrypter::isSequential() const { return m_parentDevice->isSequential(); } bool XorEncrypter::open(OpenMode mode) { QIODevice::open(mode); return m_parentDevice->open(mode); } qint64 XorEncrypter::pos() const { return m_parentDevice->pos(); } bool XorEncrypter::reset() { return m_parentDevice->reset(); } bool XorEncrypter::seek(qint64 pos) { return m_parentDevice->seek(pos); } qint64 XorEncrypter::XorEncrypter::size() const { return m_parentDevice->size(); } bool XorEncrypter::waitForBytesWritten(int msecs) { return m_parentDevice->waitForBytesWritten(msecs); } bool XorEncrypter::waitForReadyRead(int msecs) { return m_parentDevice->waitForReadyRead(msecs); }