Encryption GPG将没有内容的文件/解密为空文件

Encryption GPG将没有内容的文件/解密为空文件,encryption,gnupg,shell-exec,Encryption,Gnupg,Shell Exec,我为一个gpg问题挣扎了几天,自己也找不出解决办法。如果您能帮助我解决以下问题,我将非常高兴: 我需要用php解密一个gpg文件。为此,我使用以下命令: cat passphrase.txt | /usr/local/bin/gpg --decrypt --passphrase-fd 0 stammdaten.txt.gpg>stammdaten.txt the passphrase.txt contains the password for decryption stammdaten.

我为一个gpg问题挣扎了几天,自己也找不出解决办法。如果您能帮助我解决以下问题,我将非常高兴:

我需要用php解密一个gpg文件。为此,我使用以下命令:

cat passphrase.txt | /usr/local/bin/gpg --decrypt --passphrase-fd 0 stammdaten.txt.gpg>stammdaten.txt

the passphrase.txt contains the password for decryption
stammdaten.txt.gpg is the encrypted file
the decrypted data will be written in stammdaten.txt

when i run this command in php:


shell_exec=("cat passphrase.txt | /usr/local/bin/gpg --decrypt --passphrase-fd 0 stammdaten.txt.gpg>stammdaten.txt")


i get a zero-byte output file (stammdaten.txt) with owner=ftpadmin and group=psacln

but when i execute the same command via ssh terminal (as root), the data will be decrypted and written correctly with file owner=root and group=root.
我认为,这是一个许可问题。如何在php中正确使用该命令?我还试图用解密文件中的ftprightson来chown和chgrp,但似乎没有任何帮助

每一个答案都是高度赞赏的。谢谢

您可以尝试使用

cat passphrase.txt | /usr/local/bin/gpg --output stammdaten.txt --decrypt --passphrase-fd 0 stammdaten.txt.gpg
相反

您可以尝试的另一件事是在shell中以ftpadmin的形式在stammdaten.txt文件所在的目录中运行此命令,以确保它不是文件权限问题

su ftpadmin
cat passphrase.txt | /usr/local/bin/gpg --output stammdaten.txt --decrypt --passphrase-fd 0 stammdaten.txt.gpg

最后我让它开始工作:

首先,我更改了用于解密的gpg命令,将密码短语回显为stdin:

$passphrase = utf8_decode('mypassphrase');
$encrypted = 'fullsystempathtogpgfile.gpg';

"echo '$passphrase' | /usr/local/bin/gpg -v -v --batch --passphrase-fd 0 --no-default-keyring $encrypted";
在使用shell_exec执行之前,我需要更改gpg的homedir:

在设置之前:

putenv("GNUPGHOME=/var/www/.gnupg");
但是很明显,php用户(在我的例子中是“ftpadmin”,用“whoami”发现)没有访问该目录的权限,因此我将.gpg文件夹复制到我新创建的php用户文件夹:/home/ftpadmin(使用777个perms)中,并更改了GNUPGHOME:

putenv("GNUPGHOME=/home/ftpadmin/.gnupg");

现在我可以用php解密gpg文件了。也许你能为你的类似问题找到一些帮助。再次感谢您的回答。

如果我尝试您建议的命令,我会得到gpg:Invalid option“-passphrase-fd0”,如果我尝试“su-ftpadmin”,什么都不会发生,我仍然以root用户身份登录。但当我“su www data”时,我会切换并执行我的初始命令。文件所有者和组为“www数据”,文件写入成功。是否可以将我的命令作为www数据执行?无效选项问题是我的错误…该命令在shell中工作,但在php的shell_exec中不工作。