[solved] AES & nRf24l01

hi guys

i am trying to send encrypted data using AES via a pair of nrf24l01 and arduino uno boards,i have made two sketches but can not get it working

the used library for AES GitHub - DavyLandman/AESLib: Arduino Library for AES Encryption (source based on avr-crypto-lib)

for transmitter

#include <AESLib.h>
#include <SPI.h>
#include <printf.h>
#include <nRF24L01.h>
#include <RF24_config.h>
#include <RF24.h>

// Funk defines
#define CE_PIN 9
#define CSN_PIN 10

// Funk
// create RF24 radio object using selected CE and CSN pins
RF24 radio(CE_PIN,CSN_PIN);

// Funk
// Pipe Adresse
const uint64_t address = 0xE7E7E7E7E7;


byte key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
char data3[] = "0123456789012345"; //16 chars == 16 bytes


void setup() {

  Serial.begin(115200);
  //Serial.println(sizeof(myData));

  printf_begin();

  // Funkverbindung
 radio.begin();
 radio.setPALevel(RF24_PA_MIN);
 radio.setChannel(100); //  geht bis 125
 radio.setDataRate(RF24_1MBPS);   // geht bis max. 2MBPS
 //radio.setPayloadSize(32);
 radio.setAutoAck(0);
 radio.enableDynamicPayloads();
 radio.setRetries(15,15);
 radio.openWritingPipe(address);
 radio.printDetails();
 

}



void loop() {
 
aes128_enc_single(key, data3);
  
  Serial.println("encrypted:");
  for(int i=0; i<16; i++) {
    Serial.print(data3[i]);
  }

 
  radio.write(data3, sizeof(data3));
 
}

and for the receiver

#include <SPI.h>
#include <printf.h>
#include <nRF24L01.h>
#include <RF24_config.h>
#include <RF24.h>
#include <AESLib.h>


// Funk defines
#define CE_PIN 9
#define CSN_PIN 10

byte key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
char data3[16];

// Funk
// create RF24 radio object using selected CE and CSN pins
RF24 radio(CE_PIN,CSN_PIN);


// Funk
// Pipe Adresse
const uint64_t address = 0xE7E7E7E7E7;



void setup() {
  Serial.begin(115200);   //Starten der seriellen Kommunikation mit 9600 baud

  // Wird benötigt für die printDetails Funktion
  printf_begin();

  // Funkverbindung
 radio.begin();
 radio.setPALevel(RF24_PA_MIN);
 radio.setChannel(100); //  geht bis 125
 radio.setDataRate(RF24_1MBPS);
 //radio.setPayloadSize(32);
 radio.setAutoAck(0);
 radio.enableDynamicPayloads();
 radio.openReadingPipe(1,address);
 radio.printDetails();

   radio.startListening();
 
 }

void loop() {

  if (radio.available()) {
 
    radio.read(data3, sizeof(data3));
   
aes128_dec_single(key, data3);
   
  
  
        
    
  }
 Serial.println("Decrypted");
    for(int i=0; i<16; i++) {
    Serial.print(data3[i]);
  }

}

i have tried to use the sketches in this link but could not get it work too

https://forum.arduino.cc/index.php?topic=609003.0

"can not get it working" and "could not get it work" do not provide useful information.

What happens if you do not encrypt/decrypt ? Is it the encryption/decryption or the radio ? You need to narrow down the problem a bit. What do the Serial.print(...) statements show?

The OP of the AES Encryption over RF with nRF24L01+ - Project Guidance - Arduino Forum link claimed to use an Arduino Pro Mini. What Arduino are you using?

vaj4088:
"can not get it working" and "could not get it work" do not provide useful information.

What happens if you do not encrypt/decrypt ? Is it the encryption/decryption or the radio ? You need to narrow down the problem a bit. What do the Serial.print(...) statements show?

The OP of the AES Encryption over RF with nRF24L01+ - Project Guidance - Arduino Forum link claimed to use an Arduino Pro Mini. What Arduino are you using?

i am using arduino uno on both transmitter and receiver

and what i ment by can not get it work means i couldnot get the real message that i trasnmit encrypted being decrypted on the receiver size

So, what happens if you skip the encryption/decryption ?

vaj4088:
So, what happens if you skip the encryption/decryption ?

the couple of nrf work normally if i skip encryption and decryption

Ah, good, that helps narrow down the issue. Unfortunately, I am unfamiliar with that encryption/decryption library. As the problem seems to be in the encryption/decryption library or your use of it, I can be of no further help. Sorry!

vaj4088:
Ah, good, that helps narrow down the issue. Unfortunately, I am unfamiliar with that encryption/decryption library. As the problem seems to be in the encryption/decryption library or your use of it, I can be of no further help. Sorry!

thank u very much

Does encrypt --> decrypt work without transmission over the radio?

Bigger picture question would be why do you think you need to encrypt the data at all?

gfvalvo:
Does encrypt --> decrypt work without transmission over the radio?

Bigger picture question would be why do you think you need to encrypt the data at all?

yes encrp/decrypt is working normally

