-
Notifications
You must be signed in to change notification settings - Fork 1
/
cryptoaes.cpp
150 lines (119 loc) · 3.3 KB
/
cryptoaes.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "cryptoaes.h"
#include <string>
using std::string;
#include "filters.h"
using CryptoPP::StringSink;
using CryptoPP::ArraySink;
using CryptoPP::StringSource;
using CryptoPP::ArraySource;
using CryptoPP::StreamTransformationFilter;
#include "osrng.h"
using CryptoPP::AutoSeededRandomPool;
#include "aes.h"
using CryptoPP::AES;
#include "ccm.h"
using CryptoPP::CBC_Mode;
using CryptoPP::CBC_Mode_ExternalCipher;
#include "modes.h"
#include "QDebug"
#include "QString"
void QByteArrayToUCharArray(QByteArray src, unsigned char *dest)
{
for (int i = 0; i < src.size(); i++)
{
dest[i] = src.at(i);
}
}
QByteArray UCharArrayToQByteArray(unsigned char *src, int p_size)
{
QByteArray array((char*)src, p_size);
return array;
}
class CryptoAesPrivate {
public:
CryptoPP::AutoSeededRandomPool rnd;
QByteArray m_key;
QByteArray key() const {
return m_key;
}
const byte *keyByte() const {
return (byte*)key().data();
}
int keySize() {
return key().size();
}
void setKey(QByteArray key) {
if (key != m_key) {
m_key = key;
}
}
QByteArray generateKey() {
byte key[32];
rnd.GenerateBlock(key, 32);
return UCharArrayToQByteArray(key, 32);
}
QByteArray encrypt(QByteArray in, QByteArray key) {
setKey(key);
return encrypt(in);
}
QByteArray encrypt(QByteArray in) {
byte iv[CryptoPP::AES::BLOCKSIZE];
rnd.GenerateBlock(iv, CryptoPP::AES::BLOCKSIZE);
QByteArray out = QByteArray((char*)iv, CryptoPP::AES::BLOCKSIZE);
int inputSize = in.size();
string cipher;
CBC_Mode<AES>::Encryption aes(keyByte(), keySize(), iv);
ArraySource((byte *)in.data(), inputSize, true, new StreamTransformationFilter(aes, new StringSink(cipher)));
QByteArray encryptedBytes = QByteArray(cipher.c_str(), cipher.size());
out.append(encryptedBytes);
return out;
}
QByteArray decrypt(QByteArray in, QByteArray key) {
setKey(key);
return decrypt(in);
}
QByteArray decrypt(QByteArray in) {
QByteArray iv = in.left(AES::BLOCKSIZE);
in.remove(0, AES::BLOCKSIZE);
string decrypted;
QByteArray result;
try {
CBC_Mode<AES>::Decryption aes_dec;
aes_dec.SetKeyWithIV((byte *)key().data(), keySize(), (byte *)iv.data());
ArraySource((byte *)in.data(), in.size(), true,
new StreamTransformationFilter(aes_dec,
new StringSink(decrypted)));
result = QByteArray(decrypted.c_str(), in.size());
} catch (CryptoPP::Exception err) {
result = "Failed to decrypt";
qDebug() << QString(err.GetWhat().c_str());
}
return result;
}
};
CryptoAes::CryptoAes() :
p(new CryptoAesPrivate)
{
}
CryptoAes::~CryptoAes() {
}
QByteArray CryptoAes::key() const
{
return p->key();
}
void CryptoAes::setKey(QByteArray key)
{
p->setKey(key);
}
QByteArray CryptoAes::encrypt(QByteArray in, QByteArray key)
{
return p->encrypt(in, key);
}
QByteArray CryptoAes::decrypt(QByteArray in, QByteArray key)
{
return p->decrypt(in, key);
}
QByteArray CryptoAes::generateKey()
{
return p->generateKey();
}