SouthParkMe

Hi.

This site tends to hold geeky things.

Caesar Cipher

Caesar Cipher

The Caesar cipher is a simple shift in letters. Traditionally, Caesar would shift by three, replacing A with D, B with E and so on.

To decode, this is simply reversed.

Here is an example :

PLAIN: THIS IS THE CAESAR CIPHER
CODED: WKLV LV WKH FDHVDU FLSKHU

What if we don’t know how much the shift is? We simply write the alphabet below the message, looping around when we get to Z.

Notice the message starts ‘W’, so in that column we write X, Y, Z, A, B….. this is repeated for each letter.

One line will contain the message.

00 : WKLV LV WKH FDHVDU FLSKHU
01 : XLMW MW XLI GEIWEV GMTLIV
02 : YMNX NX YMJ HFJXFW HNUMJW
03 : ZNOY OY ZNK IGKYGX IOVNKX
04 : AOPZ PZ AOL JHLZHY JPWOLY
05 : BPQA QA BPM KIMAIZ KQXPMZ
06 : CQRB RB CQN LJNBJA LRYQNA
07 : DRSC SC DRO MKOCKB MSZROB
08 : ESTD TD ESP NLPDLC NTASPC
09 : FTUE UE FTQ OMQEMD OUBTQD
10 : GUVF VF GUR PNRFNE PVCURE
11 : HVWG WG HVS QOSGOF QWDVSF
12 : IWXH XH IWT RPTHPG RXEWTG
13 : JXYI YI JXU SQUIQH SYFXUH
14 : KYZJ ZJ KYV TRVJRI TZGYVI
15 : LZAK AK LZW USWKSJ UAHZWJ
16 : MABL BL MAX VTXLTK VBIAXK
17 : NBCM CM NBY WUYMUL WCJBYL
18 : OCDN DN OCZ XVZNVM XDKCZM
19 : PDEO EO PDA YWAOWN YELDAN
20 : QEFP FP QEB ZXBPXO ZFMEBO
21 : RFGQ GQ RFC AYCQYP AGNFCP
22 : SGHR HR SGD BZDRZQ BHOGDQ
23 : THIS IS THE CAESAR CIPHER 👈
24 : UIJT JT UIF DBFTBS DJQIFS
25 : VJKU KU VJG ECGUCT EKRJGT


This can also be done by using a cylinder called a wheel cipher, sometimes called a Jefferson Wheel Cipher.

This has rings running A to Z along its length. Dial in the message (in sections if neccesary, and rotate the cylinder to read off the result).

I am imagining wheels with the alphabet in order, but a wheel cipher need not have the alphabet in order - and so shuffling the wheels can make it hard to decrypt.

Wheel Cipher

Photo Credit: Ryan Somma

The Caesar cipher is very easy to undo by trial and error. Even if we didn’t know it was a Caesar cipher, we could count up the letters, and we would note the pattern of letter frequencies matched the underlying language, just shifted along a bit.

The Caesar cipher is not at all secure.

A common use for a Caesar cipher is a shift of 13. This is called ‘ROT13’ and, if applied twice, it gets undone. It can be used to provide a little obscurity in, for example, an online discussion containing film spoilers.

Extension

I wrote some Python code to generate the example used above. Firstly, here is the code for the example (to decrypt, or shift the letters by a different about, change the +3 to another number, up to 25. To decrypt, use 23 (as 3+23 is 26 - you're back where you started) :

plaintext = input().upper()
ciphertext = ""

for letter in plaintext:
    val=ord(letter)-65
    if 0 <= val <= 25 :
        ciphertext += chr((val+3) % 26 +65)
    else:
        ciphertext += letter

print('PLAIN:',plaintext)
print('CODED:',ciphertext)

For generating the list to 'solve' the cipher, I used this code :

plaintext = input().upper()

for i in range(26):
    ciphertext = ""

    for letter in plaintext:
        val=ord(letter)-65
        if 0 <= val <= 25 :
            ciphertext += chr((val+i) % 26 +65)
        else:
            ciphertext += letter

    print(str(i).zfill(2),':',ciphertext)

Extant et ad Ciceronem, item ad familiares domesticis de rebus, in quibus, si qua occultius perferenda erant, per notas scripsit, id est sic structo litterarum ordine, ut nullum verbum effici posset: quae si qui investigare et persequi velit, quartam elementorum litteram, id est D pro A et perinde reliquas commutet.

Suetonius, Life of Julius Caesar 56

Links

Recent Cryptography Posts

Finding Pythagorean Triples

Finding Pythagorean Triples

Braess' Paradox

Braess' Paradox