and for why i need to use encryption decryption cz this is a part of a project that requires privacy so can not send raw data in air

Couple things:

float ackMessg[2]={1,2};
byte ackMessgLen = 4; // NB this 4 is the number of bytes in the 2 ints that will be received

Why is 'ackMessg' an array of floats when the next line says it will be 2 ints? The length of two floats would be 8.

I'm not familiar with the AESLib.h library, but from your code it looks like the function aes256_enc_single() encrypts the plaintext in situ. If so, on every pass through loop it would be re-encrypting the previous encryption results, not the original plaintext. Is that what you want?

If it were my project, I'd print the value stored in each byte of the array (in decimal or hex) right before it's sent with radio.write(). Then, I'd print the raw bytes received at the RX end. Compare the two for match.

gfvalvo:
Couple things:

float ackMessg[2]={1,2};

byte ackMessgLen = 4; // NB this 4 is the number of bytes in the 2 ints that will be received



Why is 'ackMessg' an array of floats when the next line says it will be 2 ints? The length of two floats would be 8.

I'm not familiar with the AESLib.h library, but from your code it looks like the function aes256_enc_single() encrypts the plaintext in situ. If so, on every pass through loop it would be re-encrypting the previous encryption results, not the original plaintext. Is that what you want?

If it were my project, I'd print the value stored in each byte of the array (in decimal or hex) right before it's sent with radio.write(). Then, I'd print the raw bytes received at the RX end. Compare the two for match.

makes sense

mestek86:
makes sense

i have tried making some attempts but with no results
anyone can help me or at least give me a clue?

mestek86:
i have tried making some attempts but with no results

Show your 'best' (new) attempt; full code both receiver and transmitter.

latest attempt

trasnmitter

#include <AESLib.h>
#include <SPI.h>
#include <printf.h>
#include <nRF24L01.h>
#include <RF24_config.h>
#include <RF24.h>


// Funk defines
#define CE_PIN 9
#define CSN_PIN 10

// Funk
// create RF24 radio object using selected CE and CSN pins
RF24 radio(CE_PIN,CSN_PIN);

// Funk
// Pipe Adresse
const uint64_t address = 0xE7E7E7E7E7;

byte key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
char data3[16] = "0123456789012345"; //16 chars == 16 bytes

void setup() {

  Serial.begin(115200);
  //Serial.println(sizeof(myData));
aes128_enc_single(key, data3);
  printf_begin();

  // Funkverbindung
 radio.begin();
 radio.setPALevel(RF24_PA_MIN);
 radio.setChannel(100); //  geht bis 125
 radio.setDataRate(RF24_1MBPS);   // geht bis max. 2MBPS
 //radio.setPayloadSize(32);
 radio.setAutoAck(0);
 radio.enableDynamicPayloads();
 radio.setRetries(15,15);
 radio.openWritingPipe(address);
 radio.printDetails();
 
   radio.stopListening();
   //radio.powerDown();

}

void loop() {
 
  radio.write(data3, sizeof(data3));
 

}

receiver

#include <SPI.h>
#include <printf.h>
#include <nRF24L01.h>
#include <RF24_config.h>
#include <RF24.h>
#include <AESLib.h>


// Funk defines
#define CE_PIN 9
#define CSN_PIN 10


byte key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
char data3[16];



// Funk
// create RF24 radio object using selected CE and CSN pins
RF24 radio(CE_PIN,CSN_PIN);


// Funk
// Pipe Adresse
const uint64_t address = 0xE7E7E7E7E7;



void setup() {
  Serial.begin(115200);   //Starten der seriellen Kommunikation mit 9600 baud

  // Wird benötigt für die printDetails Funktion
  printf_begin();

  // Funkverbindung
 radio.begin();
 radio.setPALevel(RF24_PA_MIN);
 radio.setChannel(100); //  geht bis 125
 radio.setDataRate(RF24_1MBPS);
 //radio.setPayloadSize(32);
 radio.setAutoAck(0);
 radio.enableDynamicPayloads();
 radio.openReadingPipe(1,address);
 radio.printDetails();

   radio.startListening();
 
 }

void loop() {

  if (radio.available()) {
 
    radio.read(&data3, sizeof(data3));
   

   
  
  
        
    
  }
 Serial.println("Decrypted");
    aes128_dec_single(key, data3);
Serial.print(data3);
}

i got strange characters at the receiver side

Per my suggestion in Reply #10: Did you try to print the actual byte contents of the array right before sending it and right after receiving it?

gfvalvo:
Per my suggestion in Reply #10: Did you try to print the actual byte contents of the array right before sending it and right after receiving it?

yes but i did not include in the example cz i receive the text normally without encryption /decryption
the problem is when i use both together

Prove it. Encrypt the array. Print all of its bytes (I prefer using HEX). Send the array via radio. Print the received bytes. Post the code and the Serial output from both TX and RX.

gfvalvo:
Prove it. Encrypt the array. Print all of its bytes (I prefer using HEX). Send the array via radio. Print the received bytes. Post the code and the Serial output from both TX and RX.

ok

solved!!!!