使用gnupg来加密文件报错

如题,我在我的配置里设置好关于epa-file的选项,

(setq exec-path (list (concat Files "Misc/GnuPG/")))
(custom-set-variables '(epg-gpg-program (concat Files "Misc/GnuPG/gpg2.exe")))
(setq epg-gpg-home-directory (concat Files "Misc/GnuPG/.gnupg"))
(setq epa-file-select-keys 0)
(setq epa-file-encrypt-to nil)
(setq epa-file-cache-passphrase-for-symmetric-encryption t)

gpg2.exe 应该是启作用了,但是创建.gpg文件,它自动加密的时候报错:

Error while encrypting with "e:/All Resources/Emacs/Files/Misc/GnuPG/gpg2.exe":

gpg: keyblock resource ‘e:/All Resources/Emacs/Files/Misc/GnuPG/.gnupg/pubring.gpg': No such file or directory
gpg: can't connect to the agent: IPC connect call failed
gpg: problem with the agent: No agent running
gpg: can't connect to the agent: IPC connect call failed
gpg: problem with the agent: No agent running
gpg: error creating passphrase: Operation cancelled
gpg: symmetric encryption of ’[stdin]' failed: Operation cancelled
can't create `e:/All Resources/Emacs/Files/Misc/GnuPG/.gnupg/random_seed': No such file or directory

这个应该怎么解决啊?

只需要这一项就够了,后面的配置需要了再添加。

发现 macOS 里面没法用图形界面的Emacs来加密。

只要是能单独运行的话,应该就没有问题吧,可能是调用的问题

需要弹出密码窗口的问题。 据说 RMS 都没搞定。 得用一个专门patch过的GPG版本

解决方法:

用 Emacs 自带的 pinentry 包,可以把 Emacs 作为 pin entry 而不用特地装那个特地 patch 过的 GPG 了。

To use, add allow-emacs-pinentry to ~/.gnupg/gpg-agent.conf, and start the server with M-x pinentry-start.

适用 Homebrew 最新的 GPG 版本。

你需要把 gpg-agent 的位置加入到环境变量里。

更新了一下gnupg,发现无法找到gpg-agent,也遇到相同的错误了。

你不需要设置 epg-gpg-program,因为Emacs会优先使用gpg2,然后再使用gpg。这个两个程序没有实质的区别,gpg比gpg2更小,所以gpg更适合资源紧张的机器。

一般这样设置就可以正常工作了。

(setq epa-pinentry-mode 'loopback)
(setq epa-file-select-keys 0)
;; ask encryption password once
(setq epa-file-cache-passphrase-for-symmetric-encryption t)
(epa-file-enable)

这个玩意问题很多,比如如果用emacs打开某个加密过的文件,它会弹出一个gui窗口,但是如果你的工作环境没有gui就会报错,所以需要(setq epa-pinentry-mode 'loopback),这样就可以在emacs内部输入密码了。

其次,如果使用emacs打开了一次加密文件,输入密码,解密文件,那么第二次打开就不需要输入密码了(前提是加上(setq epa-file-cache-passphrase-for-symmetric-encryption t),如果没有这句,每次保存修改后的加密文件都需要重置密码),而且即使重启emacs,也不需要输密码就能自动解密。因为gpg-agent帮你缓存了密码了!所以需要kill这个进程。这个设计实在是蛋疼,因为只要你忘了杀掉这个进程,别人就可以简单的用emacs打开你的加密文件!

所以为了安全,你可能还需要

(add-hook 'kill-emacs-hook (defun personal--kill-gpg-agent ()
                             (shell-command "pkill gpg-agent")))

总结就是,从用户体验来看,这个软件实在太几把难用了。

4 个赞