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
  Facing issue in python regex newline match Shr 6 1,143 Oct-25-2023, 09:42 AM
Last Post: Shr
  Python error on mentioned Arduino port name dghosal 5 796 Aug-22-2023, 04:54 PM
Last Post: deanhystad
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,616 Dec-01-2022, 01:52 PM
Last Post: deanhystad
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,773 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  Creating a TG crypto Bot_ problem when trying to with bot.polling p1ner0 1 1,383 Apr-27-2022, 03:43 AM
Last Post: p1ner0
  how to manage crypto trading flooding data from exchange servers Mikeardy 0 1,209 Dec-26-2021, 08:31 PM
Last Post: Mikeardy
  need help one time pad encryption implementation ! nad556 1 1,912 Nov-28-2020, 06:11 PM
Last Post: nad556
  encryption and decryption with python ibrahim 1 1,768 May-16-2020, 03:14 PM
Last Post: Larz60+
  Can't transmit serial fast Python to Arduino pyserial mRKlean 0 2,319 Mar-29-2020, 08:12 PM
Last Post: mRKlean
  Problem with using Crypto library reks2004 2 2,372 Mar-27-2020, 05:38 AM
Last Post: buran

Forum Jump:

User Panel Messages

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