openssl_error_string() for PHP 在 Windows 开发环境中不起作用

如何解决openssl_error_string() for PHP 在 Windows 开发环境中不起作用

如果我在 MAC OSX 开发环境中使用 openssl_encrypt 加密某些内容,则无法在我的 Windows 开发环境中对其进行解密。

  • 我的 Mac 开发环境将 MAMP 用于运行 PHP 的 OSX 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,$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,$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,$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,$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'

解决方法

@Sammitch 给了我这个问题的正确答案。我需要将我的 Key 和 IV 放在引号中。所以key和IV的正确代码如下:

$cipher = 'AES-128-CBC';
$key = '1234567890123456';
$iv = '1234567890123456';
$plaintext = '1234';

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?