Python Forum
AES encryption - does not match between arduino and python 3 crypto - Printable Version

+- Python Forum (
+-- Forum: Python Coding (
+--- Forum: General Coding Help (
+--- Thread: AES encryption - does not match between arduino and python 3 crypto (/Thread-AES-encryption-does-not-match-between-arduino-and-python-3-crypto)

AES encryption - does not match between arduino and python 3 crypto - guillaume55 - Sep-23-2018

I'm trying to send encrypted data from python3 to an esp8266 via mqtt.
Currently mqtt with no encryption works but i'm stuck on the encryption part.
I have two codes: Esp8266 C code for arduino environment and python3 code for the server. Codes just encrypt the same string with the same iv and key. I expected to have the same encrypted string but I get different strings. I've tried with several "MODE" for python to match with the arduino code but I'm unable to get the same string. Block size is 16 for both.

Here is the python3 code :
from Crypto.Cipher import AES
obj ='1111111111111111', AES.MODE_CBC, '0000000000000000')
message = "The answer is no"
ciphertext = obj.encrypt(message)
obj2 ='1111111111111111', AES.MODE_CBC, '0000000000000000')
Output : b'\xe6\xc3\x88\x1a\xa5I\xa7qA\x16B\xd4\xb5s~Q'
b'The answer is no'

and the C code (library :
#include <Crypto.h>
#include <base64.hpp>

#define BLOCK_SIZE 16

uint8_t key[BLOCK_SIZE] = { 0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31, 0x31, 0x31 };
uint8_t iv[BLOCK_SIZE] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };

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];
  //RNG::fill(iv, BLOCK_SIZE);  //genere aleatoirement un iv 
  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() {
  while (!Serial) {
    ; //wait

void loop() {
  char plain_text[] = "The answer is no";
  unsigned long time = 0;
  time = micros();
  // encrypt
  int length = 0;
  bufferSize(plain_text, length);
  char encrypted[length];
  encrypt(plain_text, encrypted, length);

  Serial.print("Encrypted: ");
  Serial.print("en ");
  Serial.println(" µs");
  time = micros();
  // decrypt
  length = strlen(encrypted);
  char decrypted[length];
  decrypt(encrypted, decrypted, length);

  Serial.print("Decrypted: ");

   Serial.print("en ");
  Serial.println(" µs");

Output : Encrypted: Z9cuEgmxcZ1VZ4//dR6GA0x1U6WAIXjOh0hLSXTTWOM=
en 508 µs
Decrypted: The answer is no
en 493 µs

More over I don't know why I have to use a 16 byte string as a message for python and not for arduino code