reddit / emacs
Unable to decrypt Org files on Windows.
eis3nheim · 2022-11-14 10:50
I downloaded [GnuPG](https://gnupg.org/download/index.html) for **Windows** and installed it and tried to **encrypt** a test Org file with it, and it prompted me with a passphrase and the **encryption has been done**
But when I try to decrypt it and run `org-decrypt-entry` I am **not prompted with anything** and nothing changes.
**This is the configuration I use.**
(require 'org-crypt)
(org-crypt-use-before-save-magic)
(setq org-tags-exclude-from-inheritance (quote ("crypt")))
(setq org-crypt-key nil)
**Edit:** This is the guide I followed, [Configuring Emacs for (symmetric) Org Mode Encryption](https://fluca1978.github.io/2021/09/16/Emacs_Org_Encrypt.html).
oldprogrammer · 2022-11-14 11:59
The link you posted indicates they pressed C-x C-s to show a popup window asking for the passphrase. Looks like you have to do that before org-decrypt-entry will work.
If that isn't working for you, did you maybe rebind that keystroke combination to something else?
eis3nheim · 2022-11-14 13:35
The encryption is working as it should, my issue is with the decryption, I can't decrypt any encrypted Org file.
I initiate the `org-decrypt-entry` with no effect.
oldprogrammer · 2022-11-14 13:51
What error is showing up in the message buffer?
eis3nheim · 2022-11-14 14:07
There is no error in the message buffer, when I invoke `org-decrypt-entry` nothing is happening as if I hadn't even called it.
Could you view the following [demonstration](https://imgur.com/a/fclcRVI)?
oldprogrammer · 2022-11-14 15:26
So I went and followed the setup you linked initially, and then duplicated your situation and for me it worked. When I put the cursor in the encrypted block and executed the decrypt function it worked. No messages in any buffers I could see though.
oldprogrammer · 2022-11-14 16:50
Ok I think I figured out what is going on. When I tested everything it worked just fine for me, but then I made one modification to my data and it stops working exactly like yours, I added the \^M to the end of each line and it stops working. I'm using Windows but my buffers are setup not to insert the \^M via the coding system.
With the \^M showing up at the end of the preamble line
-----BEGIN PGP MESSAGE-----^M
org-crypt doesn't recognize that section as being an encrypted section. Inside the org-crypt.el file there is a function called **org-at-encrypted-entry-p** and it is doing an exact string match for that line but without the \^M. The function allows for leading tabs, but not for a carriage return.
So, because of the \^M the line no longer matches and thus org-crypt doesn't think that starts an ecrypted block.
eis3nheim · 2022-11-14 17:33
That is really amazing, thank you so much, I really appreciate it.
Do you have any idea why is there an extra character being appended?
I don't mean to be lazy, I will search for it.
Thank you again.
oldprogrammer · 2022-11-14 18:00
Hard to say, normally Emacs should properly handle that it is on Windows and not consider the carriage return even if present. Usually this happens when there some type of mixing of encoding in the file. You might look in your Emacs configs to see if any type of file encoding setting was present.
Another thought, could have something to do with the program that is doing the encryption. The Emacs code executes an external program to actually perform the encryption by running a shell command. It then captures the output of that shell command which should be the encrypted block. It is possible either the program it is using is generating the carriage returns or the shell that Emacs is configured to use is causing it.
DrSpotter · 2022-11-14 21:06
I've this trick in my init file as I was seeing the same thing. Never worked out why it happened, but this was enough to stop it being a pain for me:
(defun my/fix-org-crypt-windows-line-encodings ()
"Remove bogus ^M characters after encrypting entries on Windows."
(interactive)
(save-excursion
(save-restriction
(org-narrow-to-subtree)
(goto-char (point-min))
(while (search-forward "\x0d" nil t)
(replace-match "")))))
;; We only need to do this on Windows.
(when mswindows-p
(advice-add 'org-encrypt-entry :after 'my/fix-org-crypt-windows-line-encodings))
This just removes those bogus carriage-returns, if any exist, immediately after an entry is encrypted.
eis3nheim · 2022-11-15 03:04
Thank you again, I really appreciate it.
eis3nheim · 2022-11-15 03:04
Thank you.
oldprogrammer · 2022-11-15 11:20
Glad to help
oldprogrammer · 2022-11-15 11:20
Nice.