Peter Boldt
Peter Boldt

Reputation: 73

Arduino AES128 encryption - decryption problem

I have a following code on my Arduino board:

#include <Crypto.h>
#include <base64.hpp>
#define BLOCK_SIZE 16
uint8_t key[BLOCK_SIZE] = { 1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7 };
uint8_t iv[BLOCK_SIZE] = { 7,6,5,4,3,2,1,9,8,7,6,5,4,3,2,1 };

void bufferSize(char* text, int &length)
{
  int i = strlen(text);
  int buf = round(i / BLOCK_SIZE) * BLOCK_SIZE;
  length = (buf <= i) ? buf + BLOCK_SIZE : length = buf;
}

void encrypt(char* plain_text, char* output, int length)
{
  byte enciphered[length];
  AES aesEncryptor(key, iv, AES::AES_MODE_128, AES::CIPHER_ENCRYPT);
  aesEncryptor.process((uint8_t*)plain_text, enciphered, length);
  int encrypted_size = sizeof(enciphered);
  char encoded[encrypted_size];
  encode_base64(enciphered, encrypted_size, (unsigned char*)encoded);
  strcpy(output, encoded);
}

void decrypt(char* enciphered, char* output, int length)
{
  length = length + 1; //re-adjust
  char decoded[length];
  decode_base64((unsigned char*)enciphered, (unsigned char*)decoded);
  bufferSize(enciphered, length);
  byte deciphered[length];
  AES aesDecryptor(key, iv, AES::AES_MODE_128, AES::CIPHER_DECRYPT);
  aesDecryptor.process((uint8_t*)decoded, deciphered, length);
  strcpy(output, (char*)deciphered);
}

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ; //wait
  }

}

void loop() {
  char plain_text[] = "123456789";
  unsigned long time = 0;

  time = micros();
  encrypt;
  int length = 0;
  bufferSize(plain_text, length);
  char encrypted[length];
  encrypt(plain_text, encrypted, length);
  Serial.println(encrypted); 
  decrypt;
  length = strlen(encrypted);
  char decrypted[length];
  decrypt(encrypted, decrypted, length);
  Serial.print("Decrypted: ");
  Serial.println(decrypted);
  delay(1000);
}

It can encrypt and decrypt, i have on serial next output:

NJf0oXNZ92NVczkeXEUhkg==
Decrypted: 123456789

But the problem is, if i use an online tool for decryption https://www.devglan.com/online-tools/aes-encryption-decryption , with Key 1234567891234567, IV 7654321987654321 and CBC 128 (even with ECB 128 without IV) i just receive an error message:

Given final block not properly padded. Such issues can arise if a bad key is used during decryption.

What is wrong with my code?

Upvotes: 2

Views: 1825

Answers (2)

Luke Joshua Park
Luke Joshua Park

Reputation: 9806

Nearly all online "AES tools" are made very poorly, usually by people with very little understanding of cryptography - do not rely on them to test your code.

Instead, test your code against well defined test vectors, like these ones. I've included the first test case below:

Key       : 0x06a9214036b8a15b512e03d534120006
IV        : 0x3dafba429d9eb430b422da802c9fac41
Plaintext : "Single block msg"
Ciphertext: 0xe353779c1079aeb82708942dbe77181a

Upvotes: 2

Pras
Pras

Reputation: 4044

If you are specifying the key as 1234567891234567, the online tool may interpret it wrongly, most of them expect the keys to be provided in hex, so you should try specifying the key as 01020304050606080901020304050607, same applies to IV as well

Upvotes: 1

Related Questions