Jump to content

PHP and Python AES ECB question


Recommended Posts

Hi,

I'm very new to python and I have a question about AES.
I have to use a function in both python and PHP which uses AES/ECB crypto, but couldn't get the equivalent output.
The following python code is my starting point:

 

from Crypto.Cipher import AES
...
  path = text.encode("utf-8")
  h = MD5.new(path)
  path_hash = h.digest()
  aes = AES.new(aes_key, AES.MODE_ECB)
  signing = aes.encrypt(path_hash)
  signing = heylify(signing)
  signing = signing.decode("utf-8")

I tried to get the same result with php openssl_encrypt, but none of the cryptos resulted the same.

Any help would be greatly appreciated.

István 

Link to comment
Share on other sites

14 hours ago, mistvan said:

I finally got it, it generates almost the same:

bin2hex(openssl_encrypt(openssl_digest($bemenet,'md5',true), 'aes-256-ecb', $key, OPENSSL_RAW_DATA));

Is "almost the same" ok?  If this was meant to be a cross platform solution for encrypting/decrypting data between subsystems written in different languages, I would think that you would require "exactly the same".

One thing obviously wrong with your Python is that your code snippet calls the "heylify"  function, when it should have been 

signing = hexlify(signing)

One other important thing to keep in mind is that the length of the AES key needs to match.  You are using a 256 bit key in your PHP snippet, but it is not clear in your python snippet.  You would need to insure that you are using a 32 byte key with your python code.

If the hex version of the ciphertext is exactly the same for each function, then you have success, but if not exactly the same, then there's a mistake.  The real way to test would be to take the exact same plaintext, and key, and encrypt/decrypt both in php and python.  So probably you want to write your own encrypt(), decrypt() functions in both python and php.  Mainly these functions will just have the code snippets you provided here, passing the plaintext and key, or the ciphertext and key for decryption.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.