This post is Part 2 of a three part series:
Using the Vignere Cipher to Encrypt a Message (Part 1)
Using the Vignere Cipher to Encrypt a Message (Part 2) <– You are here
Using the Vignere Cipher to Encrypt a Message (Part 3)
In Part 1, I gave a brief introduction to the Vignere cipher and how it works. I mentioned that there are two ways to go about emulating the Vignere cipher with a program. One of them involves essentially replicating the Caesar cipher, which I covered in this post, except with a dynamic shift number based on the keyword. The other method involves a Vignere table. In this post, I’m going to cover the first approach: the pseudo-Caesar cipher method.
This is a really fun problem and it takes a little while to get into, but once you get a grasp on what’s happening, it’s extremely rewarding.
Outlining our approach in psuedocode would look something like this:
Let’s make our basic function first, then deal with the details of how to encode it after.
So, in this block, we have laid the groundwork for our vunderful program. Now, we need to give the
encode() function some function-ality!
To encode or not to encode?
We’ll start by asking ourselves what we want this function to accomplish. We want it to take in two parameters: a message, and a string with the same length as message consisting of a keyword repeated over and over again. Then, we want to do some magic to these inputs, and essentially layer them together to create an encoded word.
In order to do this “layering,” we will use the Caesar cipher approach. We’ll get the character code of each letter in the message, the character code of the corresponding letter in the keyword string (at the same index), combine these character codes, and presto change-o this cumulative character code into a new letter: our cipher letter. Do this for each letter in the message, and we have our cipher text.
Now, in code:
See where I used the
let keyword in the callback to the
.forEach method? To be honest, I’m not quite sure when it’s appropriate to use
let vs. when not to. Perhaps that’s a good topic for a future post. I know it has something to do with block scoping and the variable getting “trashed” after it’s used, so it doesn’t waste memory. Based on these two pieces of information, I think it’s safe to say that it’s best to use
let when you have a for loop or a higher order function and want to generate a temporary, local variable to operate on, such as in this case.
Anyways, back from tangent town – this function looks a lot like the Caesar cipher because it is, in fact, a Caesar cipher. Nothing really new here, so in the interest of keeping this blog DRY, I’d recommend checking out this post to learn more about how this code works.
In Part 3, things get a little more interesting. I’ll go over how to solve this problem using a Vignere table instead.
Til next time, Vinny out.