I am trying to figure it out. Why are my encryption and decryption not correct?
The used library for AES:
https://github.com/DanielVukelich/Arduino-AES
This is for testing encrypt and decrypt. //https://www.hanewin.net/encrypt/aes/aes-test.htm
#include "Rij_Consts.h"
#include "Rijndael.h"
void setup() {
Serial.begin(115200);
//mbedtls_aes_context aes;
unsigned char iv[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
//char * key = "abcdefghijklmnop";
//const unsigned char key[16];
const unsigned char key[16] = {0xE8,0xE9,0xEA,0xEB,0xED,0xEE,0xEF,0xF0,0xF2,0xF3,0xF4,0xF5,0xF7,0xF8,0xF9,0xFA};
//unsigned char key[16] = {0xFA,0xF9,0xF8,0xF7,0xF5,0xF4,0xF3,0xF2,0xF0,0xEF,0xEE,0xED,0xEB,0xEA,0xE9,0xE8};//กลับ
//char *input = "Tech tutorials x";
const unsigned char input[16] ={0x01,0x4B,0xAF,0x22,0x78,0xA6,0x9D,0x33,0x1D,0x51,0x80,0x10,0x36,0x43,0xE9,0x9A};
//unsigned char input[16] ={0x9A,0xE9,0x43,0x36,0x10,0x80,0x51,0x1D,0x33,0x9D,0xA6,0x78,0x22,0xAF,0x4B,0x01};//กลับ
unsigned char output[16]; //6743C3D1519AB4F2CD9A78AB09A511BD ต้องได้ตามนี้
printf("Testing ECB mode:\n");
printf("Original Plaintext:\n");
for(int i = 0; i < 16; i++){
printf("%2.2X ", input[i]);
}
unsigned char keys[176];//for 128
Schedule_Keys(AES_128, key, 16, keys);
printf("\n\nKEYs:\n");
for(int i = 0; i < 16; i++){
printf("%2.2X ", keys[i]);
}
Encrypt(AES_128, input, 16, keys, output);
printf("\n\nCiphertext:\n");
for(int i = 0; i < 16; i++){
char str[16];
sprintf(str, "%2.2X ", output[i]);
Serial.print(str);
}
Decrypt(AES_128, input, 16, keys, output);
printf("\n\nCiphertext:\n");
for(int i = 0; i < 16; i++){
char str[16];
sprintf(str, "%2.2X ", output[i]);
Serial.print(str);
}
}
void loop() {}
My output : enter image description here
You can see my encryption and decryption not correct.
I tried to reconstruct the full code.
#include "Rij_Consts.h"
#include "Rijndael.h"
//https://www.hanewin.net/encrypt/aes/aes-test.htm สําหรับเทสเทียบ
void setup() {
Serial.begin(115200);
unsigned char iv[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
const unsigned char key[16] = {0xE8,0xE9,0xEA,0xEB,0xED,0xEE,0xEF,0xF0,0xF2,0xF3,0xF4,0xF5,0xF7,0xF8,0xF9,0xFA};
const unsigned char plainText[16] ={0x01,0x4B,0xAF,0x22,0x78,0xA6,0x9D,0x33,0x1D,0x51,0x80,0x10,0x36,0x43,0xE9,0x9A};
unsigned char cipherText[16]; //6743C3D1519AB4F2CD9A78AB09A511BD Answer
printf("Testing ECB mode:\n");
printf("Original Plaintext:\n");
for(int i = 0; i < 16; i++){
Serial.printf("%2.2X ", plainText[i]);
}
unsigned char keys[176];//for 128
Schedule_Keys(AES_128, key, 16, keys);
printf("\n\nKEYs:\n");
for(int i = 0; i < 16; i++){
Serial.printf("%2.2X ", keys[i]);
}
Encrypt(AES_128, plainText, 16, keys,cipherText );
Serial.printf("\n\nCiphertext:\n");
for (int i = 0; i < 16; i++) {
Serial.printf("%2.2X ", cipherText[i]);
}
unsigned char decryptedText[16];
Decrypt(AES_128,cipherText , 16, keys, decryptedText);
Serial.printf("\n\ndecryptedText:\n");
for (int i = 0; i < 16; i++) {
Serial.printf("%2.2X ", decryptedText[i]);
}
}
void loop() {}
1 Answer 1
There are some issues with your code.
As mentioned in my comment, Decrypt ()
expects the Cipher Text as input and generates the Decrypted text as an output. However, in your code you're using the Plain text (which you confusingly named "input") as an input. The correct input should be your Cipher Text (which is the output from the Encrypt ()
function and which you called "output"). Please read the documentation of the AES library.
I suggest the following changes to your code to get it working:
Replace the part of your code, following Decrypt (...)
until the end of setup()
with the following code:
unsigned char decryptedText[16];
Decrypt(AES_128, output, 16, keys, decryptedText);
Serial.printf("\n\ndecryptedText:\n");
for (int i = 0; i < 16; i++) {
Serial.printf("%2.2X ", decryptedText[i]);
}
I would also suggest you name your variables to avoid confusion, for example you could replace input[]
with plainText[]
and output[]
with cipherText[]
. Some of your Hex output code is also unnecessarily complex and could do with some restructuring.
I've compiled and run the above changes with your code on an ESP32 and it produces the expected Cipher text and correctly decrypts it. It produces the following output:
00:21:01.583 -> Testing ECB mode:
00:21:01.583 -> Original Plaintext:
00:21:01.630 -> 01 4B AF 22 78 A6 9D 33 1D 51 80 10 36 43 E9 9A
00:21:01.630 ->
00:21:01.630 -> KEYs:
00:21:01.630 -> E8 E9 EA EB ED EE EF F0 F2 F3 F4 F5 F7 F8 F9 FA
00:21:01.630 ->
00:21:01.630 -> Ciphertext:
00:21:01.630 -> 67 43 C3 D1 51 9A B4 F2 CD 9A 78 AB 09 A5 11 BD
00:21:01.630 ->
00:21:01.630 -> decryptedText:
00:21:01.630 -> 01 4B AF 22 78 A6 9D 33 1D 51 80 10 36 43 E9 9A
In your question you mention that the generated Cipher text is not as expected. I can't help you with that, as I can't reproduce it.
Decrypt
should be the output ofEncrypt
.