The below code decrypt the incoming encrypted
text from b4a the first time immediately after esp32
flash but subsequently, the decrypted text is not readable anymore.
Could you go through and point out the issues. Thanks
// 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
The above code decrypt the incoming encrypted text from b4a the first time immediately after esp32 flash but subsequently, the decrypted text is not readable anymore. Can you go through and point out the issues. Thanks
Below is the b4a code:
Sub Process_Globals
Private Key As String = "ThisIsA16ByteKey" ' Must be 16 bytes for AES-128
Private iv As String = "InitializationVctr" ' IV must be 16 bytes
End Sub
'Sub GenerateIV As String
' Dim PWC As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
' Dim IV As String
' For i=0 To 15
' IV=IV & PWC.CharAt(Rnd(0,PWC.Length))
' Next
' Return IV
'End Sub
Sub AES_Encrypt(input As String, IV As String, pass As String) As String
Dim inputB() As Byte = input.GetBytes("UTF8")
Dim passB() As Byte = pass.GetBytes("UTF8")
Dim IVb() As Byte = IV.GetBytes("UTF8")
Dim kg As KeyGenerator
Dim C As Cipher
kg.Initialize("AES") 'Yes, AES only
kg.KeyFromBytes(passB)
C.Initialize("AES/CBC/PKCS5Padding")
C.InitialisationVector = IVb
Dim datas() As Byte = C.Encrypt(inputB, kg.Key, True)
Return SU.EncodeBase64(datas)
End Sub
usage:
Dim encryptthisstring As String = "thistest"
Dim encrytedString As String = EncryptText(encryptthisstring, iv, key)
Log(encrytedString)