在PHP中通过shell_exec()调用OpenSSL命令

在PHP中通过shell_exec()调用OpenSSL命令,php,encryption,openssl,Php,Encryption,Openssl,当我执行: openssl_decrypt( base64_decode(file_get_contents('/path/to/file')), 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv ); shell_exec('openssl enc -aes-256-cbc -base64 -d -A -p -K ' . $key . ' -iv ' . $iv . ' -in /path/to/file -out

当我执行:

openssl_decrypt(
    base64_decode(file_get_contents('/path/to/file')),
    'aes-256-cbc',
    $key,
    OPENSSL_RAW_DATA,
    $iv
);
shell_exec('openssl enc -aes-256-cbc -base64 -d -A -p -K ' . $key . ' -iv ' . $iv . ' -in /path/to/file -out /path/to/dest');
OpenSSL完美地解密了我的文件。但是,当我执行时:

openssl_decrypt(
    base64_decode(file_get_contents('/path/to/file')),
    'aes-256-cbc',
    $key,
    OPENSSL_RAW_DATA,
    $iv
);
shell_exec('openssl enc -aes-256-cbc -base64 -d -A -p -K ' . $key . ' -iv ' . $iv . ' -in /path/to/file -out /path/to/dest');
未创建目标文件

有人知道会出什么问题吗?我的客户希望能够上传高达2GB的大文件,而将这么多数据加载到PHP变量似乎是一个非常糟糕的主意

编辑:

有了bin2hex,我似乎有了一个理智的命令:

openssl enc  -aes-256-cbc -base64 -d -A -p -K 64343438343165333635663434663262633036636235656462383238356239303763373365353633 -iv abdd099c7bac8b514089d8c901c8395c -in /usr/www/vault/new/d71fd708181573c5f92c8f500ddcb399/787 -out /tmp/decrypted/57574484b684c
但我得到的是:

openssl enc  -aes-256-cbc -base64 -d -A -p -K M�>VO���[ދ��  �7^6 -iv ⬧⬧⬧⬧⬧⬧⬧ -in /usr/www/vault/new/d71fd708181573c5f92c8f500ddcb399/787 -out /tmp/decrypted57574484b684c

可能需要对参数进行编码,以便shell能够实际执行命令:

shell_exec('openssl enc -aes-256-cbc -base64 -d -A -p -K '
 . escapeshellarg($key) . ' -iv ' . escapeshellarg($iv)
 . ' -in /path/to/file -out /path/to/dest');
如果文件名包含空格等,则同样适用于文件名

编辑:实际上Artjom B.是对的:openssl说:-K/-iv key/iv in hex是下一个参数。因此,您需要对其进行十六进制编码:

shell_exec('openssl enc -aes-256-cbc -base64 -d -A -p -K '
 . bin2hex($key) . ' -iv ' . bin2hex($iv)
 . ' -in /path/to/file -out /path/to/dest');

您的web用户是否具有运行该文件的权限?是的,权限是正确的,因为php openssl_decrypt命令将使用相同的输入参数完全解密该文件。
$key
$iv
必须对该命令进行十六进制编码。这个问题不是关于如何在php中执行shell命令,这是关于openssl没有正确解密文件。是的,我尝试了十六进制编码我的密钥和iv到十六进制,但它仍然没有输出任何东西。我将继续看它。bin2hex使用“最高半字节优先”编码,这是网络字节顺序。您可以尝试使用pack:
pack(“h*”,$key)