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