34SimpleCrypt::SimpleCrypt(): myKey(0), myCompressionMode(CompressionAuto), myProtectionMode(ProtectionChecksum), myLastError(ErrorNoError)
36 QRandomGenerator generator(uint(QDateTime::currentMSecsSinceEpoch() & 0xFFFF));
44SimpleCrypt::SimpleCrypt(quint64 key): myKey(key), myCompressionMode(CompressionAuto), myProtectionMode(ProtectionChecksum), myLastError(ErrorNoError)
46 QRandomGenerator generator(uint(QDateTime::currentMSecsSinceEpoch() & 0xFFFF));
57 isDebugMessage = thisState;
65 return isDebugMessage;
80void SimpleCrypt::splitKey()
84 for (
int i = 0;i < 8; i++)
87 for (
int j = i; j > 0; j--) { part = part >> 8; }
89 myKeyParts[i] =
static_cast<char>(part);
98 QByteArray plaintextArray = plaintext.toUtf8();
107 if (myKeyParts.isEmpty())
109 qWarning() <<
"No key set.";
114 QByteArray ba = plaintext;
119 ba = qCompress(ba, 9);
124 QByteArray compressed = qCompress(ba, 9);
125 if (compressed.count() < ba.count())
132 QByteArray integrityProtection;
136 QDataStream s(&integrityProtection, QIODevice::WriteOnly);
137 s << qChecksum(ba.constData(), ba.length());
142 QCryptographicHash hash(QCryptographicHash::Sha1);
145 integrityProtection += hash.result();
148 QRandomGenerator generator(uint(QDateTime::currentMSecsSinceEpoch() & 0xFFFF));
149 char randomChar = char(generator.generate() & 0xFF);
152 ba = randomChar + integrityProtection + ba;
157 int cnt = ba.count();
161 ba[pos] = ba.at(pos) ^ myKeyParts.at(pos % 8) ^ lastChar;
162 lastChar = ba.at(pos);
166 QByteArray resultArray;
167 resultArray.append(
char(0x03));
168 resultArray.append(
char(flags));
169 resultArray.append(ba);
180 QByteArray plaintextArray = plaintext.toUtf8();
182 QString cypherString = QString::fromLatin1(cypher.toBase64());
192 QString cypherString = QString::fromLatin1(cypher.toBase64());
201 QByteArray cyphertextArray = QByteArray::fromBase64(cyphertext.toLatin1());
203 QString plaintext = QString::fromUtf8(plaintextArray, plaintextArray.size());
214 QString plaintext = QString::fromUtf8(ba, ba.size());
224 QByteArray cyphertextArray = QByteArray::fromBase64(cyphertext.toLatin1());
235 if (myKeyParts.isEmpty())
237 qWarning() <<
"No key set.";
242 QByteArray ba = cypher;
244 if( cypher.count() < 3 )
245 {
return QByteArray(); }
247 char version = ba.at(0);
252 qWarning() <<
"Invalid version or not a cyphertext.";
256 CryptoFlags flags = CryptoFlags(ba.at(1));
265 char currentChar = ba[pos];
266 ba[pos] = ba.at(pos) ^ lastChar ^ myKeyParts.at(pos % 8);
267 lastChar = currentChar;
273 bool integrityOk(
true);
281 quint16 storedChecksum;
283 QDataStream s(&ba, QIODevice::ReadOnly);
287 quint16 checksum = qChecksum(ba.constData(), ba.length());
288 integrityOk = (checksum == storedChecksum);
292 if (ba.length() < 20)
297 QByteArray storedHash = ba.left(20);
299 QCryptographicHash hash(QCryptographicHash::Sha1);
301 integrityOk = (hash.result() == storedHash);
311 { ba = qUncompress(ba); }
324 qDebug() << thisMessage;
bool getDebugMessage()
get Debug Message
void setDebugMessage(bool thisState)
set Debug Message
void setMessage(const QString &thisMessage)
set Message
QString decryptToString(const QString &cyphertext)
decrypt To String QString. decryptToString
void setKey(quint64 key)
set Key. setKey
@ CryptoFlagChecksum
CryptoFlagChecksum
@ CryptoFlagNone
CryptoFlagNone
@ CryptoFlagCompression
CryptoFlagCompression
@ CryptoFlagHash
CryptoFlagHash
SimpleCrypt()
Constructor. SimpleCrypt.
QByteArray decryptToByteArray(const QString &cyphertext)
decrypt To Byte Array QString. decryptToByteArray
QByteArray encryptToByteArray(const QString &plaintext)
encrypt To Byte Array QString. encryptToByteArray
QString encryptToString(const QString &plaintext)
encrypt To String QString. encryptToString