Decrypt AES128-CBC String

Hi,

I need to decrypt a vector crypted with AES128 with CBC.

My vector is composed by two block of 16 byte each. -> total 32 Byte.

I try to use the AES.h library found on the forum (new AES library - Libraries - Arduino Forum).

I created a function to build the initial value key (create_AESiv).

When I run the sketch, on Arduino Mega, the first error occur when I try to set the AES key with this command: aes.set_key(&key[0], 128)

#include <AES.h>

#define N_BLOCKS        2

void create_AESiv(void);

AES aes ;

byte key[16] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
byte AESiv_Vector[16];

//uint8_t Block1[16] = {0xB3,0x20,0x4A,0x5F,0xCB,0x17,0x57,0x29,0xF1,0xE5,0xEB,0x23,0x29,0x1A,0x3D,0xB0};
//uint8_t Block2[16] = {0x39,0x29,0xCC,0x7A,0x6E,0xAD,0x36,0xE1,0xCD,0x48,0x22,0x1A,0xD7,0x7D,0x57,0xFA}; 

byte EncryptedMSG[32] = {0xB3,0x20,0x4A,0x5F,0xCB,0x17,0x57,0x29,0xF1,0xE5,0xEB,0x23,0x29,0x1A,0x3D,0xB0, 
                         0x39,0x29,0xCC,0x7A,0x6E,0xAD,0x36,0xE1,0xCD,0x48,0x22,0x1A,0xD7,0x7D,0x57,0xFA};
                            
byte DecryptedMSG[32];                            
                     
                     
uint8_t Access_No =0x60;
uint8_t Manufactor[]={0xC5, 0x14};
uint8_t SerialNumber[]={0x67,0x07,0x60,0x24};
uint8_t Version=0x00;
uint8_t DeviceType=0x07;


void setup() { 
 //Initialize serial and wait for port to open:
  Serial.begin(19200); 
  Serial.println("Decrypt AES128"); 
  
  //Set Key
  if(aes.set_key(&key[0], 128))
    Serial.println("Key Set");
  else
    Serial.println("Key not Set");
  
  create_AESiv();
  
  if(aes.cbc_encrypt (EncryptedMSG, DecryptedMSG, 2, AESiv_Vector)){
      Serial.println("Decrypt Success");
  }else
      Serial.println("Decrypt Fail");
  

} 


void loop() { 


} 

void create_AESiv(void)
{
  AESiv_Vector[0]=Manufactor[0];
  AESiv_Vector[1]=Manufactor[1];
  AESiv_Vector[2]=SerialNumber[0];
  AESiv_Vector[3]=SerialNumber[1];  
  AESiv_Vector[4]=SerialNumber[2];  
  AESiv_Vector[5]=SerialNumber[3];  
  AESiv_Vector[6]=Version;  
  AESiv_Vector[7]=DeviceType; 
  AESiv_Vector[8]=Access_No; 
  AESiv_Vector[9]=Access_No;  
  AESiv_Vector[10]=Access_No; 
  AESiv_Vector[11]=Access_No;   
  AESiv_Vector[12]=Access_No; 
  AESiv_Vector[13]=Access_No;   
  AESiv_Vector[14]=Access_No;  
  AESiv_Vector[15]=Access_No;  
  

  
  Serial.print("AES IV Vector: ");
  SerialPrintHEXVector(AESiv_Vector,16);
    
}

void SerialPrintHEXVector(uint8_t *data, uint8_t dataLen)
{
  unsigned char idx=0;
 
  while(idx<dataLen){
      Serial.print(data[idx],HEX);
      Serial.print(" ");
      idx++;
  } 
  Serial.println(""); 
}

Could anyone help me?

Thanks to all

When I run the sketch, on Arduino Mega, the first error occur when I try to set the AES key with this command: aes.set_key(&key[0], 128)

And that error is?

By the way, key is only 16 bytes. Why tell the function that it is 128 bytes?

The set_key() function takes the length in bytes or in bits, the only valid values are
16,24,32,128,192,256 - 16 and 128 both mean the key is 16 bytes long for instance,
this is a convenience since the variants are named by their bit lengths and not everyone
can divide by 8(!)

If you are decrypting you must call cbc_decrypt, not cbc_encrypt... Should be
obvious I'd have thought. Did you run and examime the example? Checked the
test-vectors example gets the right answers?