6

I try to concatenate the output of AES-256 encryption to a string (to compare this string against the encrypted String sent from an Android phone).

Basically, the concatination seems to work, but after a few runs errors (non readable characters, string getting shorter instead of longer) or crashes occur. It is reproducible, crashes at the exact same point after restart.

I extracted some lines of Arduino code that demonstrate the problem. It does the following:

  1. Create a random number and write it into an array (works)
  2. AES- encode this array (works)
  3. Build a HEX representation of each array index (works)
  4. Concatenate the indices to a String (crashes)

#include <SPI.h>
#include "aes256.h"  //include this lib

uint8_t key[] = {9,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,
                 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8 }; //the encryption key

aes256_context ctxt; //context needed for aes library


void setup() {  
  Serial.begin(9600);  
}


void loop() {

  uint8_t data[] = {
       0x53, 0x73, 0x64, 0x66, 0x61, 0x73, 0x64, 0x66,
       0x61, 0x73, 0x64, 0x66, 0x61, 0x73, 0x64, 0x65, }; //the message to be encoded  

  long InitialRandom = random(2147483647); //0 to highest possible long
  String RandomString = "" ; 
  RandomString+=InitialRandom; //random number to String            
  Serial.println(RandomString); //for debugging

  //update data array: Random String into data array                 
  for (int x=0; x<RandomString.length(); x++){
       data[x] = RandomString[x];
  }

  //this encrypts data array, which changes  
  aes256_init(&ctxt, key); //initialize the lib
  aes256_encrypt_ecb(&ctxt, data); //do the encryption     
  aes256_done(&ctxt);  

  //Here the problem starts.............................................
  String encrypted=""; //the string I need finally 

  for (int x=0; x<sizeof(data); x++){ //data array is 16 in size    
        int a = data[x]; 
        String b = String (a, HEX);
        if(b.length()==1) b="0"+b;  //if result is e.g "a" it should be "0a"                         
        encrypted.concat(b);  //this line causes the problem!!! 
        //Serial.println(b); //works perfect if above line commented out    
        Serial.println(encrypted); //see the string geting longer until problems occur      
  }
  //Here the problem ends.............................................

        Serial.println(); //go for next round, until crashing
}

I have searched the forums, tried different ways to concatenate (+ operator, strcat). All had similar effects. I read that the String library had a bug, updated Arduino IDE to 1.0.

This has kept me busy for days, any help is highly appreciated,

Thanks a lot!

1
  • It starts from 0 and grows to 32 byte (each for-loop adds 2 visible bytes) Commented Feb 7, 2012 at 18:07

1 Answer 1

4

You are probably running out of memory as Arduino only has a small amount.

Check how much memory you have free during your loop.

The culprit may be that the implementation of String (see Arduino WString.cpp) is using realloc(), and your memory is probably being heavily defregmented with one or two byte strings (each of which has a 16 byte heap header cost).

You could re-write the above more efficiently by pre-allocating a String reserve() functions to pre-allocate the buffer. Or re-write it using native C++ char arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

The suggestion from peterept worked perfect for my problem. I inserted the line "encrypted.reserve(33);" under the variable declaration. I searched the internet for the reserve() function on arduino and found some explanation on it: code.google.com/p/arduino/issues/detail?id=449 Thanks for your great

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.