I created some functions for AESLib
#include "AESLib.h"
byte aes_key[] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
// General initialization vector (same as in node-js example) (you must use your own IV's in production for full security!!!)
byte aes_iv[N_BLOCK] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
byte enc_iv[N_BLOCK] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
void CRYPTO_Init(void)
{
gen_iv(aes_iv);
set_paddingmode((paddingMode)0);
}
void CRYPTO_Encrypt(const char* msg, int msg_len, char* encrypted, byte iv[])
{
int cipherlength = AESLib_get_cipher_length(msg_len);
encrypt((byte*)msg, msg_len, encrypted, aes_key, sizeof(aes_key), iv);
}
void CRYPTO_Decrypt(char* msg, uint16_t msg_len, char* decrypted, byte iv[])
{
decrypt((byte*)msg, msg_len, decrypted, aes_key, sizeof(aes_key), iv);
}
Now I test it
uint32_t COM_Crypto(uint32_t argc, char** args)
{
if (argc == 0)
return MSG_MIS_ARG;
int msg_len = strlen(args[1]);
char encrypted[128 + 1];
char decrypted[2*(128 + 1)];
byte iv[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
CRYPTO_Encrypt(args[1], msg_len, encrypted, iv);
USART_SendString(SYS_USART, encrypted);
USART_SendString(SYS_USART,"\r");
msg_len = strlen(encrypted);
CRYPTO_Decrypt(encrypted, msg_len, decrypted, iv);
USART_SendString(SYS_USART, decrypted);
return MSG_OK;
}
I see encrypted data but when I decrypt it back I get gibberish. What do I do wrong?