DEV Community

Cover image for Render to Caesar the things that are Caesar's
JP Antunes
JP Antunes

Posted on • Updated on

Render to Caesar the things that are Caesar's

The /r/portugal sub-reddit can be a surprising little place. Today someone asked if the message in the cover picture was some form of encrypted text.

Well, it's a Caesar's cipher with ROT1, which is a simple dictionary substitution cipher and we can easily come up with encrypt/decrypt functions by mapping the character index in the source message's alphabet to the destination's.

//first we need to define our regular alphabet
const alphaB = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
//now the rot1 alphabet, ie our regular alphabet shifted by one place
const rot1 = 'ZABCDEFGHIJKLMNOPQRSTUVWXY'

//our encrypt/ decrypt functions use the same logic but invert the source and target alphabets
const caesarsEnc = str => [...str].map(e => alphaB[rot1.indexOf(e)])
const caesarsDec = str => [...str].map(e => rot1[alphaB.indexOf(e)])

The functions only work with capital letters, but it's enough for our purposes.

const myMsg = 'HELLO WORLD';
> caesarsEnc(myMsg).map(e => e !== undefined ? e : ' ').join('')
'IFMMP XPSME'

const mySecretMsg = 'IFMMP XPSME'
> caesarsDec(mySecretMsg).map(e => e !== undefined ? e : ' ').join('')
'HELLO WORLD'

Top comments (0)