How to Build a Cipher Machine with JavaScript

Originally published at: https://www.sitepoint.com/how-to-build-a-cipher-machine-with-javascript/

I was overjoyed recently when I read the news that the British mathematician, Alan Turing will feature on the Bank of England’s new £50 note. Turing occupies a special place in the hearts of computer nerds for effectively writing the blueprints for the computer. He also helped to break the notoriously difficult naval Enigma code used by the Nazi U-boats in World War II. In honor of this I decided a quick tutorial to build a cipher machine using the JavaScript skills that are covered in my book JavaScript Novice To Ninja.

The cipher we’ll be using is the Caesar cipher, named after the Roman emperor, Julius Caesar. It is one of the most simple ciphers there are and simply shifts each letter along a set number of places. For example, the phrase ‘Hello World’ would become ‘KHOOR ZRUOG’ using a shift of 3 (which it is the shift that Julius Caesar is thought to have used).

Our cipher machine

You can see an example of the finished code here. Have a play around at writing some secret messages to get a feel for how it works.

To get started, fire up your favorite text editor and save the following as caesar.html: