Coding Vigenere Cyphers

Recently, I rewatched The Prestige, the very good Christopher Nolan move where Hugh Jackman and Christian Bale play rival magicians in the late 1800s. Jackman’s character has an old journal, written in cypher text that he slowly and meticulously needs to work out over the course of the movie.

I’ve always been fascinated by encryption, and I remembered that he used a Vigenere Cypher, but I had to look back up how those work.

Ceasar Cypher

Imagine a cypher where you just switch each number an agreed-upon number of letters up the alphabet. If you’re number is 3, then you would substitute D for A, E for B, etc. X would loop around and become A, Y would be B, etc.

This is a Ceasar Cypher, reportedly used by Julius himself, but I bet it didn’t take too long to break that code. If you think about it, the cypher that millions solve in the daily paper is an order of magnitude more difficult.

Enter Vigenere

To make it more difficult, you can shift each letter, but by a different amount for each position. So the first letter might shift by 3 characters, the second letter by 19, the third by 8. Instead of a shared number, you use a key phrase. We do our shift based on each letter’s position in the alphabet, using a = 0, b = 1, etc.

For example, if our key phrase is “ahoy”, then we shift the first number by 0 characters (a = 0), our second character by 7 characters (h = 7), our third character by 14 (o = 14), and our fourth character by 24 (y = 24). After that, we start back with a and shift by zero.

You can see how tedious this would be to decipher by hand, especially with a very long key phrase. And even today, if you avoided dictionary words and had a keyphrase that was very long, I think this would be difficult to decipher.

I thought it would be fun to code this algorithm. When I made that decision, I was on a bit of a Swift kick, so I did it in Swift. If you want to try, you can copy and paste from here, or use Github links that I’ll provide at the end. (I also made an implementation in javascript, so if Swift isn’t your thing, you can jump down just a bit.)

Swift Implementation

Here’s a class with all of the cypher/decipher logic:

And here’s an extension to String that lets you use it on any string:

Then to use it, you just need to apply the new methods to your string:

Javascript Implementation

Once my Swift project was old enough to be almost completely out of mind, I had a client looking for a relatively secure way to share an account password. I wrote up a document explaining how to get and use a PGP certificate key pair, but process is still just too technical. A day or so later, I thought about this algorithm, and figured I could do the same thing using javascript on a web form.

, I also decided that I needed a web form that does the same thing. That meant writing my same functions in javascript. I use an object to keep all of the functions and the keyword together in a namespace.

Then to use it, just set the key path, and call either transformString() or untransformString().

Again, here’s the page where I implemented this code.

This isn’t going to slow down the NSA one bit. If your keyphrase is really short, it’s almost not secure at all. You can improve your security by using only one case (or mixing cases at random), removing spaces and punctation, using very long key phrases, and/or avoiding dictionary words in your key phrases.

It’s probably good enough for sharing a passcode with a client, keeping your kids’ Christmas list secret, or making illicit plans with a date. (I’m talking to young people hiding their plans from parents here. Please don’t use my code to sneak around on your spouse, you creeps!)

Download source code from GitHub.

Let me know if you use any of this for your own fun little project!

Posted in Javascript, Swift | Tagged , , , , , , | Leave a comment

Comments are closed.