reddit / emacs
Did you know that Emacs had a built-in Caesar cipher?
scribe36 · 2022-03-17 05:15
Been using 'macs for years. It still continues to pleasantly surprise. I discovered the `message-caesar-buffer-body` which by default shifts forward 13 places. Naturally, I wrote a hydra,
```lisp
(use-package message
:ensure hydra :after hydra
:config
(defhydra hydra-caesar ()
"Caesar Cipher"
("f" (lambda () (interactive) (message-caesar-buffer-body 1)) "Shift Forward")
("b" (lambda () (interactive) (message-caesar-buffer-body -1)) "Shift Backward"))
(hydra-set-property 'hydra-caesar :verbosity 1))
```
Let's see who can decrypt this,
> Dozdbv uhphpehu wkdw wr dujxh, dqg zlq, lv wr euhdn grzq wkh uhdolwb ri wkh shuvrq brx duh dujxlqj djdlqvw. Lw lv sdlqixo wr orvh brxu uhdolwb, vr eh nlqg, hyhq li brx duh uljkw. - Kduxnl Pxudndpl
Now, there there. For the uninitiated, this is ancient encryption breakable by pen and paper in 2 minutes. Note that I did not provide a key to the cipher above and yet people will be coming up with the plaintext. Read more [here](https://en.wikipedia.org/wiki/Caesar_cipher).
holgerschurig · 2022-03-17 07:32
It wasn't good in the times of the Caesars, it isn't good today.
[deleted] · 2022-03-17 07:35
[deleted]
nv-elisp · 2022-03-17 07:42
;;One of the oldest schemes of the world.
;;The beginning becomes the end and the end
;;becomes its own little wonderland.
;;Easy to figure out if you're not hard-boiled.
(defun +shift-downcase-char (char n)
"Return CHAR shifted by N letters."
(+ 97 (mod (- (+ char n) 97) 26)))
(defun +string->downcase-chars (string)
"Return a list of chars from downcased STRING."
(mapcar #'string-to-char (split-string (downcase string) "" 'omit-nulls)))
(defun +decrypt (ciphertext dict)
"Brute force decrypt CIPHERTEXT by comparing it to DICT."
(let ((words (split-string ciphertext " " 'omit-nulls))
word)
(catch 'decrypted
(while (setq word (pop words))
(let* ((letters (+string->downcase-chars word))
(shifts (cl-loop for i from 1 to 25
collect (cons i (mapconcat (lambda (c) (char-to-string (+shift-downcase-char c i))) letters)))))
(when-let ((key (cl-some (lambda (shift) (and (member (cdr shift) dict) (car shift))) shifts)))
(throw 'decrypted
(mapconcat #'char-to-string
(cl-loop for char in (+string->downcase-chars ciphertext)
collect (if (<= 97 char 123) (+shift-downcase-char char key) char)))))))
(user-error "Unable to determine cipher shift."))))
(let ((ciphertext "Dozdbv uhphpehu wkdw wr dujxh, dqg zlq, lv wr euhdn grzq wkh uhdolwb ri wkh shuvrq brx duh dujxlqj djdlqvw.\
Lw lv sdlqixo wr orvh brxu uhdolwb, vr eh nlqg, hyhq li brx duh uljkw. - Kduxnl Pxudndpl")
(english-dict (with-temp-buffer
(insert-file-contents-literally "/usr/share/dict/words")
(split-string (downcase (buffer-string)) "\n"))))
(+decrypt ciphertext english-dict))
mickeyp · 2022-03-17 07:48
Better still, Emacs comes with `decipher`, a package to aid in deciphering and breaking all manner of substitution ciphers:
https://www.masteringemacs.org/article/fun-games-in-emacs#decipher
b3n · 2022-03-17 07:49
See also: `M-x rot13-region`
Useful when you want to add spoilers to plain-text. Lbh jbhyqa'g jnag gb fcbvy fbzrguvat, jbhyq lbh?
[deleted] · 2022-03-17 07:52
[deleted]
arthurno1 · 2022-03-17 08:42
I don't understand why people down vote jokes.
AndreaSomePostfix · 2022-03-17 08:52
It is fantastic that there is also a mode to read what you cyphered (rot13-mode) :D
[deleted] · 2022-03-17 09:03
[deleted]
arthurno1 · 2022-03-17 09:16
I thought it was a joke. I have no idea about the history of the cipher :) Still, is there a reason to down vote if people are just conversing? If you don't like the conversation, don't take a part of it (I mean not you personally, who ever down voted your comment from the beginning).
vifon · 2022-03-17 09:31
I know nobody asked, but you may want to revisit your `use-package` call. Loading `message` while `:ensure`-ing `hydra` seems like something just waiting to break. Providing a non-bool to `:ensure` should be only used for packages with differing feature names and package names (example: `(use-package org :ensure org-plus-contrib)`), not for unrelated packages as a kind of dependency specification.
wasamasa · 2022-03-17 13:05
The decipher package is pretty nice, it can compete with web solutions like the [dcode.fr one](https://www.dcode.fr/monoalphabetic-substitution).
[deleted] · 2022-03-17 14:45
[deleted]
arthurno1 · 2022-03-17 15:51
I was expecting that people will do downvote it just for fun after I said it, so it's ok :). I guess you are right.
donio · 2022-03-17 16:37
It's useful for obscuring text, sort of like a spoiler tag.
thevoidzero · 2022-03-17 17:05
> always remember that to argue and win is to break down the reality of the person you are arguing against it is painful to lose your reality so be kind even if you are right
\- haruki murakami
Victory.
stayclassytally · 2022-03-17 18:44
Beware the ides of March
ThockiestBoard · 2022-03-17 18:51
[Relevant bit from `use-package` documentation](https://github.com/jwiegley/use-package#package-installation)
Bodertz · 2022-03-17 20:42
Interesting package. The "stats" related keybindings seem to break because the variable `decipher-stats-buffer` is nil. And the function `decipher-stats-buffer` fails when trying to create it. At least for me.
Other stuff seems to work, though.
CloudsOfMagellan · 2022-03-18 10:00
Wish I knew about this in my encryption class at uni