17

I have an accounts.org.gpg file. I can open it using gpg directly:

$ gpg -d ./accounts.org.gpg 

You need a passphrase to unlock the secret key for
user: "Omair Majid <removed@removed>"
4096-bit RSA key, ID 7CC8AAC6, created 2012-01-18 (main key ID 66484681)

gpg: encrypted with 4096-bit RSA key, ID 7CC8AAC6, created 2012-01-18
    "Omair Majid <removed@removed>"
+TITLE: Accounts
<rest of output removed>

But I can't open the same file using Emacs. Trying to open it in emacs gives me an error:

epa-file--find-file-not-found-function: Opening input file: Decryption failed, No secret key: E295ECEC7CC8AAC6

Is there something I need to configure on the emacs side to make this work?

For some additional background, I created this file on another machine (using the same version of Emacs), on which Emacs could read/write to that file just fine. I then copied over the contents of ~/.gnupg over to this machine. And now gpg can still read it, but Emacs can't.

EDIT: Could this be a gpg1 vs gpg2 difference?

$ gpg2 --decrypt accounts.org.gpg 
gpg: encrypted with RSA key, ID E295ECEC7CC8AAC6
gpg: decryption failed: No secret key

How was the Emacs on the other machine (using same configuration, same version of Emacs (25.1) and same OS (Fedora 24 x86_64, same version of gpg and gpg2 packages)) read from it and write to it. Anyway using,

(setq epg-gpg-program "/usr/bin/gpg")

Still results in the same error.

omajid
  • 508
  • 4
  • 12
  • Have you tried `(setq epg-gpg-program "gpg2")` ? – caisah Oct 16 '16 at 07:11
  • I did. It's the default too. And it doesn't work. Just like the gpg2 command line program doesn't. I am starting to think this is a problem with gpg2 itself, not emacs at all. – omajid Oct 16 '16 at 23:04
  • 3
    @caisah See [Bug #24229](https://debbugs.gnu.org/cgi/bugreport.cgi?bug=24229), `setq` doesn't work for `epg-gpg-program`, you have to customize it. – npostavs Oct 17 '16 at 16:09

1 Answers1

22

You do not say which OS nor version of emacs you have.

I solved yesterday exactly the same problem in OSX and emacs 25.1 using information from this link: http://colinxy.github.io/software-installation/2016/09/24/emacs25-easypg-issue.html

I ran these commands:

brew unlink gnupg2 gpg-agent dirmngr
brew uninstall gnupg2 gpg-agent dirmngr
brew install gnupg21
ln -s /usr/local/bin/gpg2 /usr/local/bin/gpg

and set the pinentry mode in my emacs conf file:

(setf epa-pinentry-mode 'loopback)
npostavs
  • 9,033
  • 1
  • 21
  • 53
Heikki
  • 2,961
  • 11
  • 18
  • Please see my edits for OS and Emacs versions. I definitely don't plan to override the system versions of gpg/gpg2. – omajid Oct 17 '16 at 14:51
  • The standard brew package gnupg has for some time now been version 2.1. Older versions 1.4 and 2.0 that are not fully compatible with it are available as separate packages. – Heikki Jul 10 '17 at 19:20
  • Just a info: I had a similar problem on Linux and this solution fixed my problem as well. – Manoel Vilela May 01 '18 at 15:22
  • 3
    Thanks for pointing out the `(setf epa-pinentry-mode 'loopback)` trick. – phs Mar 11 '19 at 09:26
  • 2
    Adding the `(setf epa-pinentry-mode 'loopback)` line in .emacs was sufficient for my Gnu Emacs 26.2 with gpg 2.2.16 from homebrew. – Dave X Sep 15 '19 at 14:42
  • 1
    Apparently `(setf epa-pinentry-mode 'loopback)` is enough without needing to do all the brew operations. – xji Feb 24 '20 at 13:22
  • `(setf epa-pinentry-mode 'loopback)` is what I was looking for as well. Note that homebrew is just the name of the package manager for some systems. If the GnuPG package (https://gnupg.org) isn't installed on your system, use your favourite package manager to install it instead of homebrew. For me it's portage/emerge on Gentoo Linux. – AstroFloyd Jul 26 '22 at 12:15