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() {}
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() {}
Decrypt
should be the output ofEncrypt
.