-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.cpp
58 lines (51 loc) · 2.15 KB
/
crypto.cpp
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
#include "crypto.h"
namespace Crypto
{
Crypto::Crypto()
{
settings = new QSettings(QCoreApplication::applicationDirPath()+"/qTame.td", QSettings::IniFormat);
settings->setIniCodec("UTF-8");
}
Crypto::~Crypto()
{
delete settings;
}
QString Crypto::EncryptBase64(QString text)
{
settings->beginGroup("User");
QString iv = generateIv();
QByteArray hashKey = QCryptographicHash::hash(settings->value("key").toString().toLocal8Bit(), QCryptographicHash::Sha256);
settings->endGroup();
QByteArray hashIv = QCryptographicHash::hash(iv.toLocal8Bit(), QCryptographicHash::Md5);
return (iv + QAESEncryption::Crypt(QAESEncryption::AES_256,
QAESEncryption::CBC,
text.toLocal8Bit(),
hashKey,
hashIv).toBase64());
}
QString Crypto::DecryptBase64(QString text)
{
settings->beginGroup("User");
QByteArray hashKey = QCryptographicHash::hash(settings->value("key").toString().toLocal8Bit(), QCryptographicHash::Sha256);
settings->endGroup();
QByteArray hashIv = QCryptographicHash::hash(text.left(32).toLocal8Bit(), QCryptographicHash::Md5);
return QAESEncryption::RemovePadding(QAESEncryption::Decrypt(QAESEncryption::AES_256,
QAESEncryption::CBC,
QByteArray::fromBase64(text.mid(32).toLocal8Bit()),
hashKey,
hashIv,
QAESEncryption::ZERO), QAESEncryption::ISO);
}
QString Crypto::generateIv()
{
QString result("");
const char alphanum[] = "0123456789!@#$%^&*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int string_length = sizeof(alphanum)-1;
srand(time(0));
for(int i = 0; i < 32; i++)
{
result.append(alphanum[rand() % string_length]);
}
return result;
}
}