Decrypting the Caesar cipher using shell

In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. [...]
#!/bin/sh

IN="MJHVIZN ZPIO YJHPN"

for I in $(seq 25); do
    echo $I $IN | tr $(printf %${I}s | tr ' ' '.')\A-Z A-ZA-Z
done

This outputs:

1 NKIWJAO AQJP ZKIQO
2 OLJXKBP BRKQ ALJRP
3 PMKYLCQ CSLR BMKSQ
4 QNLZMDR DTMS CNLTR
5 ROMANES EUNT DOMUS
6 SPNBOFT FVOU EPNVT
7 TQOCPGU GWPV FQOWU
8 URPDQHV HXQW GRPXV
9 VSQERIW IYRX HSQYW
10 WTRFSJX JZSY ITRZX
11 XUSGTKY KATZ JUSAY
12 YVTHULZ LBUA KVTBZ
13 ZWUIVMA MCVB LWUCA
14 AXVJWNB NDWC MXVDB
15 BYWKXOC OEXD NYWEC
16 CZXLYPD PFYE OZXFD
17 DAYMZQE QGZF PAYGE
18 EBZNARF RHAG QBZHF
19 FCAOBSG SIBH RCAIG
20 GDBPCTH TJCI SDBJH
21 HECQDUI UKDJ TECKI
22 IFDREVJ VLEK UFDLJ
23 JGESFWK WMFL VGEMK
24 KHFTGXL XNGM WHFNL
25 LIGUHYM YOHN XIGOM

The only line which makes sense is 5 ROMANES EUNT DOMUS, giving us our solution.

Comments (4)

Or you could use the caesar command from the bsdgames packages:

corsac@scapa: echo MJHVIZN ZPIO YJHPN |caesar
ROMANES EUNT DOMUS

(yes, it automatically finds the right key using letter frequencies)

Aug. 31, 2013, 9:46 p.m. #
Neat.
Andy Cater

"People called Romans they go the house?" - _Much_ more impressive if it translated it properly as

"Romani ite domum" (including hte locative). :)

Don't know if the Debian package for Crank also does Caesar slide ...

Aug. 31, 2013, 10:44 p.m. #
Dominick

When I want to decrypt my own words, I put the phrase in. But something happens, and every output is the same as the input.. Why? What am I doing wrong?

July 26, 2014, 5:19 a.m. #
Fa

Hi, nice tip. I didn't know abot the dot in tr, how does it works? I don't see it in the manual....
Thanks!

April 26, 2015, 12:17 a.m. #