I am also facing different results on encryption and decryption.
Code: Select all
void testdecode()
{
// only CBC requires that input length shall be multiple of 16
#define INPUT_LENGTH 16
mbedtls_aes_context aes;
// key length 32 bytes for 256 bit encrypting, it can be 16 or 24 bytes for 128 and 192 bits encrypting mode
uint8_t key[] = {0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
uint8_t iv[] = {0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
uint8_t iv2[] = {0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
uint8_t input[INPUT_LENGTH] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
uint8_t encrypt_output[INPUT_LENGTH] = {0};
uint8_t decrypt_output[INPUT_LENGTH] = {0};
mbedtls_aes_init(&aes);
mbedtls_aes_setkey_enc(&aes, key, 256);
//sprintf((char*)input, "%s","Hello Testing");
mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, INPUT_LENGTH, iv, input, encrypt_output);
Serial.println("Encrypted Array:");
for (uint8_t i=0; i<INPUT_LENGTH; i++)
{
Serial.print(String(encrypt_output[i])+" ");
}
Serial.println();
mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_DECRYPT, INPUT_LENGTH, iv2, encrypt_output, decrypt_output);
Serial.println("Decrypted Array:");
for (uint8_t i=0; i<INPUT_LENGTH; i++)
{
Serial.print(String(decrypt_output[i])+" ");
}
Serial.println();
mbedtls_aes_free(&aes);
}