-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqxorcipher.cpp
More file actions
47 lines (36 loc) · 872 Bytes
/
qxorcipher.cpp
File metadata and controls
47 lines (36 loc) · 872 Bytes
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
#include "qxorcipher.h"
QXORCipher::QXORCipher(QString key)
{
setKey(key);
}
void QXORCipher::setKey(QByteArray key)
{
if(key.isEmpty())
{
qDebug() << "failed to set new key. Key can't be empty";
return;
}
this->key = key;
}
void QXORCipher::setKey(QString key)
{
this->key = key.toUtf8();
}
QByteArray QXORCipher::encrypt(QByteArray data)
{
QByteArray result;
int keyIndex = 0;
for(int i = 0; i<data.length(); i++)
{
result.append( data[i]^key[keyIndex++] );
if(keyIndex == key.length()) keyIndex = 0;
}
return result;
}
QByteArray QXORCipher::decrypt(QByteArray data)
{
//Rember that decryption is the same as encryption regarding
//XOR cipher. So we can just reuse encrypt method and have a
//decrypt method just so it is easier to read.
return encrypt(data);
}