【发布时间】:2020-05-06 13:33:45
【问题描述】:
我正在尝试将加密文本从 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创建的base64编码的加密字符串,以便我们调试吗?
-
请发布一些有效示例代码,以及它的输出。您的 python 缺少
AES的导入 [我假设from Crypto.Cipher import AES] 和密钥password导致ValueError: AES key must be either 16, 24, or 32 bytes long。 -
@JohnConde 我刚刚按照您的要求更新了问题。我包含了我尝试过的确切代码以及 shell 的确切输出。
-
@Sammitch 是的,我现在已将导入包含在问题中。我在这里更改密码只是为了表示目的。我已经发布了我现在尝试加密的密码。
-
好吧,至少其中一部分可能是 PHP 和 Python 之间的 CFB 段大小不匹配,详见:stackoverflow.com/questions/46346371/… 尽管我似乎无法弄清楚如何让 PHP 和 Python 同意,即使这个例子的好处。