SergeyBel/AES

Length of encrypted data is incorrect

XDoubleU opened this issue · 1 comments

unsigned char* key = (unsigned char*)")4~UK(G_M^[[fZ1D";
unsigned char* iv = (unsigned char*)"GVCV]7NLe_9ngOYD";

unsigned char* plain = (unsigned char*)"AZERTY";
unsigned char* padded = addPadding(plain, strlen((char*)plain), strlen((char*)key));
printf("Plain length: %d\n", strlen((char*)plain));
printf("Padded length: %d\n", strlen((char*)padded));

unsigned int byteLength = strlen((char*)padded)*sizeof(unsigned char);
printf("Bytelength: %d\n", byteLength);
    
unsigned char* encryptedPadded = aes.EncryptCBC(padded, byteLength, key, iv);
printf("EncryptedPadded value is %s\n", encryptedPadded);
printf("EncryptedPadded length: %d\n", strlen((char*)encryptedPadded));

Using this code i get following output:

Plain length: 6
Padded length: 16
Bytelength: 16
EncryptedPadded value is ■jæ ä┼ý$!Î◄Ô=╠vZƒ■´▒
EncryptedPadded length: 22

I expect EncryptedPadded length to be 16 but it's 22.

FYI: addPadding uses PKCS7 padding & without padding the issue remains

Length of encrypted data is always equal to the length of plain data

Encrypted data is not null-terminated string that's why strlen function returns unpredictable result