I've printed the bytes values and the size of the encrypted packet and there are the same at both sides. (on the server and on the client, but openssl is still returning me an error sometimes, not at every execution)
data :
216 231 40 108 97 250 69 90 79 179 226 104 216 120 124 170 73 216 119 66 205 158 123 249 153 34 30 51 177 223 26 141 size : 32
data :
103 199 125 199 210 194 147 59 76 111 21 78 116 198 211 6 size : 16
Error encrypting message: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
data :
57 208 110 186 16 229 111 96 137 204 150 177 138 59 73 22 size : 16
Error encrypting message: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
data :
10 223 62 4 158 180 44 217 31 151 22 151 215 245 94 120 size : 16
Error encrypting message: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
data :
142 220 144 37 30 193 223 130 100 217 65 212 237 32 248 242 105 18 140 65 88 52 32 66 103 41 20 202 107 113 83 203 size : 32
This problem happens only when I send and receive data with sockets.
Is it possible that SFML wipes out some null bytes at the end of the packet when receiving data through a socket
And I can't find a forum, where can I get some help with openssl ?
Especially for those functions :
unsigned char* AES_ENC::encrypt(const unsigned char* data, int dataSize, int* newSize) {
int cLen = dataSize+AES_BLOCK_SIZE;
int fLen = 0;
unsigned char *encData = (unsigned char*) malloc(cLen);
if (!EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv))
return nullptr;
if (!EVP_EncryptUpdate(e_ctx, encData, &cLen, (unsigned char*) data, dataSize))
return nullptr;
if(!EVP_EncryptFinal_ex(e_ctx, encData+cLen, &fLen)) {
return nullptr;
}
*newSize = cLen + fLen;
return encData;
}
unsigned char* AES_ENC::decrypt(const unsigned char* encData, int dataSize, int* newSize) {
int pLen = dataSize;
int fLen = 0;
unsigned char *data = (unsigned char*) malloc(pLen);
if (!EVP_DecryptInit_ex(d_ctx, EVP_aes_256_cbc(), nullptr, key, iv))
return nullptr;
if (!EVP_DecryptUpdate(d_ctx, data, &pLen, (unsigned char*) encData, dataSize))
return nullptr;
if (!EVP_DecryptFinal_ex(d_ctx, data+pLen, &fLen)) {
char* err = (char*) malloc(130);
ERR_load_crypto_strings();
ERR_error_string(ERR_get_error(), err);
fprintf(stderr, "Error encrypting message: %s\n", err);
free(err);
return nullptr;
}
*newSize = pLen + fLen;
return data;
}