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