【发布时间】:2021-09-19 06:33:29
【问题描述】:
如果我在我的 MAC OSX 开发环境中使用 openssl_encrypt 加密某些内容,我无法在我的 Windows 开发环境中对其进行解密。
- 我的 Mac 开发环境使用 MAMP for OSX 运行 PHP 7.4.2.
- 我的 Windows 开发环境正在使用 MAMP for Windows 运行 PHP 7.4.2。
几点说明:
- 如果我在 Windows 中使用
openssl_encrypt加密,我也可以在 Windows 中解密它。 - 如果我在 Mac 上加密它,我无法在 Windows 中解密它,但我可以在 Mac 中解密它就好了。
- 我在 windows 中得到的错误是
error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt。
我已阅读这篇文章:How to resolve the "EVP_DecryptFInal_ex: bad decrypt" during file decryption
从这篇文章中,我猜我使用的是不兼容的 openssl_decrypt 版本,但我不确定如何解决这个问题,或者这是否是问题所在。
这是我的代码:
<?php
/**
* First, this code works on both Mac and Windows
*/
$cipher = "AES-128-CBC";
$key = 1234567890123456;
$iv = 1234567890123456;
$plaintext = '1234';
$encrypted = openssl_encrypt($plaintext, $cipher, $key, 0, $iv);
if(false === $encrypted)
{
echo openssl_error_string();
die;
}
echo "Plain text: " . $plaintext . "<br>";
echo "Encrypted text: " . $encrypted . "<br><br>";
// on Mac $encrypted = w9oKTqKTtvBuRUVbhQP/qw==
// on Win $encrypted = 19MQn7slHAAdFYR1TJSZxQ==
$decrypted = openssl_decrypt($encrypted, $cipher, $key, 0, $iv);
$result = $decrypted === $plaintext;
echo "Text was encrypted and decrypted on the same system: ";
print $result ? 'It worked<br><br>' : 'It did not work<br><br>';
// output on both Windows and Mac - It worked
/**
* Code below does not work
*/
// This is the encrypted text the Mac produces
$text_encrypted_mac = 'w9oKTqKTtvBuRUVbhQP/qw==';
$decrypted = openssl_decrypt($text_encrypted_mac, $cipher, $key, 0, $iv);
$result = $decrypted === $plaintext;
echo "Start with text encrypted on Mac: ";
print $result ? 'It worked<br>' : 'It did not work<br>';
// output on Mac - 'It worked'
// output on Windows - 'It did not work'
// this is the encrypted text I get on Windows
$text_encrypted_win = '19MQn7slHAAdFYR1TJSZxQ==';
$decrypted = openssl_decrypt($text_encrypted_win, $cipher, $key, 0, $iv);
$result = $decrypted === $plaintext;
echo "Start with text encrypted on Windows: ";
print $result ? 'It worked<br>' : 'It did not work<br>';
// output on Mac - 'It did not work'
// output on Windows - 'It worked'
【问题讨论】:
-
请发布完整且有效的测试数据集。
fake_key不是有效的 AES 密钥(或者您是否使用过此密钥?)。因此,在 Windows 和 MAC 下发布明文、密钥、IV 和最重要的密文。有了这个,它至少可以缩小哪个系统加密不正确。 -
问题已更新,现在密钥为 16 位。感谢您的意见。这并没有改变结果。我仍然遇到同样的问题。
-
@user 9014097 - 我的 Windows 使用的 openssl 版本似乎与我的 Mac 不同,并且与您的机器不同。你知道我如何更改我正在使用的 openssl 版本吗?
-
1. AES 的输出在 OpenSSL 的版本或实现之间不有所不同。 2.你在Mac上得到的密文
w9oKTqKTtvBuRUVbhQP/qw==是正确的。 3. 您的密钥/iv/plaintext 在 Windows 机器上有所不同,或者该机器上的 OpenSSL 出现了严重错误。 4. 键和 IV 是字符串,而不是数字。在它们周围加上一些引号,也许 Windows 版本没有正确处理类型。想想看,如果你还在运行 32 位版本的 PHP,它根本不会喜欢那些 16 位数字。 -
@Sammitch - 谢谢!!!!在我的密钥和 IV 中添加引号就可以了。非常感谢您的帮助。
标签: php encryption openssl mamp php-openssl