Anyone know how to decrypt with mbed aes CTR mode?
Posted: Tue Aug 13, 2019 3:08 pm
I'm trying to encrypt/decrypt data using mbed aes CTR mode. I can get the encryption to work, but I can't figure out how to make it decrypt. Below is my code:
I get the following:
aes input: 48454c4c4f
aes encrypt: 3856ba1f36
aes decrypt: 3853a9924d
As you can see, aes input =/= aes decrypt. Any ideas?
Code: Select all
mbedtls_aes_context aes;
unsigned char key[16];
unsigned char input[128] = {0x48, 0x45, 0x4c, 0x4c, 0x4f};
unsigned char output[128];
unsigned char decrypt[128];
size_t input_len = 40;
unsigned char nonce_counter[16] = {0};
unsigned char stream_block[16];
unsigned int nc_off = 0;
memcpy(key, key_128, 16);
printf("aes Key: \n");
for(int i = 0; i < 16; i++)
{
printf("%x",key[i]);
}
printf("\n");
mbedtls_aes_init(&aes);
mbedtls_aes_setkey_enc(&aes, key, 128);
mbedtls_aes_setkey_dec(&aes, key, 128);
mbedtls_aes_crypt_ctr(&aes, input_len, &nc_off, nonce_counter, stream_block, input, output);
printf("aes input: \n");
for(int i = 0; i < 5; i++)
{
printf("%x",input[i]);
}
printf("\n");
printf("aes encrypt: \n");
for(int i = 0; i < 5; i++)
{
printf("%x",output[i]);
}
printf("\n");
mbedtls_aes_crypt_ctr(&aes, input_len, &nc_off, nonce_counter, stream_block, output, decrypt);
printf("aes decrypt: \n");
for(int i = 0; i < 5; i++)
{
printf("%x",decrypt[i]);
}
printf("\n");
aes input: 48454c4c4f
aes encrypt: 3856ba1f36
aes decrypt: 3853a9924d
As you can see, aes input =/= aes decrypt. Any ideas?