Two years ago, when I was still in school, I wrote a letter to my future self (it was a school project). It contained a few encrypted messages, so that my present me could do a fun exercise now.
The message was encrypted using Caesar Cipher, which just increases every letter by a specified number, producing unreadable text (I mean, you could also decrypt this in your head by just looking at the the text for some time, but this way it's more fun).
Turns out out it was a really fun 10 min coding challenge. I should do more of them as I often tend to forget how much fun programming actually is and always was.
Maybe this was the most important message to my future self: Never stop enjoy coding.
Here is the code needed to decrypt a Caesar encrypted message using brute force.
function caesar(str, increment) { | |
const letters = 'abcdefghijklmnopqrstuvwxyz'; | |
return str | |
.toLowerCase() | |
.split('') | |
.map(c => { | |
const index = letters.indexOf(c); | |
if (index === -1) return c; | |
let newIndex = index + increment; | |
if (newIndex < 0) { | |
newIndex = letters.length - 1; | |
} else if (newIndex >= letters.length) { | |
newIndex = newIndex- letters.length; | |
} | |
return letters[newIndex]; | |
}) | |
.join(''); | |
} | |
function allCaesar(str) { | |
for (let i = 0; i < 26; i++) { | |
console.log(caesar(str, i)); | |
} | |
} | |
allCaesar('pbqr vf neg. Nanepul fubhyq gnxr bire gur jbeyq. wninfpevcg sbe gur shgher?') |
Top comments (0)