mistvan Posted October 5, 2021 Share Posted October 5, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/313868-php-and-python-aes-ecb-question/ Share on other sites More sharing options...
requinix Posted October 5, 2021 Share Posted October 5, 2021 If you're having problems with some code then you should probably post the code. Quote Link to comment https://forums.phpfreaks.com/topic/313868-php-and-python-aes-ecb-question/#findComment-1590690 Share on other sites More sharing options...
mistvan Posted October 6, 2021 Author Share Posted October 6, 2021 I finally got it, it generates almost the same: bin2hex(openssl_encrypt(openssl_digest($bemenet,'md5',true), 'aes-256-ecb', $key, OPENSSL_RAW_DATA)); Quote Link to comment https://forums.phpfreaks.com/topic/313868-php-and-python-aes-ecb-question/#findComment-1590719 Share on other sites More sharing options...
gizmola Posted October 6, 2021 Share Posted October 6, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313868-php-and-python-aes-ecb-question/#findComment-1590737 Share on other sites More sharing options...
mistvan Posted October 7, 2021 Author Share Posted October 7, 2021 Thanks, heylify was just a typo, the code was working. Unfortunately I can't modify the python script, because it's not mine and running on a remote server. Thanks for your reply, István Quote Link to comment https://forums.phpfreaks.com/topic/313868-php-and-python-aes-ecb-question/#findComment-1590752 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.