【问题标题】:How to decrypt AES-CFB retrieved from post variable in php?如何解密从 php 中的 post 变量中检索到的 AES-CFB?
【发布时间】: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 同意,即使这个例子的好处。

标签: python php aes


【解决方案1】:

感谢@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'}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-12
    • 2021-12-20
    • 2018-08-02
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    相关资源
    最近更新 更多