Editing Encrypted Files in Emacs

Now that Emacs has encryption functions built-in (previously, an external library had to be added), you can use GnuPG to keep all your sensitive data secret. Still, there isn’t an easy way to tell Emacs to treat a whole file as encrypted. So I rewrote mc-gpg-file-mode.el from emacswiki to use the built-in emacs functions. Here it is (non-colorized version on emacswiki):

  (defvar pgg-gpg-user-id "YOUR-ID-HERE")  (autoload 'pgg-make-temp-file "pgg" "PGG")  (autoload 'pgg-gpg-decrypt-region "pgg-gpg" "PGG GnuPG")  (define-generic-mode 'gpg-file-mode    (list ?#)     nil nil    '(".gpg\\'" ".gpg-encrypted\\'")    (list (lambda ()              (add-hook 'before-save-hook                        (lambda ()                           (let ((pgg-output-buffer (current-buffer)))                            (pgg-gpg-encrypt-region (point-min) (point-max)                                                    (list pgg-gpg-user-id))))                        nil t)              (add-hook 'after-save-hook                         (lambda ()                          (let ((pgg-output-buffer (current-buffer)))                            (pgg-gpg-decrypt-region (point-min) (point-max)))                          (set-buffer-modified-p nil)                          (auto-save-mode nil))                        nil t)              (let ((pgg-output-buffer (current-buffer)))                (pgg-gpg-decrypt-region (point-min) (point-max)))              (auto-save-mode nil)              (set-buffer-modified-p nil)))    "Mode for gpg encrypted files")