Encryption in PHP, decryption in Python

PHP code:

$key = "12345678abcdefgh12345678abcdefgh"; $iv = "12345678abcdefgh"; $plaindata = "This is a test string."; $enc = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaindata, MCRYPT_MODE_CBC, $iv)); echo($enc); 

Result:

 QBN0Yue3D9hBrBuD01n5KWG+lv2doMf97cKm/AeusAI= 

How can this be decrypted in Python?

+4
source share
2 answers

Try something like this (altho I have PyCrypto)

 from Crypto.Cipher import AES import base64 AES.key_size=128 iv="your iv" key="your key" crypt_object=AES.new(key=key,mode=AES.MODE_CBC,IV=iv) decoded=base64.b64decode(plain) # your ecrypted and encoded text goes here decrypted=crypt_object.decrypt(decoded) 

This will lead to decrypted text, but it will be padded with bytes so that it is a multiple of 16.

You should probably select the correct fill pattern and delete it accordingly

+5
source

Read the manual , it is pretty well documented.

 data = base64.b64decode('QBN0Yue3D9hBrBuD01n5KWG+lv2doMf97cKm/AeusAI=') 
-7
source

Source: https://habr.com/ru/post/1411775/


All Articles