如何解密从php中的post变量获取的AES-CFB?

问题描述 投票:1回答:1

我正在尝试将加密的文本从python脚本发送到php页面。我已在python中成功对其进行了加密,但无法在PHP中对其进行解密。

Python代码:

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代码,我有:我从here

获得了用于PHP解密的代码
<?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;
?>

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

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'}
python php aes
1个回答
0
投票

感谢@Sammitch,花了几个星期的时间,我终于做到了。

用于加密数据的Python代码:

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代码:

<?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;

python的输出:

Actual string:  {'platform': 'Linux', 'hostname': 'some-name', 'ram': '8 GB'}

b'7p3rqnEEuGp2mVj4gSHTccEK/1FUMnQDrwSHEd9Kv504NoLlv72mdxT6VcaKxA8JFUnbS75qSjyLWBFSjw=='

Decrypted string: {'platform': 'Linux', 'hostname': 'some-name', 'ram': '8 GB'}
© www.soinside.com 2019 - 2024. All rights reserved.