Search code examples
pythonphpaes

How to decrypt AES-CFB retrieved from post variable in php?


I'm trying to send an encrypted text from a python script to a php page. I successfully encrypted it in python, but I'm unable to decipher it in PHP.

Python Code:

from Crypto.Cipher import AES
message="{'platform': 'Linux', 'hostname': 'some-name', 'ram': '8 GB'}"
key = "=e+r28W^8PkyYtwk"
obj = AES.new(key, AES.MODE_CFB, '1101020304050607')
ciphertext = obj.encrypt(str(message))
print(message, "||", key, "||", ciphertext)

url = 'https://ebenezer-isaac.com/indexer.php'
myobj = {'message': ciphertext}
x = requests.post(url, data = myobj)
print("")
print(x.text)

PHP Code I have: I got the code for PHP decryption from here

<?php
echo "Hello || ";
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
function decrypt_openssl($key, $str) {
  $iv = "1101020304050607";
  return openssl_decrypt($str, 'AES-256-CFB', $key, OPENSSL_ZERO_PADDING, $iv);
}
$encrypted = $_POST["message"];
echo "Encrypted : ".$encrypted." || ";
$decrypted = decrypt_openssl("=e+r28W^8PkyYtwk",$encrypted);
echo "Decrypted : ".$decrypted;
?>

Current Output of Python :

{'platform': 'Linux', 'hostname': 'some-name', 'ram': '8 GB'} || =e+r28W^8PkyYtwk || b'*T\xbd\x9e\xfe\xfa#\x9b\x8a-\xc1\xd3W\x96\xc0\x93\xa7\x99\xceS1\xe1q\x13\xc8j~n\xe1\x97\xb6\xef\x93\xa87\xa9\xe0?\x1f\xe4\x99\xf6\xe8\xfd\xc1q\x13\xe07uV\xb1gu\xa1V\xd2\xd7}\xb4l'

Hello || Encrypted : *T����#��-��W������S1�q�j~nᗶ7��?�����q�7uV�gu�V��}�l || Decrypted : �5��f0�?w

Expected Output of Python :

{'platform': 'Linux', 'hostname': 'some-name', 'ram': '8 GB'} || =e+r28W^8PkyYtwk || b'*T\xbd\x9e\xfe\xfa#\x9b\x8a-\xc1\xd3W\x96\xc0\x93\xa7\x99\xceS1\xe1q\x13\xc8j~n\xe1\x97\xb6\xef\x93\xa87\xa9\xe0?\x1f\xe4\x99\xf6\xe8\xfd\xc1q\x13\xe07uV\xb1gu\xa1V\xd2\xd7}\xb4l'

Hello || Encrypted : *T����#��-��W������S1�q�j~nᗶ7��?�����q�7uV�gu�V��}�l || Decrypted : {'platform': 'Linux', 'hostname': 'some-name', 'ram': '8 GB'}

Solution

  • Thanks to @Sammitch, with a few tweeks, I finally did it.

    The Python code which encrypts data:

    def my_encrypt(data):
        encryption_key = base64.b64decode('c7e1wJFz+PBwQix80D1MbIwwOmOceZOzFGoidzDkF5g=')
        bs = AES.block_size
        cipher = AES.new(encryption_key, AES.MODE_CFB, "1101020304050607")
        encrypted = cipher.encrypt(data)
        return base64.b64encode(encrypted)  
    
    message="{'platform': 'Linux', 'hostname': 'some-name', 'ram': '8 GB'}"
    print("Actual string: " ,str(message))
    data_encrypted = my_encrypt(str(message))
    print("")
    print(data_encrypted)
    url = 'https://ebenezer-isaac.com/indexer.php'
    myobj = {'message': data_encrypted}
    x = requests.post(url, data = myobj)
    print("")
    print(x.text)
    

    PHP Code which decrypts the data:

    <?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    $encryption_key = base64_decode('c7e1wJFz+PBwQix80D1MbIwwOmOceZOzFGoidzDkF5g=');
    $data_encrypted = $_POST["message"];
    $data_decrypted = openssl_decrypt($data_encrypted, 'aes-256-cfb8', $encryption_key, OPENSSL_ZERO_PADDING, "1101020304050607");
    echo "Decrypted string: ". $data_decrypted;
    

    OUTPUT of python :

    Actual string:  {'platform': 'Linux', 'hostname': 'some-name', 'ram': '8 GB'}
    
    b'7p3rqnEEuGp2mVj4gSHTccEK/1FUMnQDrwSHEd9Kv504NoLlv72mdxT6VcaKxA8JFUnbS75qSjyLWBFSjw=='
    
    Decrypted string: {'platform': 'Linux', 'hostname': 'some-name', 'ram': '8 GB'}