PHP - 'openssl_decrypt' 函数需要什么?

PHP - What is expected by 'openssl_decrypt' function?

(抱歉,我的母语不是英语)

我用 AES-256-ECB 加密了数据,我使用 'openssl_decrypt' 函数,但我总是收到错误值。

$encrypted = '86090e16c5d950a35efe801f8eb5201b';
$key = 'PasswordExample_PasswordExample_';
$text = openssl_decrypt($encrypted, 'aes-256-ecb', $key);
echo "Result: $text";

我尝试使用 OPENSSL_RAW_DATA 选项,并使用 base64_encode() 传递 $encrypted$key,但没有任何效果。

一个朋友通过软件发给我这个密文,我可以在这个网站上看到答案:http://aes.online-domain-tools.com/ (.:Text example:.),但是我想用 PHP自动化流程。

有人可以帮助我吗?我认为 PHP 网页没有足够的关于此功能的文档。


更新:

这个测试代码显示了我的问题:

<?php

$encrypted_hex = '86090e16c5d950a35efe801f8eb5201b';
$encrypted_str = pack("H*", $encrypted_hex);
$encrypted_64 = base64_encode($encrypted_str);

echo "Encrypted (str): '$encrypted_str'<br>\n";
echo "Encrypted (hex): '$encrypted_hex'<br>\n";
echo "Encrypted (64): '$encrypted_64'<br>\n";
echo "<br>\n";

$key_str = 'PasswordExample_PasswordExample_';
$key_hex = implode(unpack("H*", $key_str));
$key_64 = base64_encode($key_str);

echo "Key (str): '$key_str'<br>\n";
echo "Key (hex): '$key_hex'<br>\n";
echo "Key (64): '$key_64'<br>\n";
echo "<br>\n";

$result_str = openssl_decrypt($encrypted_str, 'aes-256-ecb', $key_str);
$result_hex = openssl_decrypt($encrypted_hex, 'aes-256-ecb', $key_hex);
$result_64 = openssl_decrypt($encrypted_64, 'aes-256-ecb', $key_64);

echo "Result (str): '$result_str'<br>\n";
echo "Result (hex): '$result_hex'<br>\n";
echo "Result (64): '$result_64'<br>\n";
echo "<br>\n";

生成的HTML为:

加密(str):'��P��^����'
加密(十六进制):'86090e16c5d950a35efe801f8eb5201b'
加密 (64):'hgkOFsXZUKNe/oAfjrUgGw=='

密钥(str):'PasswordExample_PasswordExample_'
密钥(十六进制):'50617373776f72644578616d706c655f50617373776f72644578616d706c655f'
键 (64): 'UGFzc3dvcmRFeGFtcGxlX1Bhc3N3b3JkRXhhbXBsZV8='

结果(str):''
结果(十六进制):''
结果 (64): ''

如果我添加 OPENSSL_RAW_DATA 则没有变化。该问题在 Windows 10 和 Ubuntu 18.04(Apache 2 和 PHP 7)上仍然存在。我的错误在哪里?有人可以给我看一个使用 aes-256-ecb 的例子吗?

解密可以用:

$result = openssl_decrypt(hex2bin($encrypted_hex), 'aes-256-ecb', $key_str, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING);
  • 关于 OPENSSL_ZERO_PADDING-标志:website uses Zero-Byte-padding, openssl_encrypt/decrypt PKCS7-padding. With the OPENSSL_ZERO_PADDING-flag the PKCS7-padding can be deactivated. Note that the OPENSSL_ZERO_PADDING-flag disables padding, it doesn't mean that Zero-Byte-padding is used (the name of the flag is somehow confusing), see openssl_encryptopenssl 在 mailismagic dot com 的评论。 Zero-Byte-padding 不被 openssl_encrypt/decrypt 直接支持,即 Zero-Byte-padding 必须由用户实现。由于只在 PHP-side 上进行解密,因此只有 un-padding(即最后删除 zero-values)必须手动完成(目前 [=49= 中缺少) ]).对于当前的明文(.:Text example:.),没有注意到丢失的un-padding,因为明文恰好是一个块(16字节)长,所以在加密过程中不会发生Zero-Byte-padding (与附加完整块的 PKCS7 填充相反)。

  • 关于 OPENSSL_RAW_DATA-flag:密文不是 Base64 编码的,因此必须设置 OPENSSL_RAW_DATA-flag。或者:

    $result = openssl_decrypt(base64_encode(hex2bin($encrypted_hex)), 'aes-256-ecb', $key_str, OPENSSL_ZERO_PADDING);
    

    可用于解密

  • 关于第一个参数:由于密文存储为hex-string,因此必须首先对其进行解码(hex2bin($encrypted_hex)pack("H*", $encrypted_hex)),如前所述评论。