When I have experimented with using a key higher than 26 on a Caesar cipher, I have had some very ...interesting... results. The encryption works fine, but when I attempt to decrypt the encrypted message, I am unable to decrypt the message successful.

I understand that a Caesar can theoretically have a key size of 26, I am just curious as to what would happen with a longer key. Can somebody assist with both the mathematical side, and if it is possible to use the cipher with a key size higher than 26?

EDIT: In my program I am converting the message to ASCII, adding the key size. To decrypt, I convert it back from ASCII to characters.

  • 2
    $\begingroup$ If you define it the natural way, 26 is equivalent to plaintext, 27 equivalent to 1, etc. $\endgroup$ Dec 5, 2016 at 10:17
  • 1
    $\begingroup$ Please clarify. Do you allow to plaintext and ciphertext to contain only letters, or do you allow all ASCII characters? If you allow all ASCII then your max key can be value between 0 and 127. Any key higher then that will just duplicate the result of those 128 keys. $\endgroup$ Dec 5, 2016 at 10:42
  • $\begingroup$ Hey trickrider, anything missing from the given answers? Don't forget to follow up on your questions! $\endgroup$
    – Maarten Bodewes
    Dec 12, 2016 at 23:08

2 Answers 2


The Caesar cipher doesn't just add a value to the character value. What it does is the following (per character in the plaintext):

  • it converts the character to the index of the character in the alphabet, say 0..25 for uppercase characters;
  • it performs modular addition of the value, using the size of the alphabet as the modulus;
  • it converts the resulting index back into a character.

For example you add the key 3 to the 'Z' character, then you:

  • convert the 'Z' character to the index 25 (index starting with 0, so 26 - 1);
  • add 3 and perform mod 26: $25 + 3 = 28$, $28 \bmod 26 = 2$;
  • convert the index 2 to the alphabet, 'C' (as 2 means the third character).

In human language, once you're past 'Z', you start with 'A' again.

Decryption is identical, but it uses subtraction of the key instead of addition.

Great, so what if the key is 26 or higher?

Well, because we use modular addition you can simply do the following: $c = p + k (\mod 26) = p + k - n \times 26 (\mod 26)$. In other words, you can subtract 26 as many times as you want until you get in the index range 0..25 again.

In the example above, using a key of 29 is identical to a key valued $29 - 26 = 3$.

A quick mind also notes that $-3 \bmod 26 = 23$. Which means that decryption with key 3 is identical to encryption with key 23. So basically you only have to create an encryption routine.

You'll also notice that a key that is a multiple of 26 (0, 26, 52 will map the index (and thus the character) back onto itself. So those keys are weak keys and should not be used. A key of 13 will make decryption identical to encryption (as 26 - 13 = 13 again).

Now above explains what you do if you keep to an alphabet size of 26 characters. If you have an alphabet of 26 characters. You can also extend the alphabet, e.g. using the "alphabet":


In that case you can perform modular addition / subtraction with a modulus of $26 + 26 + 2 + 10 = 64$.

To view a good implementation in Python you can have a look here


By definition Caesar Cipher uses only 26 characters that are usually capital letters. The first step of implementation is changing them to numbers. You can do it by creating a substitution table in Your favorite programming language that might look like this (C#)

IDictionary<char, int> encode = new Dictionary<char, int> { { 'A', 0 }, { 'B', 1 }, { 'C', 2 }, (...), { 'Z', 25 }, };

Other way is to use ASCII. You can see that capital letters are in order and they begin on value 65. This means that You can just subtract from char 65 to get the same result: (C#)

char plaintextLetter = 'C';
int value = (int)plaintextLetter - 65;

At this point we changed a string to array of integers. We add the key, and make sure that our values don't fall away from 0-25 range using a modulo 26 operation. This step pretty much eliminates sense of any key larger than 25, because it will just be algorithmically identical to a key in range 0-25.

int cipher = (value + key) % 26

now all what's left is convert back to letters: (C#)

IDictionary<int, char> decode = new Dictionary<int, char> { { 0,'A' }, { 1,'B' }, { 2,'C' }, (...), { 25,'Z' }, };

Or by using ASCII table: (C#)

char ciphertextLetter = (char)(cipher + 65);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.