Python Forum
AES encryption - does not match between arduino and python 3 crypto
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AES encryption - does not match between arduino and python 3 crypto
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

Possibly Related Threads…
Thread Author Replies Views Last Post
  need help one time pad encryption implementation ! nad556 1 529 Nov-28-2020, 06:11 PM
Last Post: nad556
  encryption and decryption with python ibrahim 1 612 May-16-2020, 03:14 PM
Last Post: Larz60+
  Can't transmit serial fast Python to Arduino pyserial mRKlean 0 908 Mar-29-2020, 08:12 PM
Last Post: mRKlean
  Problem with using Crypto library reks2004 2 852 Mar-27-2020, 05:38 AM
Last Post: buran
  About Arduino and Python usb webcam tuts Simurg 1 663 Mar-20-2020, 07:25 PM
Last Post: Larz60+
  Help Graphing Arduino Data Real Time in Python nschulz 0 1,082 Mar-12-2020, 06:15 PM
Last Post: nschulz
  Python crypto byte plaintext to hexint Mangaz90 2 1,017 Feb-21-2020, 07:42 PM
Last Post: Mangaz90
  File encryption itzik 5 919 Nov-05-2019, 12:29 PM
Last Post: Gribouillis
  Vernam encryption method for files JohnCTX 1 818 Sep-18-2019, 04:31 PM
Last Post: JohnCTX
  Create file archive that contains crypto hash ED209 1 880 May-29-2019, 03:05 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020