Hey @zachary
Hope your vacation is going great!
I have pretty much completed the ecb initialization of session on my project and am now working on cbc decryption. I have tried to follow your demo as closely as possible but am running into a problem I am not sure how to solve.
I am sending encrypted data via HTTP get to a PHP server which then replys back with a cbc encrypted packet. The return packet contains 32 characters. I take this raw encrypted return from the server and pass it to my function to decrypt. It is working well except for one problem. I call cbc decrypt and pass the 32 character string and it decrypts but only the first 16 characters correctly. The second chunk of 16 characters comes back as “gobilty goop”. Here is my decrypt fuction:
void cbcDecrypt(String messageBody){
const char *message = messageBody.c_str();
int length = strlen(message)+1;
//Create input and output buffers for encryption.
unsigned char buf[length];
unsigned char oBuf[strlen(message)];
//Make sure buffers are empy
memset(buf, 0, length);
memset(oBuf, 0, strlen(message));
//Copy message into input buffer
memcpy(buf, message, length);
size_t paddedLength = pad(buf, length);
//Get IV out of memory
unsigned char iv[16];
memset(iv, 0, 16);
for(int i = 0; i < 16; i++){
iv[i] = EEPROM.read(i+20);
}
//Decrypt message into output buffer
aes_setkey_dec(&aes, key, 128);
aes_crypt_cbc(&aes, AES_DECRYPT, paddedLength, iv, buf, oBuf);
//Store first 16 decrypted characters as round key for next cbc encrypted send
for(int i = 0; i < 16; i++){
EEPROM.write(i+36, oBuf[i]);
}
//Print out the decrypted message and the length of the encrypted message received
Serial.print("Decrypted return: ");
Serial.println((char *)oBuf);
Serial.print("Return length+1 = ");
Serial.println(length);
}
Does anything about this stand out to you? Feel like I am doing it as safely as possible but that second 16 character chunk just is not getting decrypted correctly.
Thank you,
Travis