GEt the chipper Data

How can I get the chipperbuffer data from this code : I wont to send the data to decrypt in another ESP 32

#include "mbedtls/aes.h"

void encrypt(char * plainText, char * key, unsigned char * outputBuffer){

mbedtls_aes_context aes;

mbedtls_aes_init( &aes );
mbedtls_aes_setkey_enc( &aes, (const unsigned char*) key, strlen(key) * 8 );
mbedtls_aes_crypt_ecb( &aes, MBEDTLS_AES_ENCRYPT, (const unsigned char*)plainText, outputBuffer);
mbedtls_aes_free( &aes );
}

void decrypt(unsigned char * chipherText, char * key, unsigned char * outputBuffer){

mbedtls_aes_context aes;

mbedtls_aes_init( &aes );
mbedtls_aes_setkey_dec( &aes, (const unsigned char*) key, strlen(key) * 8 );
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, (const unsigned char*)chipherText, outputBuffer);
mbedtls_aes_free( &aes );
}

void setup() {

Serial.begin(115200);

char * key = "abcdefghijklmnop";

char *plainText = "Tech tutorials x";
unsigned char cipherTextOutput[16];
unsigned char decipheredTextOutput[16];

encrypt(plainText, key, cipherTextOutput);
decrypt(cipherTextOutput, key, decipheredTextOutput);

Serial.println("\nOriginal plain text:");
Serial.println(plainText);

Serial.println("\nCiphered text:");
for (int i = 0; i < 16; i++) {

char str[3];

sprintf(str, "%02x", (int)cipherTextOutput[i]);
Serial.print(str);

}

Serial.println("\n\nDeciphered text:");
for (int i = 0; i < 16; i++) {
Serial.print((char)decipheredTextOutput[i]);
}
}

void loop() {}

Isn't the encrypted data held in the array cipherTextOutput?

yaps, but can I send chipperBuffer value to another ESP. in this case I use RX/TX for communication

when I have sent the value from the chipperbuffer, when the value is decrypted the result is weird. :
The result:
⸮d⸮

My expect result is :
111

I would start by printing out the contents of the cipherTextOutput buffer on the transmitter side, which the code you posted does already.

The on the receiver side, print out the bytes that have been received from the transmitter. These should (obviously!) be the same, otherwise you have a problem in either transmission or reception.

If you are still having problems, then post your code for both the transmitter and the receiver (use code tags - see forum guidelines) along with the serial outputs and you should hopefully get further help.

This code for Transmitter

#include "mbedtls/aes.h"

char * key = "abcdefghijklmnop";
char *plainText = "Tech tutorials x";
unsigned char cipherTextOutput[16];
unsigned char decipheredTextOutput[16];

void encrypt(char * plainText, char * key, unsigned char * outputBuffer) {

mbedtls_aes_context aes;

mbedtls_aes_init( &aes );
mbedtls_aes_setkey_enc( &aes, (const unsigned char*) key, strlen(key) * 8 );
mbedtls_aes_crypt_ecb( &aes, MBEDTLS_AES_ENCRYPT, (const unsigned char*)plainText, outputBuffer);
mbedtls_aes_free( &aes );
}

void decrypt(unsigned char * chipherText, char * key, unsigned char * outputBuffer) {

mbedtls_aes_context aes;

mbedtls_aes_init( &aes );
mbedtls_aes_setkey_dec( &aes, (const unsigned char*) key, strlen(key) * 8 );
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, (const unsigned char*)chipherText, outputBuffer);
mbedtls_aes_free( &aes );
}

void setup() {
Serial.begin(115200);
}

void loop() {
encrypt(plainText, key, cipherTextOutput);
decrypt(cipherTextOutput, key, decipheredTextOutput);

Serial.println("\nOriginal plain text:");
Serial.println(plainText);

Serial.println("\nCiphered text:");
for (int i = 0; i < 16; i++) {

char str[3];

sprintf(str, "%02x", (int)cipherTextOutput[i]);
Serial.print(str);
Serial2.print(str);

}

Serial.println("\n\nDeciphered text:");
for (int i = 0; i < 16; i++) {
Serial.print((char)decipheredTextOutput[i]);
}

delay(4000);
}

Result

