GPG 用密码加密文件后,emacs 每次解密都需要输入密码。

gpg-agent.conf 已经设置了保持的时间长短:

default-cache-ttl 3600
default-cache-ttl-ssh 3600
pinentry-program /usr/bin/pinentry-tty
pinentry-timeout 3600

在终端里尝试 gpg2 并不需要每次都输密码,而且在终端里解密过后,emacs 也免密。但如果不在终端里解密,直接用 emacs 打开,每次都要输密码。Emacs 调用 gpg2 的命令类似如下命令:

gpg2 --no-tty --status-fd 1 --yes --enable-progress-filter --command-fd 0 --output /dev/stdout --pinentry-mode loopback --decrypt -- foo.gpg

这个命令在终端里每次也都需要输入密码,大家有什么解决方案没有?

pinentry program是不是需要换成gui版本的?

我用的 swaywm,那几个 gui 的没启动起来。

我在 Linux, macOS 和 Windows 下都是用 GUI 的Pinentry 输入密码,只要输入一次就可以了。

请问,你试过 emacs 调用 gpg2 的那个命令吗?我主贴里写清楚了的终端里没问题。

如果是从终端启动的Emacs也一样吗?

没留意过,我很少用终端的 Emacs,都是启动 GUI Emacs。 最开始配置 gpg时,好像 pinentry-tty 确实是有问题的,后来我改成用 gui 那个pinentry 就都好了。

在 Linux 下,我的 gpg-agent.conf 设置如下:

default-cache-ttl 3600
pinentry-program /usr/bin/pinentry-gnome3

你可以看看你的系统有几个 pinentry,换一个能用的:

ls /usr/bin/ | grep pinentry

Related and not answered:

https://unix.stackexchange.com/questions/776916/gpg-pinentry-mode-loopback-used-to-ask-passphrase-once-but-now-it-asks-alway

另外我发现设置 epa-file-cache-passphrase-for-symmetric-encryption 对同一个 emacs 进程有效,另起一个 emacs 仍需重新输密码。我的最终目的是在使用 emacs-async 的时候少输密码,所以这个方案对这种场景不管用。我目前的想法是在 decrypt 时加一个 advice 起一个终端来让 gpg 输密码。

emacs 支持用 emacs 自身作为 pinentry。

配置 epg-pinentry-mod 变量为 loopback。

具体的可以看官方文档

However, these are considered insecure or semi-obsolete and might not be supported by your operating system or distribution.

前提条件你忘记看了。。。不安全的是这些,不是 epg-pientry-mode

There are other options available to use Emacs as Pinentry, you might come across a Pinentry called pinentry-emacs or gpg-agent option allow-emacs-pinentry.

1 个赞

这玩意儿不就是打开 .gpg 文件时,emacs prompt 密码用的吗?当然设了的呀?不然后面调用 gpg2 的命令里的 loopback 参数怎么来的?

三个问号不要误以为我回复有情绪哈。

gui pientry不能启动肯定是有问题的,如果你的发行版提供了gpg-agent.service,可以试试用systemd启动gpg-agent,应该是wayland的一些环境变量没有设置对导致启动找不到显示器,我个人经验,gui pinentry启动之后整个桌面环境都有效,tty不一定。

我不是说过了:

pinentry-tty 有效的。

在我上面放的链接里:

In short (Edit 1): How to make gpg -qd --pinentry-mode loopback out.gpg cache the passphrase for a period of time in the cli. It used to cache but now it doesn’t.

So pinentry-qt correctly caches the passphrase but any subsequent--pinentry-mode loopback command requires a passphrase input with pinentry-qt in order not ask for the passphrase again.

所以怎么解决这个问题?

好吧我现在能想到的只有gnome-keyring没有设置好这一个原因,首先loopback是不cache密码的,Windows和Linux我都试过,只用loopback每次都需要密码,但是一旦用别的pinentry输入一次密码,密码就会保存在keyring里面,直到重启电脑之前都有效,如果你想做到开机之后免输入密码,可能需要用非对称密钥加密或者ssh-agent,因为gnome-keyring可以开机之后自动cache这两个东西。

Sway 用户在此

我的配置是这样

(setq epa-file-cache-passphrase-for-symmetric-encryption t
      epg-pinentry-mode 'loopback)    ; use minibuffer instead of popup

然后同一个 emacs session 就没问题

上面也提到了: