I want to encrypt a file without being asked for a password, by specifying the password in the code.

I tried

(setq my-context (epg-make-context))
 my-context (lambda (&rest _ignore) "my-password"))
(epg-encrypt-file my-context "file.txt" nil "file.bin")

and I was still asked for a password.

PS. Please do not tell me that this is not secure (I know) and that I should use gnupg-agent (I already do).

1 Answer 1


Here is what I ended up using:

(defun sds-password ()
  "Return a consistent password for 'sds-encrypt' & 'sds-decrypt'.
Should not be a variable for security reasons."
  (concat "pass:" (auth-source-pick-first-password :host "whatever")))
(defun sds-encrypt (fname encrypted)
  "Encrypt FNAME into ENCRYPTED, cf 'sds-decrypt'."
  (call-process "openssl" nil nil nil "enc" "-pass" (sds-password)
                "-aes-256-cbc" "-salt" "-in" fname "-out" encrypted)
  (set-file-times encrypted (file-attribute-modification-time (file-attributes fname))))
(defun sds-decrypt (fname encrypted)
  "Decrypt ENCRYPTED into FNAME, cf 'sds-encrypt'."
  (call-process "openssl" nil nil nil "enc" "-d" "-pass" (sds-password)
                "-aes-256-cbc" "-salt" "-in" encrypted "-out" fname)
  (set-file-times fname (file-attribute-modification-time (file-attributes encrypted))))

This is not exactly what I asked for, but good enough for me for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.