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
|
#include "cryptaes.h"
CryptAes::CryptAes()
: CryptClassEvp()
{
m_iv = QByteArray(16, 0);
}
const EVP_CIPHER *CryptAes::algorithm()
{
switch (m_keyLength)
{
case 128:
return EVP_aes_128_cbc();
break;
case 192:
return EVP_aes_192_cbc();
break;
case 256:
return EVP_aes_256_cbc();
break;
}
return EVP_enc_null();
}
void CryptAes::encrypt()
{
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, m_key.data(), m_key.size()))
{
handleOpenSslError();
}
m_key = QByteArray(256, 0);
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if (1 != EVP_DigestFinal_ex(ctx, (unsigned char *) m_key.data(), NULL))
{
handleOpenSslError();
}
EVP_MD_CTX_destroy(ctx);
CryptClassEvp::encrypt();
}
void CryptAes::decrypt()
{
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, m_key.data(), m_key.size()))
{
handleOpenSslError();
}
m_key = QByteArray(256, 0);
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if (1 != EVP_DigestFinal_ex(ctx, (unsigned char *) m_key.data(), NULL))
{
handleOpenSslError();
}
EVP_MD_CTX_destroy(ctx);
CryptClassEvp::decrypt();
}
|