Passing a String to radio.encrypt()

Started by jimboeri, May 28, 2016, 01:21:50 AM

jimboeri

I would like to remove the encryption key from the sketch itself and store it in EEPROM.

I have the key in EEPROM and have got it back & can print it out, however when I try to pass the string to radio.encrypt() I get the following error message.
'
exit status 1
no matching function for call to 'RFM69_ATC::encrypt(String&)


The code in the sketch is:
  radio_encrypt = "";
  char y;
  for(int x = RADIO_ENCRYPT; x < RADIO_ENCRYPT+16; x++){
    y = EEPROM.read(x);
    //Serial.println(y);
    radio_encrypt = radio_encrypt + y;
  }

  radio.initialize(FREQUENCY, radio_node, radio_network);
  #ifdef IS_RFM69HW
    radio.setHighPower(); //uncomment only for RFM69HW!
  #endif
  radio.encrypt(radio_encrypt);


I am using the Arduino IDE.

What am I doing wrong?

TomWS

Quote from: jimboeri on May 28, 2016, 01:21:50 AM
I would like to remove the encryption key from the sketch itself and store it in EEPROM.

I have the key in EEPROM and have got it back & can print it out, however when I try to pass the string to radio.encrypt() I get the following error message.
'
exit status 1
no matching function for call to 'RFM69_ATC::encrypt(String&)


The code in the sketch is:
  radio_encrypt = "";
  char y;
  for(int x = RADIO_ENCRYPT; x < RADIO_ENCRYPT+16; x++){
    y = EEPROM.read(x);
    //Serial.println(y);
    radio_encrypt = radio_encrypt + y;
  }

  radio.initialize(FREQUENCY, radio_node, radio_network);
  #ifdef IS_RFM69HW
    radio.setHighPower(); //uncomment only for RFM69HW!
  #endif
  radio.encrypt(radio_encrypt);


I am using the Arduino IDE.

What am I doing wrong?
Well, to me it looks like you've declared radio_encrypt to be a String object.  radio.encrypt() wants a pointer to a char array and there isn't an automatic conversion of a String object to a char *.

String objects are notoriously inefficient in an embedded controller and, since the encryption variable IS fixed length it's probably better to simply allocate the space as:
char radio_encrypt[16];    // sixteen bytes of storage
...
  char y;
  for(int x = 0; x <16; x++){
    y = EEPROM.read( RADIO_ENCRYPT+x);
    //Serial.println(y);
    radio_encrypt[x] = y;
  }


This also gives you a RAM variable to store the encryption key from which to copy into EEPROM.

BTW, the EEPROMex library would let you:
   EEPROM.writeBlock(RADIO_ENCRYPT, radio_encrypt, 16);    // there is a corresponding readBlock as well...


Tom

jimboeri

Many thanks Tom, you described exactly what I needed to do. I've implemented it now and it works great.
Now I don't need to have my encryption keys all over Github.