gnupg decrypt命令使用带有密码短语的php

gnupg decrypt命令使用带有密码短语的php,php,linux,encryption,server,gnupg,Php,Linux,Encryption,Server,Gnupg,Im使用Gnupg解密文件: gpg --decrypt -o file.xml file.gpg You need a passphrase to unlock the secret key for user: "TEST-COMPANY (DAM Key) <test@test.de>" 4096-bit RSA key, ID 257C2D21, created 2018-04-23 Enter passphrase: 当系统请求相位反馈时,问题出现了 我试过这个: $

Im使用Gnupg解密文件:

gpg --decrypt -o file.xml file.gpg

You need a passphrase to unlock the secret key for
user: "TEST-COMPANY (DAM Key) <test@test.de>"
4096-bit RSA key, ID 257C2D21, created 2018-04-23

Enter passphrase: 
当系统请求相位反馈时,问题出现了

我试过这个:

$command = 'gpg --decrypt -o file.xml file.gpg | [Passphrase]'
但是不起作用

你知道吗


谢谢你

只是添加了OP和@CD001在评论中得出的答案,因为这对我帮助很大(谢谢!),而且似乎是一个常见的问题(密钥是用密码短语生成的,生成新密钥不是一个选项)。在了解到从GNUPG2.1开始,它无法用密码短语生成的密钥解密文件(如注释中所述)之前,我正在竭尽全力尝试使用GnuPG函数进行解密。使用预设密码短语配置gpg代理可能很好,但我更喜欢这里的OP所做的

$encrypted_file = "file.csv.pgp";
$path_to_file = $_SERVER["DOCUMENT_ROOT"]."/dir1/dir2";
$passphrase = "passphrase";
$command = "echo {$passphrase} | gpg --passphrase-fd 0 --batch --yes {$path_to_file}/{$encrypted_file}";
exec($command);
如果成功,解密的文件将位于同一目录中,没有.pgp扩展名。所以要确保它是成功的

$decrypted_file = str_replace(".pgp", "", $encrypted_file );
if (file_exists("{$path_to_file}/{$decrypted_file}")) {
    echo "Successfully decrypted $encrypted_file to $decrypted_file";
}

如果
--passphrase[passphrase]
不起作用,请尝试;您可能还需要
--batch
(防止它等待响应)
gpg--decrypt-o file.xml file.gpg--passphrase[passphrase]
您没有看到我前面评论中的链接吗
echo[Passphrase]| gpg--Passphrase fd 0--batch file.gpg
我意识到它在终端上工作,但在php中不起作用-我们非常接近,你是否缺少
--decrypt
选项
echo[passphrase]| gpg--passphrase fd 0--batch--no tty--yes--decrypt file.gpg
。。。我已经有一段时间没有这样做了,上次是使用gpg和
popen()
Holy hell对服务器上的文件进行加密,这是我使用php尝试的1000个“解决方案”中唯一有效的解决方案。Had还必须执行exec(“gpg--import/path/to/private/key.gpg”)和exec(“gpg--import/path/to/public/key.asc”),我想强调在密码短语上使用escapeshellargs的必要性。谢谢你
$decrypted_file = str_replace(".pgp", "", $encrypted_file );
if (file_exists("{$path_to_file}/{$decrypted_file}")) {
    echo "Successfully decrypted $encrypted_file to $decrypted_file";
}