HOWTO: Emacs: Encrypted Files
I have deleted The GitHub repositories referred to in this post; see Clearing the deck for details.
This post is part of my HOWTO series on Emacs.
There is a tool called ccrypt that uses Rijndael encryption to encrypt and decrypt files. I use this myself to secure my personal journal, and a file containing useful passwords and the like. Normally, I'd use it like this:
$ echo Hello world! > foo.txt $ ccrypt -e foo.txt Enter encryption key: pass Enter encryption key: (repeat) pass $ cat foo.txt.cpt ��䒨y u��t4@�6Z?UE� ��[~u50Օ���d����� $ ccrypt -d foo.txt.cpt Enter decryption key: pass $ cat foo.txt Hello world!
As you can see, ccrypt -e does the encryption after prompting for a key, renaming foo.txt to foo.txt.cpt in the process. Obviously, ccrypt -d does the decryption. Unfortunately the whole process is a bit convoluted, involving a command prompt and the possibility of forgetting to encrypt the file again afterwards.
Fortunately there's an Emacs plugin for ccrypt that handles all of this for you. Below are instructions on setting this up for Linux and Windows; as usual (nowadays - how things have changed), the Linux installation is quick and easy and the Windows installation awkward.
After the installation instructions, I describe how to use the plugin, and then go on to explain the changes I made to my .emacs file and .emacs.d directory to enable it.
Linux Installation
- First, install ccrypt with:
sudo apt-get install ccrypt
- Next, if you've installed my personal Emacs configuration from GitHub, update it with:
cd ~/duncans_emacs git pull
... and you're good to go.
Go and bask in the sun, drink a beer, reflect on how easy that was ... and consider making a donation to the folks who make your Linux Distro (Ubuntu in my case), or the Free Software Foundation who make Emacs.
Windows Installation
- First, download the Cygwin build of ccrypt from ccrypt-1.7.cygwin-i386.zip.
- Then, unzip that file into your program files folder, so that you have something like
C:\Program Files\ccrypt-1.7.cygwin-i386containing the following:
AUTHORS ccrypt.1 ccrypt.exe ccrypt.html ChangeLog COPYING cygwin1.dll cypfaq01.txt jka-compr-ccrypt.el jka-compr-ccrypt.elc NEWS README README.win
- Edit your PATH environment variable (instructions for Vista and XP) and add the folder you created in Step 2 above, separating it from the others with a semicolon.
You can test that your new environment variable is set correctly by opening a Command Prompt and typing:
ccrypt --version
You should see something like:
ccrypt 1.7. Secure encryption and decryption of files and streams. Copyright (C) 2000-2004 Peter Selinger.
... in response.
- Finally, if you've installed my personal Emacs configuration from GitHub, update it with by running Git Bash and entering:
cd ~/duncans_emacs git pull cd ~ rm -rf .emacs .emacs.d cp -f duncans_emacs/emacs .emacs cp -rf duncans_emacs/emacs.d .emacs.d
... and don't forget to glare at your Linux-using neighbour who's lazing around outside in the sun at this point :-)
Using It
If you save a buffer with a file extension .cpt, Emacs will automatically prompt you for a key and encrypt the buffer with ccrypt when it saves. If you open a file with the extension .cpt, Emacs will prompt you for a password to decrypt the buffer into memory. That's all there is to it.
If you're a real Emacs newbie and you're sitting there thinking "what does he mean by buffer?" then you should read through the Keyboard Basics section of this excellent Emacs tutorial. All will become clear, I promise ... and if it doesn't, please either post your questions in the comments section, or email me.
Details
In order for Emacs to load the ccrypt plugin we need to do two things in the .emacs file: add the path to the plugin to load-path (the list of paths Emacs checks when instructed to load something), and then actually load the plugin.
This line adds to the path:
(add-to-list 'load-path
"~/.emacs.d/ccrypt")And these lines load the plugin (well actually, the first is a comment):
;; allow automagic editing of ccrypt-ed files (require 'jka-compr-ccrypt "jka-compr-ccrypt.el")



Recent comments
1 week 1 day ago
3 weeks 3 days ago
4 weeks 41 min ago
11 weeks 1 day ago
15 weeks 5 days ago
31 weeks 23 hours ago
31 weeks 1 day ago
37 weeks 3 days ago
37 weeks 4 days ago
37 weeks 4 days ago