Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Linux Web服务器上运行的Windows PC和PHP之间加密/解密openSSL_Php_Encryption_Openssl_Php Openssl - Fatal编程技术网

在Linux Web服务器上运行的Windows PC和PHP之间加密/解密openSSL

在Linux Web服务器上运行的Windows PC和PHP之间加密/解密openSSL,php,encryption,openssl,php-openssl,Php,Encryption,Openssl,Php Openssl,我正在尝试学习如何使用openSSL在Windows64机器(我的PC)和运行在LinuxWeb服务器上的PHP之间执行简单的加密/解密 在我的Windows计算机上,我已经安装了Win64的OpenSSL v1.0.2k,我正在使用下面的命令使用简单密码和位于secretkey.txt中的简单密钥创建加密字符串 enc-aes-256-cbc-A-base64-nopad-nosalt-pass:hello-in secretkey.txt 当我运行上面的命令时,我得到以下字符串: 3WE7c

我正在尝试学习如何使用openSSL在Windows64机器(我的PC)和运行在LinuxWeb服务器上的PHP之间执行简单的加密/解密

在我的Windows计算机上,我已经安装了Win64的OpenSSL v1.0.2k,我正在使用下面的命令使用简单密码和位于secretkey.txt中的简单密钥创建加密字符串

enc-aes-256-cbc-A-base64-nopad-nosalt-pass:hello-in secretkey.txt

当我运行上面的命令时,我得到以下字符串:

3WE7cuBFhuLCn3/ZBnUrBn68nn3tVn0NKKz63B3uvoc=

使用上面的字符串,我希望Linux web服务器上的PHP能够像这样对其进行解密:

 $encrypted = '3WE7cuBFhuLCn3/ZBnUrBn68nn3tVn0NKKz63B3uvoc=';
 $enc = 'aes-256-cbc';
 $password = 'hello';
 $key = openssl_decrypt($encrypted, $enc, $password, OPENSSL_ZERO_PADDING);
 echo $key .' should equal this-1234-is-4567-my-8910-secret';
但是,我得到的是:

9(j)���T]��$�W�文科硕士��s�è��zz�>.(应等于此-1234-is-4567-my-8910-secret

我查看了以下网站并尝试了多个版本,但无法正确解密:



AES需要一个密钥才能工作。这是一个精确长度的字节序列(AES-256为32)。您不能直接使用密码(除非构成密码的字节恰好长度正确)

此外,需要16字节的初始化向量(IV)

OpenSSL
enc
命令从使用内部函数提供的密码中导出要使用的密钥和IV。为了解密使用
enc
加密的文件,您需要复制此函数。算法的详细信息在文档中。在PHP中,它可能如下所示:

$password = 'hello';

$bytes = "";
$last = "";

// 32 bytes key + 16 bytes IV = 48 bytes.
while(strlen($bytes) < 48) {
    $last = md5($last . $password, true);
    $bytes.= $last;
}

// First 32 bytes are the key, next 16 are the IV.
$key = substr($bytes, 0, 32);
$iv = substr($bytes, 32, 16);

不是重复。问题是尝试从命令行(windows)加密并使用php解密。建议的解决方案是在php中加密和解密所有内容。您是否在php代码中对openssl输出进行base64解码?零填充是一个糟糕的主意,通常是PKCS#7(née PKCS#5)使用填充。零填充将不允许加密恰好以零(0x00)字节结尾的二进制数据。
$enc = 'aes-256-cbc';
$result = openssl_decrypt($encrypted, $enc, $key, OPENSSL_ZERO_PADDING, $iv);