AES128 Decypher - Not able to pass exact char array into uint8_t variable

Hi jremington, just to close the loop on this issue. I got the solution by casting a string into the uint8_t variable ... I was stuck the other way around trying to sort out the issue from the char copying approach. Thanks for your advice!

Test of proof and code that might be helpful to others, as follows:

  uint8_t key_array[16];
  uint8_t message_array[16];
  int i;
  String inputt = "65D0F7758B094114AFA6D33A5EA0716AF8EC2769085E888FA68FC6309FA0451E";
  String inputtKey="";
  String inputtMessage="";

void setup()
{
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Present Sketch name -> casting3.ino ");
  Serial.println(""); 
}

void loop() 
{
  Serial.print("initial string to convert .. ");Serial.println(inputt);
  Serial.println("");   
  
  inputtKey = inputt.substring(0,inputt.length()-32); 
  inputtMessage = inputt.substring(32,inputt.length()); 
  Serial.print("KEY part in the POST      .. ");Serial.print(inputtKey);Serial.println(" -> As a String");  
  Serial.print("MESSAGE part in the POST  .. ");Serial.print(inputtMessage);Serial.println(" -> As a String");   
  Serial.println("");
  
  // converting each pair of char into hexadecimal for uint8_t type
  for ( i = 0; i < 32; i += 2) 
  {
      key_array[i/2] = (uint8_t)strtol(inputtKey.substring(i, i+2).c_str(), NULL, 16);      
  }
  for ( i = 0; i < 32; i += 2) 
  {
      message_array[i/2] = (uint8_t)strtol(inputtMessage.substring(i, i+2).c_str(), NULL, 16);
  }
   
  // printing the outputs
  
  Serial.println("Final Outputs");
  Serial.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ");
  Serial.print("KEY                       .. ");printHex(key_array,16);Serial.println(" -> as uint8_t");
  Serial.print("MESSAGE                   .. ");printHex(message_array,16);Serial.println(" -> as uint8_t");

  while(1);
}

void printHex(uint8_t *text, size_t size)
{
  for (byte i = 0; i < size; i = i + 1)
  {
    if (text[i] < 16)
    {
      Serial.print("0");
    }
    Serial.print(text[i], HEX);
  }
}

You still need to get rid of the String nonsense.

String inputt = "65D0F7758B094114AFA6D33A5EA0716AF8EC2769085E888FA68FC6309FA0451E";
  String inputtKey="";
  String inputtMessage="";

NOPE - Fully solved!

The string is just to test the code. The way we pass it is through BlueTooth service, which BTW is already working pretty neatly. The code is my way of giving a hand back. Cheers!

Thanks for your HINT!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.