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
#1
Hi,
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 = AES.new('1111111111111111', AES.MODE_CBC, '0000000000000000')
message = "The answer is no"
ciphertext = obj.encrypt(message)
print(ciphertext)
obj2 = AES.new('1111111111111111', AES.MODE_CBC, '0000000000000000')
print(obj2.decrypt(ciphertext))
Output : b'\xe6\xc3\x88\x1a\xa5I\xa7qA\x16B\xd4\xb5s~Q'
b'The answer is no'

and the C code (library : https://github.com/intrbiz/arduino-crypto)
#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() {
  Serial.begin(115200);
  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.println("");
  Serial.print("Encrypted: ");
  Serial.println(encrypted); 
  Serial.print("en ");
  Serial.print(micros()-time);
  Serial.println(" µs");
  time = micros();
  // decrypt
  length = strlen(encrypted);
  char decrypted[length];
  decrypt(encrypted, decrypted, length);

  Serial.print("Decrypted: ");
  Serial.println(decrypted);

   Serial.print("en ");
  Serial.print(micros()-time);
  Serial.println(" µs");

  delay(5000);
}
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
Thanks
Guillaume
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encryption and decryption with python ibrahim 1 302 May-16-2020, 03:14 PM
Last Post: Larz60+
  Can't transmit serial fast Python to Arduino pyserial mRKlean 0 310 Mar-29-2020, 08:12 PM
Last Post: mRKlean
  Problem with using Crypto library reks2004 2 415 Mar-27-2020, 05:38 AM
Last Post: buran
  About Arduino and Python usb webcam tuts Simurg 1 356 Mar-20-2020, 07:25 PM
Last Post: Larz60+
  Help Graphing Arduino Data Real Time in Python nschulz 0 518 Mar-12-2020, 06:15 PM
Last Post: nschulz
  Python crypto byte plaintext to hexint Mangaz90 2 653 Feb-21-2020, 07:42 PM
Last Post: Mangaz90
  File encryption itzik 5 453 Nov-05-2019, 12:29 PM
Last Post: Gribouillis
  Vernam encryption method for files JohnCTX 1 461 Sep-18-2019, 04:31 PM
Last Post: JohnCTX
  Create file archive that contains crypto hash ED209 1 486 May-29-2019, 03:05 AM
Last Post: heiner55
  Arduino / Python Environment Setup - PIP stevealbright 0 632 Feb-24-2019, 10:48 PM
Last Post: stevealbright

Forum Jump:

User Panel Messages

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