AES Encryption over RF with nRF24L01+

Hello

I'm using the nRF24L01+ from Nordic Semiconductor to send sensor data to one receiver. I now would like to encrypt this data.

I'm using the following AES Library for Arduino (I'm working with the Arduino Pro Mini):

The encryption, decryption works perfectly on the same arduino. Means I can encrypt and decrypt a byte array.
But as soon as i send the array with the nRF24L01+, I receive the wrong data on the receiving end. The sending of the data works perfectly, but as soon as I try to encrypt the data, the data comes out wrong.

Could anyone help me or has a clue what could be wrong?

The code is the following

Sending Code, encrypted:

#include <AESLib.h>
#include <SPI.h>
#include <printf.h>
#include <nRF24L01.h>
#include <RF24_config.h>
#include <RF24.h>
#include <LowPower.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[] = "01234567890123hh"; //16 chars == 16 bytes
byte data2[] = {4,2,6,5,2,6,7,8,2,3,4,5,6,2,1,4};  




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();
 if(radio.isChipConnected()) {
  Serial.println("Chip is connected");
 }
   radio.stopListening();
   //radio.powerDown();

}



void loop() {
  

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

  delay(1000);
  
  radio.write(&data2, sizeof(data2));
  

}

And the receiving code which should do the decryption:

#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};
byte 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();
 if(radio.isChipConnected()) {
  Serial.println("Chip is connected");
 }
   radio.startListening();
 
 }

void loop() {

  if (radio.available()) {
    Serial.println("Data Coming");
    Serial.println("Encrypted");
    radio.read(&data3, sizeof(data3));
    for(int i=0; i<16; i++) {
        Serial.println(data3[i]);
    }

    Serial.println("Decrypted");
    aes128_enc_single(key, data3);
    delay(1000);
    for(int i=0; i<16; i++) {
        Serial.println(data3[i]);
    }
  }


}

You are encrypting your data over and over, so it seems quite hard for the receiver to guess how often is was.
Your receiver assumes oince, which will only be true for the very first packet.

The delay in the receiving sketch is counterproductive.

Thanks, thats what I thought as well after I played around a while.

Then I tried to send only one package, which was encrypted only once and it didn't work as well.

And I tried to send 3 encrypted packages one a time, and the data was wrong encrypted as well.

What else could be the problem?

Got it!!

Sorry horrible mistake.

I mixed use the encryption function for the decryption.

It works now!

any help for the working code

Blackfield3434:
Got it!!

Sorry horrible mistake.

I mixed use the encryption function for the decryption.

It works now!

could u show us the final code or tell us what was wrong in the code that u sent as i have tried with no result

any help?

i have made it and here are the final codes that worked for me

rx

#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]);
  }

}

tx

#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() {
aes128_enc_single(key, data3);
  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() {
 

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

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