本文介绍了gnupg用密码用php解密命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Gnupg解密文件:

Im using Gnupg to decrypt a file:

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

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

Enter passphrase:

然后我编写此密码短语,然后可以使用.

Then I write this passphrase and then works.

现在我想在PHP上使用此命令使其自动完成:

And now I want to make it automatic using this command on PHP:

$command = 'gpg --decrypt -o file.xml file.gpg'
exec($command);

系统要求输入短语时出现问题.

The problem came when system ask for phassphrase.

我尝试过:

$command = 'gpg --decrypt -o file.xml file.gpg | [Passphrase]'

但不起作用.

对此有任何想法吗?

谢谢

推荐答案

只需添加OP和@ CD001在评论中指出的答案,因为它极大地帮助了我(谢谢!),而且似乎是一个常见问题(密钥是使用密码生成的,并且不能选择生成新密钥).在得知从GnuPG 2.1开始,它无法使用密码短语生成的密钥解密文件之前,我一直在努力尝试使用GnuPG函数进行解密(如注释).使用预设的密码短语配置gpg-agent可以正常工作,但是我更喜欢这里的OP所做的事情.

Just adding the answer that the OP and @CD001 figured out in the comments, because it helped me immensely (thanks!), and seems like a common issue (secret key was generated with passphrase, and generating new keys isn't an option). I was pulling my hair out trying to decrypt with the GnuPG functions, before learning that as of GnuPG 2.1, it can't decrypt a file with passphrase-generated key (as noted in comment here). Configuring gpg-agent with a preset passphrase may work fine, but I much prefer what the OP here did.

$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扩展名.因此,请确保它成功...

If successful, the decrypted file will be in the same directory, without the .pgp extension. So make sure it was successful...

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

这篇关于gnupg用密码用php解密命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 17:27