Topic: [SOLVED] AES encrypt and decrypt error
Hi, I followed the examples from wolfssl documentation to use AES. -> https://www.wolfssl.com/wolfSSL/Docs-wo … ence.html.
After executing this code:
#include <wolfssl.h>
#include <wolfssl/wolfcrypt/aes.h>
int x = 0;
void setup(void){
while(!Serial);
Serial.begin(9600);
}
void loop(void){
//AES block
Aes enc;
Aes dec;
byte key[] = {"1234567890abcdef"};
byte iv[] = {"1234567890abcdef"};
byte plain[32] = {"Hola mundoHola mundoHola mundo"}; // an increment of 16, fill with data
byte plain2[32];
byte cipher[32];
Serial.print("plain value ");
for(x=0; x<32; x++){
Serial.print(plain[x], HEX);
Serial.print(" ");
}
Serial.println(" ");
Serial.print("printing key value ");
for(x=0; x<32; x++){
Serial.print(key[x], HEX);
Serial.print(" ");
}
Serial.println(" ");
Serial.print("printing iv value ");
for(x=0; x<32; x++){
Serial.print(iv[x], HEX);
Serial.print(" ");
}
Serial.println(" ");
//encrypt
wc_AesSetKey(&enc, key, sizeof(key), iv, AES_ENCRYPTION);
wc_AesCbcEncrypt(&enc, cipher, plain, sizeof(plain));
Serial.print("cipher value ");
for(x=0; x<32; x++){
Serial.print(cipher[x], HEX);
Serial.print(" ");
}
Serial.println(" ");
Serial.print("printing key value before cipher");
for(x=0; x<32; x++){
Serial.print(key[x], HEX);
Serial.print(" ");
}
Serial.println(" ");
Serial.print("printing iv value before cipher");
for(x=0; x<32; x++){
Serial.print(iv[x], HEX);
Serial.print(" ");
}
Serial.println(" ");
//decrypt
wc_AesSetKey(&dec, key, sizeof(key), iv, AES_DECRYPTION);
wc_AesCbcDecrypt(&dec, plain2, cipher, sizeof(cipher));
Serial.print("plain2 value ");
for(x=0; x<32; x++){
Serial.print(plain2[x], HEX);
Serial.print(" ");
}
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
delay(10000);
}
We get this:
We can see that is not changing any input parameter at the function, but in each Arduino iteration we see differents values from encryption and decryption.
Besides decryption value never matches with value that we had before encrypting.
I've even used two variables "plain" and "plain2" to avoid any possible conflict.
NOTE: "for" loops just serve to print on screen, do not affect functionality, so you can ignore it.
Any idea why it happens?