# What's the point of the modulo operation in the Caesar cipher formula?

According to the problem set specification, the formula for encrypting a message using the Caesar cipher is:

``````ci = (pi + k) % 26
``````

I don't get why you need to do modulo 26. What does it even do here?

Let's consider the encryption formula given in the problem set:

``````cᵢ = (pᵢ + k) % 26
``````

Here, as in the problem set,`cᵢ` represents the value of the encrypted or enciphered letter, `pᵢ` represents the value of the plain text letter, and `k` represents the value of the encryption key.

The English alphabet has 26 letters. Let's represent those letters with the numerals 0-25, in the standard order.

So, we end up with `a = 0`, `b = 1`, `c = 2`, `...`, `z = 25`.

Let's also assume that the encryption key `k = 13`, just as in the problem set specification.

Let's try to encrypt the letter `b` using the encryption formula. We need a value for `pᵢ`, which must be `1`, because we determined that `b = 1` earlier. We also know that the key `k = 13`. So, after substitution, we end up with `cᵢ = (1 + 13) % 26`, which evaluates to `cᵢ = 14 % 26`, which evaluates further to `cᵢ = 14`. We also determined that `o = 14` earlier.

Therefore, the encrypted result of the letter `b` is the letter `o`.

In this case the modulo operation is not essential, as we would've arrived at the same result without it.

Let's try to encrypt the letter `s` using the encryption formula. We need a value for `pᵢ`, which must be `18`, because we determined that `s = 18` earlier. We also know that the key `k = 13`. So, after substitution, we end up with `cᵢ = (18 + 13) % 26`, which evaluates to `cᵢ = 31 % 26`, which evaluates further to `cᵢ = 5`. We also determined that `f = 5` earlier.

Therefore, the encrypted result of the letter `s` is the letter `f`.

In this case the modulo operation is essential, as without it `cᵢ = 31`, and we only determined letter values for `0-25`.

So, the modulo operation ensures that the encryption formula evaluates to a number that never exceeds the upper bound of 25.

• What if the key is 1, and the letter to be encrypted is 'y' c = (25 + 1) % 26 = 0 which is wrong... value of c should be 26 to represent the letter 'z'
– user7612
Jul 5 '15 at 4:42
• to solve this you can use the following formula: <br/> `c = (p + k - 1) % (26) + 1` e.g. letter 'y', and encryption key k = 65 `c = (25 + 65 - 1) % (26) + 1 = 12` which is evaluates to letter 'l'
– user7612
Jul 5 '15 at 5:10
• FYI for anyone reading this in the future like I am, the first user thinks Y = 25 when it's actually 24 (since A = 0). Letter #25 is actually Z, so 25+1 % 26 being 0 makes sense: Z (25) is shifted 1 letter to A (0). Jan 23 '18 at 1:32
• The comments above made me realize there was a problem with my initial solution. Since I initially let a = 1, b = 2, etc., instead of a = 0, b = 1, etc., it did not produce correct results with a key of 1. I think I've fixed it now. Jan 24 '18 at 3:38
• That's right, in cs50 as an introduction to computer science we're supposed to get used to thinking 0-indexed, i.e. start to count from 0. Therefore, a = 0 and z = 25. Apr 21 '19 at 19:46

The purpose of the formula is to calculate the new (cyphered) value for a given character on a string, considering a swift value.

The formula has 4 components:

ci = ciphered character pi = current position k = number of positions to be shifted 26 = number or alphabet characters

There's is something important here. The formula assumes that the first character ('a') is represented by 0, and that the last character ('z') is represented by 25. 26 characters total.

Lets apply the Caesar Cypher to the string "abyz" and a shift of 1:

``````a is replaced by 0 where pi
ci = (0+1) % 26
ci = 1

# b is replaced by 1 where pi
ci = (1+1) % 26
ci = 2

# y is replaced by 24 where pi
ci = (24+1) % 26
ci = 25

# z is replaced by 25 where pi
ci = (25+1) % 26
ci = 0
``````

As a result, we have generated a list of numbers corresponding to the position of the cyphered character on the alphabet.

``````a became to  1   or  b
b became to  2   or  c
y became to  25  or  z
z became to  0   or  a
``````

The resulting string is: 'bcza'

I hope it was helpful,

Making it more simpler.

Its Modular Arithmatic "where numbers "wrap around" upon reaching a certain value the modulus ".

after 26th alphabet it has to wrap around.

Assuming a = 1, b =2 .... and so on

taking just mod opertaion.

``````1 mod 26 = 1 (a)
2 mod 26 = 2  (b)
26 mod 26 = 0 (z)
...... and wrap it over..
``````

Adding Caesar cipher to it

``````(1+Shift) mod 26
(1+1) mod 26 = 2 (a becomes b) i.e. a shifted once
(1+2) mod 26 = 3 (a becomes c) i.e a shifted twice
``````

So to wrap around modular arithmatic is needed.

There are 26 letters of the alphabet. The cipher key can be ( a lot) longer than 26, but as soon as it reaches 27, it's been around the whole alphabet once and goes back to the start: Your key might as well have been 1, as the first 26 moves were useless as they have cancelled themselves out. And that applies to all multiples of 26, as you'd just be wasting time spinning around the alphabet.

So with a key that is greater than '26', if you subtract '26', it will lead to the same outcome. So, to avoid effort you might as well subtract '26' as many times as possible, until the remainder is less than 26 and only consider that remainder. And that is what the % modulo does.