//esp32 code
// import AES encryption library
#include "AESLib.h"
// import base64 conversion library
#include "arduino_base64.hpp"
// declare a global AESLib object
AESLib aesLib;
//
// AES key and IV (must match B4A key and IV)
byte aes_key[16] = {
0x54, 0x68, 0x69, 0x73, 0x49, 0x73, 0x41, 0x33,
0x32, 0x42, 0x79, 0x74, 0x65, 0x4b, 0x65, 0x79
}; // 16 bytes for AES-128
byte aes_iv[16] = {
0x49, 0x6e, 0x69, 0x74, 0x69, 0x61, 0x6c, 0x69,
0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x56, 0x63
}; // 16 bytes
// the decryption function
String decrypt(String encryptedBase64Text) {
// calculate the original length before it was coded into base64 string
int originalBytesLength = base64::decodeLength(encryptedBase64Text.c_str());
// declare empty byte array (a memory storage)
byte encryptedBytes[originalBytesLength];
byte decryptedBytes[originalBytesLength];
// convert the base64 string into original bytes
// which is the encryptedBytes
base64::decode(encryptedBase64Text.c_str(), encryptedBytes);
// initializing AES engine
// Cipher Mode and Key Size are preset in AESLib
// Cipher Mode = CBC
// Key Size = 128
//e bytes to be decrypted
// param 2 = the length of source bytes
// param 3 = the destination of decrypted bytes that will be saved
// param 4 = KEY
// param 5 = the length of KEY bytes (16)
// param 6 = IV
aesLib.decrypt(encryptedBytes, originalBytesLength,
decryptedBytes, aesKey, 16, aesIv);
// convert the decrypted bytes into original string
String decryptedText = String((char*)decryptedBytes);
return decryptedText;
}
usage:
String encryptedSting = server.arg("activatePw");
Serial.println(encryptedSting); // got the incomming encryption always right at all time
String pwrrly_decrypted = decrypt(encryptedSting);
Serial.println(pwrrly_decrypted); /got this right the first time after esp32 flash
but afterward jagged text that is not readable