# Caesars Cipher - how did you figure out the algorithm?

SPOILERS BELOW!

==============================================================================
In the topic title, I’m referring to the modulo operator that some people have used to encipher /decipher their messages. I think it’s a brilliant solution but I’d never have thought of it. How did those of you who used it, think to use it? For instance, was it prior experience of a similar problem or did you spot something in the problem which lent itself to modulo operations?

I included my non-modulo thinking below but it’s not a necessary read

==============================================================================
Here was my thinking:

I looked up the unicode/ASCII references and first of all simply subtracted 13 from the test string - I found out (a little too far into coding) that this would return non-Alphabetic characters if I tried to decipher a letter lower than N. As the alphabet isn’t arranged in a circle so to speak but a ‘straight’ section of a larger set, subtracting 13 on these letter would end up in the non-alphabetic range of the code set.

Anyway, from there I used if statements to define the limits of the 1st and 2nd half of the capitalised alphabet section and added or subtracted 13 accordingly.

So on to this modulo business…

I can see how %26 gives a unique number to each letter and if you add 65 to it, you will get the letter’s counterpart. I can also see how 26 is the best modulo number to use as it’s the range (any other number wouldn’t convert each letter to it’s counterpart accurately (I think)). And adding the constant 65 is because it’s the starting number of at the beginning of the alphabet and the remainders increase in steps of 1, unless it hits a multiple of 26… But how the heck did people think to use it in the first place?!

1 Like

In my case, it was previous experience with another MOOC. For some explanation, I read about Caesar Cipher and Vigenere Cipher.

Thanks for the input, lleander. There is a clock example on this site: https://www.artofproblemsolving.com/wiki/index.php/Modular_arithmetic/Introduction#Sample_Problem

That helped me along a little too