I tried the following code and managed to find the issue.
#include <Crypto.h>
#include <AES.h>
#include <string.h>
#include <Arduino.h>
String textAfterEncryption;
String textAfterDecryption;
//key[16] cotain 16 byte key(128 bit) for encryption
byte key[16]={0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
//SSID[16] contain the text we need to encrypt
byte SSID_plaintext[11]={0x54, 0x50, 0x2d, 0x4c, 0x69, 0x6e, 0x6b, 0x5f, 0x44, 0x42, 0x30};
byte SSID_cypher[11];
byte SSID_decryptedtext[11];
byte PASS[8]={0x31, 0x33, 0x31, 0x35, 0x31, 0x30, 0x31, 0x30};
byte PASS_cypher[8];
byte PASS_decryptedtext[8];
AES128 aes128;
void encrypt(){
aes128.encryptBlock(SSID_cypher, SSID_plaintext);//cypher->output block and SSID->input block
Serial.println();
Serial.print("After Encryption:");
for(int j=0;j<sizeof(SSID_cypher);j++){
textAfterEncryption += (char)SSID_cypher[j];
}
Serial.print(textAfterEncryption);
textAfterEncryption = "";
aes128.encryptBlock(PASS_cypher,PASS);//cypher->output block and SSID->input block
Serial.println();
Serial.print("After Encryption:");
for(int j=0;j<sizeof(PASS_cypher);j++){
textAfterEncryption += (char)PASS_cypher[j];
}
Serial.print(textAfterEncryption);
}
void decrypt(){
aes128.decryptBlock(SSID_decryptedtext, SSID_cypher);
Serial.println();
Serial.print("After Decryption:");
for(int i=0; i<sizeof(SSID_decryptedtext); i++){
textAfterDecryption += (char)SSID_decryptedtext[i];
}
Serial.println(textAfterDecryption);
textAfterDecryption = "";
aes128.decryptBlock(PASS_decryptedtext, PASS_cypher);
Serial.println();
Serial.print("After Decryption:");
for(int i=0; i<sizeof(PASS_decryptedtext); i++){
textAfterDecryption += (char)PASS_decryptedtext[i];
}
Serial.println(textAfterDecryption);
}
void setup() {
Serial.begin(9600);
aes128.setKey(key,16);// Setting Key for AES
encrypt();
decrypt();
}
void loop() {
// put your main code here, to run repeatedly:
}
What I did is I created encrypt() function that encrypts two blocks, then I created the decrypt() function that decrypts the two blocks respectively. Doing so decrypts only the first block successfully while the second block weirdly. The only way that works is to encrypt then decrypt before encrypting anything else like so:
#include <Crypto.h>
#include <AES.h>
#include <string.h>
#include <Arduino.h>
String textAfterEncryption;
String textAfterDecryption;
//key[16] cotain 16 byte key(128 bit) for encryption
byte key[16]={0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
//SSID[16] contain the text we need to encrypt
byte SSID_plaintext[11]={0x54, 0x50, 0x2d, 0x4c, 0x69, 0x6e, 0x6b, 0x5f, 0x44, 0x42, 0x30};
byte SSID_cypher[11];
byte SSID_decryptedtext[11];
byte PASS[8]={0x31, 0x33, 0x31, 0x35, 0x31, 0x30, 0x31, 0x30};
byte PASS_cypher[8];
byte PASS_decryptedtext[8];
AES128 aes128;
void encrypt(){
aes128.encryptBlock(SSID_cypher, SSID_plaintext);//cypher->output block and SSID->input block
Serial.println();
Serial.print("After Encryption:");
for(int j=0;j<sizeof(SSID_cypher);j++){
textAfterEncryption += (char)SSID_cypher[j];
}
Serial.print(textAfterEncryption);
textAfterEncryption = "";
aes128.decryptBlock(SSID_decryptedtext, SSID_cypher);
Serial.println();
Serial.print("After Decryption:");
for(int i=0; i<sizeof(SSID_decryptedtext); i++){
textAfterDecryption += (char)SSID_decryptedtext[i];
}
Serial.println(textAfterDecryption);
textAfterDecryption = "";
//----------------------------------------------------------------
//----------------------PASS--------------------------------------
aes128.encryptBlock(PASS_cypher,PASS);//cypher->output block and SSID->input block
Serial.println();
Serial.print("After Encryption:");
for(int j=0;j<sizeof(PASS_cypher);j++){
textAfterEncryption += (char)PASS_cypher[j];
}
Serial.print(textAfterEncryption);
aes128.decryptBlock(PASS_decryptedtext, PASS_cypher);
Serial.println();
Serial.print("After Decryption:");
for(int i=0; i<sizeof(PASS_decryptedtext); i++){
textAfterDecryption += (char)PASS_decryptedtext[i];
}
Serial.println(textAfterDecryption);
}
void setup() {
Serial.begin(9600);
aes128.setKey(key,16);// Setting Key for AES
encrypt();
}
void loop() {
// put your main code here, to run repeatedly:
}
I also tried defining two AES128 variables so that I don't use the same one but that also did not work.
Any idea? Thanks!