Android Question B4xEncryption

Timm

Member
Can somebody help me with this: I have been trying to use b4a code and b4xencryption to decrypt/encrypt string between esp32 aesLib library using arduino ide since am not used to b4r.
Can somebody point me to the right direction:
1. B4a code using b4xencryption library connecting
2. Esp32 aesLib library code for bidirectional transmission using arduino ide code .
3. Simple decrypt/encypt example code between b4a and esp32.
thanks in advance
 

Timm

Member
Thanks for your response.
Note that I want this code from b4a encryption and esp32 arduino ide aesLib code compatibility.
This way:
1. Arduino ide ( not b4r) code using easLib to decrypt the incoming b4a encrypted hex string and vice Verser. Am good at arduino ide code
2. Could you provide simple code from the side of b4a encryption library and simple code from esp32 using arduino ide code to achieve this. Then I can pick up from here. Thanks alot
 
Upvote 0

Timm

Member
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)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…