2

I need to encrypt a text in php and decrypt it in python.

This is my php code (a standard sample):

$key = "df811af36fcafd6dbd092955aa6fb15b";
$string = 'Hello World ++++';
$iv='1234567812345678';
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $string, MCRYPT_MODE_CBC, $iv));
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0");
var_dump($encrypted);
var_dump($decrypted);

Result is:

string(44) "scPCUNHEMHZK5BoXbOixX6DUpo9Z5+uvmW4lW0bnpSY="
string(16) "Hello World ++++"

Then I try in python:

import base64
from Crypto.Cipher import AES

ciphertext=base64.decodestring("scPCUNHEMHZK5BoXbOixX6DUpo9Z5+uvmW4lW0bnpSY=")
key="df811af36fcafd6dbd092955aa6fb15b"
iv='1234567812345678'
cipher=AES.new(key, AES.MODE_CBC, iv)
cipher.decrypt(ciphertext)

But result is:

'\xe6I\x7fb\xe3{\xcf\x02\xcf^\x06\x06B`\xfd\xc71\x8d\x1f\xb5f.>K\xfe/\xdf\xdf\xb0\x96\x9b^'

I found these python and php modules. another implementation in pure python and php.

http://wiki.birth-online.de/snippets/python/aes-rijndael
http://wiki.birth-online.de/snippets/php/aes-rijndael

These two modules are works together without problem, but I need to use standard/famous modules in php and python. (mcrypt in php and Crypto in python)


I just changed MCRYPT_RIJNDAEL_256 to MCRYPT_RIJNDAEL_128 in my php code and everything looked fine.

But how can I have 256 bit key?

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
iman
  • 21,202
  • 8
  • 32
  • 31
  • Looks like your answer may be here: http://stackoverflow.com/questions/8217269/decrypting-strings-in-python-that-were-encrypted-with-mcrypt-rijndael-256-in-php – sberry Feb 19 '13 at 01:07
  • Use this [perl module](http://search.cpan.org/~delta/Crypt-Rijndael_PP-0.04/Rijndael_PP.pm) to use 256bit key ... – HamZa Feb 19 '13 at 01:19
  • Thanks @sberry but I need a solution with Crypto package. – iman Feb 19 '13 at 01:28

0 Answers0