I have following c code snippet that try to encrypt "hello" using AES CBC encryption cipher. but some how, the encryption result from below code (mbedtls), is different from Java code result and online tool result. I'm a c programming language newbie, I tried to google around, and can't figure out where is the problem.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mbedtls/aes.h"
#include "mbedtls/base64.h"
#include "mbedtls/md5.h"
#include "mbedtls/cipher.h"
int main()
{
unsigned char key[] = "6c74ea86f5c867f1";
unsigned char iv[] = "5e64fe04bfd8363b";
unsigned char input[100] = {0};
memset(input, 0, 100 );
strcpy(input, "hello");
mbedtls_aes_context aes;
mbedtls_aes_init(&aes);
mbedtls_aes_setkey_enc( &aes, key, 128 );
mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, 16, iv, input, input );
int i;
for (i=0; i<100; i++)
{
printf("%x ",input[i]);
}
printf("\n");
size_t outlen=0;
char *dst = (char *)malloc(sizeof(char)*200);
mbedtls_base64_encode(dst, 100, &outlen, input, 16);
printf("cipher is: %s \n", dst);
}
the code above print "cipher is: W9T3jTvbtfYGUZ3yXkQ6wA== ", but the java encrption result using same key and iv is "Fr00ZupbEBVr4gwL/l+G2w==", and so is the result from https://www.devglan.com/online-tools/aes-encryption-decryption
Can some one help me out?