Encrypt in PHP and decrypt in Python or openssl command line
<p>A lot of data in a database I took over contains encrypted fields. The method used to encrypt the data is the following PHP code:</p>

<pre><code>&lt;?php
$text = "test 1234\ntest 2345\ntest 3456\ntest 4567";
$key = "0123456789abcdefghijklmnopqrstuv";
$enc = openssl_encrypt($text, "AES-256-CBC", $key);
echo "Raw: " . $text . "\n";
echo "Key: " . $key . "\n";
echo "Key (Hex) " . bin2hex($key) . "\n";
echo $enc;
echo "\n";
?&gt;
</code></pre>

<p>When I run the code I get the following output including a warning about the empty initialization vector (iv), which I have to ignore because the whole DB data is encrypted this way (I perfectly know this should not be done this way).</p>

<pre><code>Warning: openssl_encrypt(): Using an empty Initialization Vector (iv) is potentially insecure and not recommended in /tmp/cp3_encdec/enc2.php on line 5
Raw: test 1234
test 2345
test 3456
test 4567
Key: 0123456789abcdefghijklmnopqrstuv
Key (Hex) 303132333435363738396162636465666768696a6b6c6d6e6f70717273747576
uPNXdo2K0Gvy/+MW0YFR7utFsrNDAp8yYaDxT352W3lPKNOkNMg+l3eFKEi0zeze
</code></pre>

<p>Decrypt using php <code>openssl_decrypt($encrypted, "AES-256-CBC", $key)</code> gives me full output. Php manual does not give a lot of insight what is used to encrypt in regards to padding and iv when leaving those values empty.</p>

<p>Next I tried to decrypt on the command line using the openssl command:</p>

<pre><code>echo "uPNXdo2K0Gvy/+MW0YFR7utFsrNDAp8yYaDxT352W3lPKNOkNMg+l3eFKEi0zeze" | openssl aes-256-cbc -d -a -K 303132333435363738396162636465666768696a6b6c6d6e6f70717273747576 -iv 0
</code></pre>

<p>which works fine and returns the initial input:</p>

<pre><code>test 1234
test 2345
test 3456
test 4567
</code></pre>

<p>Trying to decrypt in Python using the following code results in wrong decryption:</p>

<pre><code>import base64
from Crypto.Cipher import AES 

PAD = u'\0000' 

def decrypt(enc, key):
    decobj = AES.new(key, AES.MODE_ECB)
    data = decobj.decrypt(base64.b64decode(enc))
    data = data.rstrip(PAD.encode())
    print(str(data))

key = "0123456789abcdefghijklmnopqrstuv"
decrypt("uPNXdo2K0Gvy/+MW0YFR7utFsrNDAp8yYaDxT352W3lPKNOkNMg+l3eFKEi0zeze", key)
</code></pre>

<p>Result, first 16 bytes are readable but not the rest:</p>

<pre><code>b'test 1234\ntest 2\x8b\xc7b|\xf9\xef\xa3\x1f\xd2\xcc\xd7#\xe7\x8b%\x8b\x981\x92\x87v4\xa8;h\xa9\xf8Fw\x7fRp'
</code></pre>

<p>Modifying my input to contain more data will also break decryption using the openssl command:</p>

<pre><code>Raw: [system] test:1234
[system] test:2345
[database] test:3456
[unknown] test:4567

Key: 0123456789abcdefghijklmnopqrstuv
Key (Hex) 303132333435363738396162636465666768696a6b6c6d6e6f70717273747576
9KWsGGLa1/g3f36kUJJ/oHNiEnIDorZULwR8pXZHwJhul2XsdZLwLN8jMptP9fcWgY42oTq7RTm+/8CKPiGFPWrY/3neLvf8UNedsVuKRlc=
</code></pre>

<p>Openssl command line:</p>

<pre><code>echo "9KWsGGLa1/g3f36kUJJ/oHNiEnIDorZULwR8pXZHwJhul2XsdZLwLN8jMptP9fcWgY42oTq7RTm+/8CKPiGFPWrY/3neLvf8UNedsVuKRlc=" | openssl aes-256-cbc -d -a -K 303132333435363738396162636465666768696a6b6c6d6e6f70717273747576 -iv 0
bad decrypt
15143:error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length:/BuildRoot/Library/Caches/com.apple.xbs/Sources/OpenSSL098/OpenSSL098-64/src/crypto/evp/evp_enc.c:323:
</code></pre>

<p>The same using Python code from above will result in first 16 bytes readable, but not the rest:</p>

<pre><code>b'[system] test:12\xc7\x91\xa6C\x11\xa3\xa4\x8cR\x12#\x84$\xf7\x0c\xd4IP!F6\xa8\xed0Np\x1d\xc7\x174\xa5\xc5N\xe3\x00\x9f\x01\xa8\xc3\x18\xea\x158\xc0:\x9b\x9cx\xee\xf9X\xfc\x1a\xcf J\xca\xc5\xf4\xbf\x08\x16\x8f&lt;'
</code></pre>

<p>Again if using php openssl_decrypt works:</p>

<pre><code>&lt;?php
$text = "9KWsGGLa1/g3f36kUJJ/oHNiEnIDorZULwR8pXZHwJhul2XsdZLwLN8jMptP9fcWgY42oTq7RTm+/8CKPiGFPWrY/3neLvf8UNedsVuKRlc=";
$key = "0123456789abcdefghijklmnopqrstuv";
$dec = openssl_decrypt($text, "AES-256-CBC", $key);
echo $dec;
echo "\n";
?&gt;

[system] test:1234
[system] test:2345
[database] test:3456
[unknown] test:4567
</code></pre>

<p>Someone has an idea how php encrypts the data, I suppose it is a padding problem but am not sure and I am open for any help on this topic.</p>