简体   繁体   English

如何使用 php cli 解密 .pgp 文件

[英]How to decrypt a .pgp file using php cli

It's the first time I need to do this.这是我第一次需要这样做。

I have to code a script in php to run with cli, the script is passed a .zip file which I have to decompress, the files inside are .pgp files and I need to decrypt these files, I have the following code.我必须在 php 中编写一个脚本以使用 cli 运行,该脚本传递了一个我必须解压缩的 .zip 文件,里面的文件是 .pgp 文件,我需要解密这些文件,我有以下代码。

function decryptPGPFiles($pgpFiles, $passphrase){
  $res = gnupg_init();
  foreach($pgpFiles as $filename){
    gnupg_adddecryptkey($res,"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",$passphrase);
    $cipher = file_get_contents($filename);
    $plain = gnupg_decrypt($res,$cipher);
    var_dump($plain);
    die;
  }
}

Of course the 'var_dump($plain);'当然是'var_dump($plain);' and 'die;'和'死;' are just for testing.仅用于测试。 Anyway the $plain is always false.无论如何 $plain 总是假的。 Assume that $pgpFiles is an array of filenames for the .pgp files in the current dir.假设 $pgpFiles 是当前目录中 .pgp 文件的文件名数组。

I do have a private key with name 'private.gpg' in the current dir and also I know the $passphrase.我在当前目录中有一个名为“private.gpg”的私钥,而且我知道 $passphrase。

I know that the 2nd parameter for the function 'gnupg_adddecryptkey' is a $fingerprint according to php.net, when I run 'gpg --import private.gpg' and then 'gpg --fingerprint' it appears the respective fingerprint and that's the one I'm passing as parameter, anyway the fingerprint appears with several blankspaces and I trim them.我知道根据 php.net,函数 'gnupg_adddecryptkey' 的第二个参数是 $fingerprint,当我运行 'gpg --import private.gpg' 然后运行 ​​'gpg --fingerprint' 时,它出现了各自的指纹,这就是我作为参数传递的一个,无论如何指纹出现了几个空格,我修剪了它们。

I'm sure I'm not doing the right steps, but I can't find a documentation clear enough for me.我确定我没有做正确的步骤,但我找不到对我来说足够清楚的文档。 It is even possible to decrypt a .pgp file from php cli?甚至可以从 php cli 解密 .pgp 文件?

Thanks in advance.提前致谢。

According to a comment on the documentation:根据对文档的评论:

gnupg_decrypt gnupg_decrypt

Mike迈克

As of gnupg version 2, it is not possible to pass a plain password any more.从 gnupg 版本 2 开始,无法再传递普通密码。 The parameter is simply ignored.该参数被简单地忽略。 Instead, a pinentry application will be launched in case of php running in cli mode.相反,如果 php 在 cli 模式下运行,则会启动 pinentry 应用程序。 In cgi or apache mode, opening the key will fail.在 cgi 或 apache 模式下,打开密钥会失败。 The simplest solution is to use keys without passwords.最简单的解决方案是使用没有密码的密钥。

Personally, I probably wouldn't use a key without a password as stated above though.就我个人而言,如上所述,我可能不会使用没有密码的密钥。 I'm still looking for a better solution but something like this has worked for now:我仍在寻找更好的解决方案,但现在这样的事情已经奏效:

shell_exec("gpg --batch --passphrase \"" . $passphrase . "\" -d $file > $decrypted_file_name");

Consider using this with escapeshellarg() or escapeshellcmd() if passing in potentially dangerous variables.如果传入具有潜在危险的变量,请考虑将其与 escapeshellarg() 或 escapeshellcmd() 一起使用。

Have you tried invoking gnupg_geterror() after gnupg_adddecryptkey() ?您是否尝试过在gnupg_geterror()之后调用gnupg_adddecryptkey() I suspect your private key is not actually getting accepted.我怀疑您的私钥实际上并未被接受。 I assume it needs to be in PHP user's GPG keyring (ie whatever or whoever is invoking the script, not necessarily your GPG keyring)?我认为它需要在 PHP 用户的 GPG 密钥环中(即调用脚本的任何人或任何人,不一定是您的 GPG 密钥环)? Also in a couple of brief tests I ran, I kept getting prompted for the passphrase on the terminal, but that could be because of my paranoid gpg config (I disable passphrase caching completely).同样在我运行的几个简短测试中,我一直在终端上提示输入密码,但这可能是因为我偏执的 gpg 配置(我完全禁用了密码缓存)。 That would definitely break things in a non-interactive scenario...这肯定会在非交互式场景中破坏事情......

Another way to trap errors would be to set gnupg_seterrormode() to ERROR_EXCEPTION or similar to see what's actually happening...另一种方法来捕获错误是设置gnupg_seterrormode()ERROR_EXCEPTION或类似的,看看有什么实际发生...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM