Encryption using password + key
This does not save the password directly in the file but does something similar without any security risk and helps you achieve what you want.
-*- epa-file-encrypt-to: ("[email protected]") -*-
The password is prompted the very first time the file is saved/created. But after that the password is prompted only once each time you open the saved file
The only catch is that you must not lose the keyring file which is saved in
~/.gnupg/ by default.
No setup is needed to be done for this in emacs.
But you do need to have your system environment ready with few libraries for the GPG feature to work.
At the time of setting this up, I had to install the following:
I needed one or two of the above libraries and I ended up installing the others because they were either mandatory or optional dependencies.
Once everything is installed, do
> gpg --gen-key
And generate a never-expiring key for yourself and associate it with your real name and email.
The generated key will be saved in your
Changing the keyring location
You can change the location of keyring by either changing
--keyring options for
Add file to the current list of keyrings. If file begins with a tilde and
a slash, these are replaced by the $HOME directory. If the filename does
not contain a slash, it is assumed to be in the GnuPG home directory
("~/.gnupg" if --homedir or $GNUPGHOME is not used).
Note that this adds a keyring to the current list. If the intent is to use
the specified keyring alone, use --keyring along with --no-default-keyring.
Using GPG with emacs
In emacs, you simply create a file with a
.gpg extension. For example, if the file was originally
auth.el, you would rename it to
Place this line at the top of the file:
;; -*- epa-file-encrypt-to: ("[email protected]") -*-
Note that I have used the elisp comment chars
;; as the example file here is
Use the exact email address you used at the time of key generation.
When you try to save it, emacs will show this prompt in a buffer:
Select recipients for encryption.
If no one is selected, symmetric encryption will be performed.
- `m' to mark a key on the line
- `u' to unmark a key on the line
u <GPG KEY> <YOUR NAME> (<YOUR GPG KEY NAME>) <<YOUR GPG KEY EMAIL>>
Navigate the point to the line containing the key, hit
Navigate the point to the
[OK] button and hit
You can now save the file and kill that file buffer.
Next time when you open that .gpg file, you will be prompted for the password only once and then consecutive saves will be password-prompt-free.