DEV Community

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

Posted on • Edited on

1

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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay