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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
#include "cryptengine.h"
CryptEngine::CryptEngine(QObject *parent)
: QObject(parent)
{
m_algorithm = 0; //Initialize NullCipher
m_cryptClass = new CryptClassNullCipher;
}
CryptEngine::~CryptEngine()
{
delete m_cryptClass;
}
void CryptEngine::generateRandomKey()
{
m_cryptClass->generateRandomKey();
}
QByteArray CryptEngine::createPasswordHash(const QString &password)
{
QByteArray ret(256, 0);
EVP_MD_CTX *ctx;
/* Create and initialise the context */
if (!(ctx = EVP_MD_CTX_create()))
{
handleOpenSslError();
}
/* Initialise the encryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher */
if (1 != EVP_DigestInit_ex(ctx, EVP_sha256(), NULL))
{
handleOpenSslError();
}
/* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if (1 != EVP_DigestUpdate(ctx, password.data(), password.size()))
{
handleOpenSslError();
}
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if (1 != EVP_DigestFinal_ex(ctx, (unsigned char *) ret.data(), NULL))
{
handleOpenSslError();
}
EVP_MD_CTX_destroy(ctx);
return ret;
}
QByteArray CryptEngine::createRandomIv(int length)
{
QByteArray ret(length, 0);
qint64 m_time = QDateTime::currentMSecsSinceEpoch();
RAND_seed(&m_time, sizeof m_time);
if(1 != RAND_bytes((unsigned char *) ret.data(), ret.length()))
{
handleOpenSslError();
}
return ret;
}
void CryptEngine::handleOpenSslError()
{
throw std::runtime_error(ERR_reason_error_string(ERR_get_error()));
}
void CryptEngine::setKey(QString value)
{
m_cryptClass->setKey( value.toLatin1() );
}
void CryptEngine::setKey(QByteArray value)
{
m_cryptClass->setKey( value );
}
void CryptEngine::setKey(const uchar *value, int keyLength )
{
m_cryptClass->setKey( value, keyLength );
}
QByteArray &CryptEngine::getKey()
{
return m_cryptClass->getKey();
}
void CryptEngine::setCryptText(QByteArray value)
{
m_cryptClass->setCryptText(value);
}
void CryptEngine::setCryptText(const uchar *value, int dataLength )
{
m_cryptClass->setCryptText(value, dataLength);
}
void CryptEngine::setClearText(QByteArray value)
{
m_cryptClass->setClearText(value);
}
void CryptEngine::setClearText(const uchar *value, int dataLength )
{
m_cryptClass->setClearText(value, dataLength);
}
void CryptEngine::encrypt()
{
//If cleartext present, set in CryptClass, encrypt, and fetch encrypted data
if( m_cryptClass->getClearText().length() > 0 )
{
m_cryptClass->encrypt();
}
}
void CryptEngine::decrypt()
{
//If crypttext present, set in CryptClass, decrypt, and fetch decrypted data
if( m_cryptClass->getCryptText().length() > 0 )
{
m_cryptClass->decrypt();
}
}
void CryptEngine::oncipherSelected_triggered(int cipher)
{
m_algorithm = cipher;
//Get Instance of Singleton mapping Integers to String for Cipher description
CiphersSingleton ciphers = CiphersSingleton::getInstance();
//Algorithm contains cipher as text.
QString algorithm = ciphers.textFromValue(m_algorithm);
//delete old CryptClass and create new one according to selected algorithm
if( algorithm == "NullCipher" )
{
delete m_cryptClass;
m_cryptClass = new CryptClassNullCipher;
qDebug("CryptEngine::oncipherSelected_triggered - NullCipher");
}
else if( algorithm == "Caesar" )
{
delete m_cryptClass;
m_cryptClass = new CryptClassCaesar;
qDebug("CryptEngine::oncipherSelected_triggered - Caesar");
}
else if( algorithm == "RC4" )
{
delete m_cryptClass;
m_cryptClass = new CryptClassRc4;
qDebug("CryptEngine::oncipherSelected_triggered - RC4");
}
else if( algorithm == "Cube" )
{
// delete m_cryptClass;
// m_cryptClass = new CryptClassCube;
qDebug("CryptEngine::oncipherSelected_triggered - Cube");
}
else if( algorithm == "AES" )
{
delete m_cryptClass;
m_cryptClass = new CryptAes;
qDebug("CryptEngine::oncipherSelected_triggered - AES");
}
else if( algorithm == "RSA" )
{
// delete m_cryptClass;
// m_cryptClass = new CryptClassRSA;
qDebug("CryptEngine::oncipherSelected_triggered - RSA");
}
else if( algorithm == "ECC" )
{
// delete m_cryptClass;
// m_cryptClass = new CryptClassECC;
qDebug("CryptEngine::oncipherSelected_triggered - ECC");
}
//Send Signal confirming if data is binary or not
emit binaryData( m_cryptClass->getBinaryData() );
}
void CryptEngine::onkeylengthChanged(int value, bool symmetric)
{
qDebug("CrptEngine::onkeylengthChanged - new Keylength: %d", value);
m_cryptClass->setKeyLength(value);
}
|