Salut,
je crypte et décrypte des messages à l'aide d'openssl et de l'algorithme aes cependant parfois lorsque je décrypte ma chaine de caractère est vide. (Donc parfois mon programme s'exécute sans soucis parfois il plante)
Et je ne comprend pas pourquoi, voici le code de ma classe qui crypte les messages :
Le .h :
Et le .cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #ifndef AES_ENC_H #define AES_ENC_H #include <iostream> #include <openssl/evp.h> #include <openssl/err.h> #include <ctime> #include <openssl/rand.h> #include <openssl/aes.h> #include "../Core/utilities.h" #include "../Math/maths.h" #define FAILURE -1 #include "export.hpp" namespace odfaeg { class ODFAEG_NETWORK_API AES_ENC { public: static int encrypt(const char* data, size_t dataSize, char** encData); static int decrypt(const char* encData, size_t dataSize, char** data); static char* getKey (); static char* getIv(); static void setKey (char* key); static void setIv (char* iv); private: AES_ENC(); ~AES_ENC() { delete[] key; delete[] iv; delete[] aesSalt; delete[] aesPass; delete e_ctx; delete d_ctx; } static unsigned char* generateKey(int length) { size = length; aesPass = new unsigned char[size / 8]; RAND_bytes(aesPass, size / 8); int nrounds = 5; RAND_bytes(aesSalt, size / 8); EVP_CIPHER_CTX_init(e_ctx); EVP_CIPHER_CTX_init(d_ctx); /* * Gen key & IV for AES 256 CBC mode. A SHA1 digest is used to hash the supplied key material. * nrounds is the number of times the we hash the material. More rounds are more secure but * slower. */ if (EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), aesSalt, aesPass, size / 8, nrounds, key, iv) == 0) std::cerr<<"Failed to hash the aes key."<<std::endl; return aesPass; } static int size; static std::string aes_key; static unsigned char *key, *iv, *aesSalt, *aesPass; static EVP_CIPHER_CTX* e_ctx, *d_ctx; }; } #endif // AES_ENC
PS : apparemment le problème vient lors de la génération de la clé. (Parfois, elle est mauvaise)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include "../../../include/odfaeg/Network/aes.h" namespace odfaeg { using namespace std; #include <string.h> int AES_ENC::size = 0; unsigned char* AES_ENC::key = new unsigned char[32]; unsigned char* AES_ENC::iv = new unsigned char[32]; unsigned char* AES_ENC::aesSalt = new unsigned char[32]; EVP_CIPHER_CTX* AES_ENC::e_ctx = new EVP_CIPHER_CTX; EVP_CIPHER_CTX* AES_ENC::d_ctx = new EVP_CIPHER_CTX; unsigned char* AES_ENC::aesPass = generateKey(256); int AES_ENC::encrypt(const char* data, size_t dataSize, char **encData) { size_t blockLen = 0; size_t encDataLen = 0; *encData = new char[dataSize + AES_BLOCK_SIZE]; if (!EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), NULL, key, iv)) return FAILURE; if (!EVP_EncryptUpdate(e_ctx, (unsigned char*) *encData, (int*)&blockLen, (unsigned char*) data, dataSize)) return FAILURE; encDataLen += blockLen; if(!EVP_EncryptFinal_ex(e_ctx, (unsigned char*) *encData + encDataLen, (int*)&blockLen)) return FAILURE; EVP_CIPHER_CTX_cleanup(e_ctx); return encDataLen + blockLen; } int AES_ENC::decrypt(const char* encData, size_t dataSize, char **data) { size_t dataLen = 0; size_t blockLen = 0; char *msg = new char[dataSize]; if (!EVP_DecryptInit_ex(d_ctx, EVP_aes_256_cbc(), NULL, key, iv)) return FAILURE; if (!EVP_DecryptUpdate(d_ctx, (unsigned char*) msg, (int*)&blockLen, (unsigned char*) encData, (int)dataSize)) return FAILURE; dataLen += blockLen; if (!EVP_DecryptFinal_ex(d_ctx, (unsigned char*) msg + dataLen, (int*)&blockLen)) return FAILURE; dataLen += blockLen; *data = new char[dataLen+1]; memcpy(*data, msg, dataLen); (*data)[dataLen] = '\0'; delete[] msg; EVP_CIPHER_CTX_cleanup(d_ctx); return dataLen; } void AES_ENC::setKey(char* sKey) { strncpy((char*) key, sKey, size / 8); } void AES_ENC::setIv(char* sIv) { strncpy((char*) iv, sIv, size / 8); } char* AES_ENC::getKey() { return (char*) key; } char* AES_ENC::getIv() { return (char*) iv; } }
Partager