I am trying to combine AES-256 encryption output into a string (to compare this string with the encrypted string sent from the Android phone).
Basically, concatenation seems to work, but after several startup errors (unreadable characters, the string gets shorter, not longer), or crashes occur. It is reproducible, crashing at the same point after a restart.
I extracted some lines of Arduino code that demonstrate the problem. It performs the following actions:
- Create a random number and write it to an array (works)
- AES-encode this array (works)
- Build a HEX representation of each array index (works)
- Merge indexes with string (crash)
#include <SPI.h>
#include "aes256.h"
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 };
aes256_context ctxt;
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, };
long InitialRandom = random(2147483647);
String RandomString = "" ;
RandomString+=InitialRandom;
Serial.println(RandomString);
for (int x=0; x<RandomString.length(); x++){
data[x] = RandomString[x];
}
aes256_init(&ctxt, key);
aes256_encrypt_ecb(&ctxt, data);
aes256_done(&ctxt);
String encrypted="";
for (int x=0; x<sizeof(data); x++){
int a = data[x];
String b = String (a, HEX);
if(b.length()==1) b="0"+b;
encrypted.concat(b);
Serial.println(encrypted);
}
Serial.println();
}
, (+ operator, strcat). . , String , IDE Arduino 1.0.
, ,
!