summaryrefslogtreecommitdiffstats
path: root/XorEncrypter.cpp
blob: 71369ca68cbb99e3ce5023ab772f57e0232d2286 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#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);
}