22:01:51.741 -> Original plain text:
22:01:51.741 -> Tech tutorials x
22:01:51.741 -> 
22:01:51.741 -> Ciphered text:
22:01:51.741 -> 567a3b23b683d8488d5d40d2a56e31d2
22:01:51.741 -> 
22:01:51.741 -> Deciphered text:
22:01:51.741 -> Tech tutorials x

Code for receiver

#include "mbedtls/aes.h"

char * key = "abcdefghijklmnop";
char *plainText = "Tech tutorials x";
char cipherTextOutput[16] = {0};
unsigned char decipheredTextOutput[16];

void encrypt(char * plainText, char * key, unsigned char * outputBuffer) {

mbedtls_aes_context aes;

mbedtls_aes_init( &aes );
mbedtls_aes_setkey_enc( &aes, (const unsigned char*) key, strlen(key) * 8 );
mbedtls_aes_crypt_ecb( &aes, MBEDTLS_AES_ENCRYPT, (const unsigned char*)plainText, outputBuffer);
mbedtls_aes_free( &aes );
}

void decrypt(unsigned char * chipherText, char * key, unsigned char * outputBuffer) {

mbedtls_aes_context aes;

mbedtls_aes_init( &aes );
mbedtls_aes_setkey_dec( &aes, (const unsigned char*) key, strlen(key) * 8 );
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, (const unsigned char*)chipherText, outputBuffer);
mbedtls_aes_free( &aes );
}

void setup() {
Serial.begin(115200);
Serial2.begin(9600);
}

void loop() {
if (Serial2.available())
{
int i = 0;
while (Serial2.available() && i < 63)
{
cipherTextOutput[i] = (char)Serial2.read();
i++;
}
cipherTextOutput[i] = '\0';
if (cipherTextOutput[0])
{
Serial.print("data diterima dari Lora ");
Serial.println(cipherTextOutput);
}

unsigned char * cipherText = (unsigned char *)cipherTextOutput;
decrypt(cipherText, key, decipheredTextOutput);

Serial.println("\n\nDeciphered text:");
for (int i = 0; i < 16; i++) {
  Serial.print((char)decipheredTextOutput[i]);
}

}
}

Result

23:09:48.592 -> Deciphered text:
23:09:48.592 -> ⸮⸮⸮p⸮,ע?̸⸮⸮d

The result I expect is that he successfully decrypts the sent ciphertext

Please Help Me

If you don't want to incur the wrath of the moderators, then please use code tags when posting your code. See the forum guide here:

In your receiver code, print out the contents of the cypherTextOutput array.

And in your receiver code:

What do you think will happen when the variable i increments beyond the bounds of the cipherTextOutput array?

EDIT: The cipherTextOutput array (once you've sorted out your serial handling) should contain the text "567a3b23b683d8488d5d40d2a56e31d2". This is not the same as the enciphered contents of the cipherTextOutput array on the transmitter. Your transmitter has converted the binary data into an ASCII hexadecimal representation (from your sprintf statement). Your receiver needs to convert the received ASCII hexadecimal data back into binary data before passing it to the decrypt function.

thanks for your hel, but can your give an example

I'll try and put an example together for you based on your code.

1 Like

Thanks You Sir

I had a look at your code with a view to giving you a working example, but I forgot that you are using an ESP32. I've only got UNOs and a MEGA2560 here so can't use your AES library.

There is an AES library for an UNO bit I think it would be sufficiently different to cause you more confusion.

So here are some steps that I think would be worth following:

  1. In your transmitter code, send out the character '<' before you print out the enciphered text to the software serial port, and the character '>' after all the enciphered text has been printed.

  2. On to the receiver side now. Have a read of the Serial Input Basics tutorial, especially example 3 now that the transmitter side had been adjusted to send the start and end markers. You can probably lift the whole recvWithStartEndMarkers function from the example.

  3. You now need to convert the ASCII hexadecimal bytes back into plain old bytes. 2 ASCII characters are needed to create 1 byte. You need to convert '0'..'9' into the actual values 0..9, which with ASCII, you can do by subtracting 48 from the character. Have a look at the ASCII tables to understand how this is working. 'A'..'F' and 'a'..'f' are handled similarly. You need to do this 2 times (shift the first value left 4x before OR'ing in the second value).

  4. You should now have the binary data that your transmitter created when it enciphered the data. You can then pass this to your decrypt function to get the original message back.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.