Caesar’s Cipher

In today’s installment of the Basic Algorithm Scripting series, we will be creating an algorithm called Caesar’s Cipher. Let’s meet caesear:

Caesar, the caesar salad.

We will use caesar, the caesar salad. Have you ever wonder what happened to Julius Caesar? … well several hundred thousand years later (a bad estimate), he is now a salad. And one more thing, do him a favor and do not touch his croutons.

As for the function, Caesar Cipher is one example of a Cipher (an algorithm for performing encryption or decryption) and Caesar Cipher is one of the simplest and more known. What you do in Caesar Cipher is you shift each letter in a word by a certain amount. It is also called Shift Cipher which…shifts letters by some set amount.

This function uses ROT-13 which is a simple letter substitution cipher that this Cipher uses many times. In the ROT-13, the values of the letters are shifted by 13 places. Thus ‘A’ ↔ ’N’, ‘B’ ↔ ‘O’ and so on.

This function will take in a ROT-13 encoded string as input and returns the string decoded. For this example, the input string will be “SERR CVMMN!”. This function should only shift alphabetic characters. So if there are any punctuations, numbers or a space, the function should not shift.

Mr. Caesar Salad turns around, you’ll see the encoded word. Once you split the string into individual characters in an array, you should see dancing croutons:

While you are drooling over these tasty, sizzling, glistening croutons, you are probalby thinking “how would I let the program know which letter comes after what in the alphabet?” There is something called ASCII (American Standard Code for Information Interchange).

So bright. So exciting.

To make the definition simple. Computers read text differently all it sees are numbers. In order for the computer to encode a number to text, ASCII code represents text on a computer to read. It is a character encoding standard. If you look up ASCII code table, you’ll see the text characters from lower case letters to upper case letters, punctuation and everything else that you see on a keyboard, represented by a number. There’s hexadecimal, decimal and other representations of each character but worry about decimal.

How does Javascript convert letters to ASCII code and the other way around? There is String.fromCharCode() and String.prototype.charCodeAt().

For String.fromCharCode(), you input a value and fromCharCode outputs the characters corresponding to the sequence of Unicode values.String.fromCharCode(65, 66, 67); // "ABC" or String.fromCharCode(97, 98, 99); // "abc" .

Capital letters and their ASCII code decimal representation (in red)

For String.charCodeAt(), you input a string and charCodeAt() outputs the Unicode of the first character in the string. It’s the opposite of String.fromCharCode() where you input the unicode value and it outputs the corresponding character whereas String.charCodeAt(), you input the string:

var str = "Hello World";

var n = str.charCode(0) // 72

and it outputs the unicode value. Unicode is a superset of ASCII and for this function and algorithm, the numbers 0–128 have the same meaning in ASCII as they have in Unicode (letters A-Z (65–90) are the same in ASCII and Unicode). Although Unicode and ASCII are not the same thing, explaining their differences goes outside of the focus of explaining what this Caesar Cipher algorithm does. You are welcome to read more about ASCII and Unicode. Now back to the function:

Here are the unicode values for each letter. Initiate a variable to append the new characters after they have been shifted 13 places. When iterating through each letter in the array, by using String.charCodeAt(), make sure the values are between 65 and 90 (greater than 64 but less than 91). Capital letters and lower case letters have different Unicode values. For capital letters 65 is A and 90 is Z. If they are not between those values, continue on and still append the value to the new string. If the next character is a letter that is between 65 and 90, add a 13 to the unicode value and use String.fromCharCode() to convert the Unicode value back to its corresponding character. But there is a problem:

After adding 13 to the value, what if the Unicode value becomes greater than 90? Well because there are 26 letters in the English alphabet, simply subtract 26 from the value greater than 90 and everything is back to normal again. Think of it as starting over whenever you get to a letter that comes after the letter M. For example, N, which is ASCII value (78), plus 13 will go to 91 but when subtracted by 26, you go back to 65 or the letter ‘A’.

The new values are circled

After iterating and using String.fromCharCode() to convert the Unicode value for each letter back to its corresponding character, the first five characters will spell out as:

After going through the rest of the string “SERR CVMMN!”, the end result (or decoded result) will be a greasy slice of:

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.