Pivoting Caesar Cipher: a toy encryption algorithm

English: Caesar cipher with a shift of 3.

English: Caesar cipher with a shift of 3. (Photo credit: Wikipedia)

As a fun little exercise, I decided I wanted to come up with an encryption algorithm. It’s nothing new, but it was enjoyable as a bit of a time-waster.

In this exercise, I’ve come up with a variation on the classic Caesar cipher (also known as ROT-13), where we shift characters a certain amount in order to mask our cleartext. However, unlike in ROT-13, we’re not going to use a fixed shift value. Instead, we’re going to establish particular characters—pivots—that cause the shift value to change for later characters in the message. The idea for this is that it will make it more difficult for an attacker to read the message, as they’d need to know which characters change the shift value, and how the value changes at each of those characters. There are two different ways we can approach the shift change: either by setting a new shift value outright, or by adjusting the existing shift value by adding or subtracting a value. We’ll go with the latter for this algorithm.

Besides adding pivot points and a changing shift value, let’s also define a range of valid characters that can be used for encryption. Caesar’s cipher was designed with only the basic Latin characters in use at the time, but these days there are more characters in the Latin alphabet, not to mention our use of Indo-Arabic numerals. For our example, we’ll use an “alphabet” of A-Z, 0-9, and the period / full stop for a total of 37 characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789. We could add more characters (additional punctuation, space, accented characters, etc.) but this should be good enough for most uses of such a simple cipher. Not to mention that 37 is a prime number, which may offer us some useful characteristics.

Since we’ve got into the example zone, let’s also set up our pivot points and their adjustment values. We’ll choose the vowels, the digit 0, and the full stop as our pivots, and the first seven primes as the shift adjustment values:

Character Adjustment Character Adjustment















Finally, we’ll choose an initial shift value. Remembering this system’s roots with the Caesar cipher, where we rotate by half the alphabet, we’ll choose 19, rounding to the odd number, coincidentally another prime number and the one that follows our final pivot point!

We want to encrypt the message “The password is swordfish.” First, of course, we’ll make it uppercase since our alphabet doesn’t have any lower case characters. Then we’ll replace the spaces with periods, and the period at the end with two periods so we know it’s the end of the sentence. Our resulting message for encryption is: “THE.PASSWORD.IS.SWORDFISH..

The first character of the message is T with position 20 in the alphabet (remember, it’s indexed from 1, not 0), and our initial shift value is 19. The sum is 39, and as that’s greater than the length of our alphabet, we have to take the modulus, or 2. Therefore, our ciphertext starts with B. The next character, H, becomes: (8 + 19) = 27 = 0. E: (5 + 19) = 24 = X, and since E is one of our pivot points, it’s time to adjust our shift value. We take our current shift, 19, and add the adjustment value 3 – the following characters will be shifted by 22 characters. With the next character being the period, we therefore calculate (37 + 22) mod 37 = 22 = V. And since the period is also a pivot, time to adjust the shift again: (22 + 17) mod 37 = 2.

But what if the shift adjustment for the period was 15? We would have ended up with a shift value of 0, but that would cause us to output cleartext until we encountered another pivot character. Therefore, let’s add the rule that if the shift value ever becomes 0, we reset it back to the initial shift value.

Likewise, we need a rule for when the modulus of the character position and shift against the alphabet length is 0. Whenever we encounter this situation, we’ll wrap back and use the final character of the alphabet, in this case the period.

When we run through the entire algorithm, we get the ciphertext “B0XVRCWW0S2OK.O6591AXZ2G6YE

Now, what about decoding the message? To convert our ciphertext back into something readable, we need to subtract the shift from each character. Given the character B and the initial shift of 19, we calculate (2 – 19) = –17. Wait. A negative number? We know about taking the modulus for positive numbers, but what do we do in this case? Simple: when we have a negative value, add the size of the alphabet. Therefore, we go (-17 + 37) = 20 = T. The next character, 0, has the value 27, so (27 – 19) = 8 = H. Then for X: (24 – 19) = 5 = E. And that’s a pivot point, so we adjust our shift by the adjustment value, giving us (19 + 3) = 22. After that, for V, (22 – 22) = 0? Uh oh, looks like we hit a snag. Let’s do the same thing as we did when creating the ciphertext, and wrap around backwards to the period. And again the shift gets adjusted, (22 + 17) mod 37 = 2.

Continuing through the decryption process, we return to our original cleartext: THE.PASSWORD.IS.SWORDFISH..