简体   繁体   English

无法使用 GnuPG 在 PHP 中加密

[英]Unable to encrypt in PHP using GnuPG

My program is supposed to encrypt a file using GnuPG but I'm unable to make it work.我的程序应该使用 GnuPG 加密文件,但我无法使其工作。 I've tried encrypting the files using command directly on the server and that works.我试过直接在服务器上使用命令加密文件,并且有效。 But when I run my PHP code, the files aren't encrypted.但是当我运行我的 PHP 代码时,文件没有加密。 I've tried encrypt single string and still failed.我试过加密单个字符串,但仍然失败。 Mind to help?介意帮忙吗?

<? php
   
   putenv("GNUPGHOME=/home/admin/.gnupg");

   $gpg=new gnupg();
   $gpg->addencryptkey("CB416DC6B06BF75D1C8DA888A552533206"); //fingerprint
   $enc=$gpg->encrypt("hi");
   echo $enc;

 ?>

I've run the geterror and it return "no key for encryption set".我已经运行了 geterror,它返回“没有加密集的密钥”。 And may I know am I setting the homedir correctly and what is the exact permission for the GnuPG homedir?我是否知道我是否正确设置了 homedir 以及 GnuPG homedir 的确切权限是什么?

Thank you in advance!先感谢您!

Make sure your syntax is correct and that the fingerprint you are using is actually available.确保您的语法正确并且您使用的指纹实际上可用。

Check that you have the key检查您是否拥有钥匙

$ gpg --list-keys 
/home/jaroslav/.gnupg/pubring.kbx
---------------------------------
pub   rsa3072 2021-10-20 [SC] [expires: 2023-10-20]
      C1CFDC84CA8A42DBF03371E75B9ED4CB2025188B
uid           [ultimate] Jaroslav Rakhmatoullin <jazzoslav@gmail.com>
sub   rsa3072 2021-10-20 [E] [expires: 2023-10-20]

Run the example运行示例

$ php gpg.php 
-----BEGIN PGP MESSAGE-----

hQGMAxqCDajFlhqQAQv8DwOnlRKYLBmlS5ItBcPPhz9lV7sBqHRdkzmpNF1fC0NE
uH/6A160IYJ24nA8e5CbnsFReEoohiC03wM4hfGmPQJ0K73aE8jsJU9UyoL8CK+b
l9WubvMK2a4/gHoD837y8U6L72mXvyKvvxW35h7CXLUHOb5R9iNkUBS4YyFbO05p
OETjMwN1nb9s2h/Z/IdKx8YOPieub+RoVs6WijE9BnsAeHRgG7QyrQ97a+xn3/lG
a0g/h65jFGYj0ocQgKANORwlfUEurZ+8tad5c0d+M5y+JZTDsYecKS1lNAQAu0tE
FTp+hbef3Tsec4mG7oJt4tXGxunr+D1hu31lqmo/nhzqIBPiPdVJuMX2vEwBZnYX
GP1c7k8tyhoRWdvKuU9aBoADiPSPY90EppXUBma9Y+X6bOPjsdHM3wxDLQIw7Xir
WByNb6/pxB8efe8nETSwIxcqyr+Lut4eBIk5lk0pnWf3goCawnENrLhsWOGhtN/x
S9wqI+9kC2LO+y3qcQkF0j0Bgq/5hQUWyQwN7mLmDvn3hUuSFs744UmrG+Uz0LqY
S3R3cQmKWgnVWonjV/X9vJ+aLB3VbubIDd0VW9I5
=HGmP
-----END PGP MESSAGE-----

Fix the syntax ( <? php is very wrong)修复语法( <? php非常错误)

$ cat gpg.php 
<?php
putenv("GNUPGHOME=/home/jaroslav/.gnupg");

$gpg=new gnupg();
$gpg->addencryptkey("C1CFDC84CA8A42DBF03371E75B9ED4CB2025188B"); //fingerprint

if ($enc = $gpg->encrypt("hi")) {
    echo $enc;
} else {
    echo $gpg->geterror() . PHP_EOL ;
}

If you are actually running this in a browser / server, then make sure the user who is running the web server can read the .gnupg folder and files in there.如果您实际上是在浏览器/服务器中运行它,请确保运行 Web 服务器的用户可以读取 .gnupg 文件夹和其中的文件。

Ok.好的。 for my user:对于我的用户:

$ namei -l /home/jaroslav/.gnupg/trustdb.gpg 
f: /home/jaroslav/.gnupg/trustdb.gpg
drwxr-xr-x root     root     /
drwxr-xr-x root     root     home
drwxr-xr-x jaroslav jaroslav jaroslav
drwx------ jaroslav jaroslav .gnupg
-rw------- jaroslav jaroslav trustdb.gpg

No bueno for apache user (you will probably have a www-data user) apache 用户没有 bueno(你可能会有一个 www-data 用户)

$ sudo -u apache namei -l /home/jaroslav/.gnupg/trustdb.gpg 
f: /home/jaroslav/.gnupg/trustdb.gpg
drwxr-xr-x root     root     /
drwxr-xr-x root     root     home
drwxr-xr-x jaroslav jaroslav jaroslav
drwx------ jaroslav jaroslav .gnupg
                             trustdb.gpg - Permission denied

If I run the php script as the apache user, I get the same error as you:如果我以 apache 用户身份运行 php 脚本,我会收到与您相同的错误:

$ sudo -u apache php gpg.php 
no key for encryption set

You can work around this issue by allowing the www-data user (or the actual user that runs the web server or the php-fpm process) to access the .gnupg folder.您可以通过允许 www-data 用户(或运行 Web 服务器或 php-fpm 进程的实际用户)访问 .gnupg 文件夹来解决此问题。

setfacl -R -m u:www-data:rwx /home/admin/.gnupg
setfacl -R -m u:apache:rwx /home/admin/.gnupg

You don't need both of the commands, just one.您不需要这两个命令,只需要一个。

This command will allow the www-data user to read everything in the .gnupg folder.此命令将允许 www-data 用户读取 .gnupg 文件夹中的所有内容 This is dangerous and someone could steal your private key.这是危险的,有人可能会窃取您的私钥。

If I were you I would instead create a .gnupg directory for the www-data user instead of using the one belonging to the admin, but that's up to you.如果我是你,我会为 www-data 用户创建一个 .gnupg 目录,而不是使用属于管理员的目录,但这取决于你。 Be careful in your production environment especially if you plan on doing something non-trivial or something involving money.在您的生产环境中要小心,特别是如果您打算做一些重要的事情或涉及金钱的事情。

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

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