If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

## Computer science

### Course: Computer science>Unit 2

Lesson 2: Ciphers

# Shift cipher

## Modular Math and the Shift Cipher

The Caesar Cipher is a type of shift cipher. Shift Ciphers work by using the modulo operator to encrypt and decrypt messages. The Shift Cipher has a key K, which is an integer from 0 to 25. We will only share this key with people that we want to see our message.

## How to Encrypt:

For every letter in the message M :
1. Convert the letter into the number that matches its order in the alphabet starting from 0, and call this number X.
( A=0, B=1, C=2, ...,Y=24, Z=25)
2. Calculate: Y = (X + K) mod 26
3. Convert the number Y into a letter that matches its order in the alphabet starting from 0.
(A=0, B=1, C=2, ...,Y=24, Z=25)
For Example: We agree with our friend to use the Shift Cipher with key K=19 for our message.
We encrypt the message "KHAN", as follows:​
So, after applying the Shift Cipher with key K=19 our message text "KHAN" gave us cipher text "DATG".
We give the message "DATG" to our friend.

## How to decrypt:

For every letter in the cipher text C :
1. Convert the letter into the number that matches its order in the alphabet starting from 0, and call this number Y.
(A=0, B=1, C=2, ..., Y=24, Z=25)
2. Calculate: X= (Y - K) mod 26
3. Convert the number X into a letter that matches its order in the alphabet starting from 0.
(A=0, B=1, C=2, ..., Y=24, Z=25)
Our friend now decodes the message using our agreed upon key K=19. As follows:
So, after decrypting the Shift Cipher with key K=19 our friend deciphers the cipher text "DATG" into the message text "KHAN".

## Why is the Shift Cipher insecure?

A cipher should prevent an attacker, who has a copy of the cipher text but does not know the key, from discovering the contents of the message. Since we only have 26 choices for the key, someone can easily try all of the 26 keys, one by one, until they recover the message. This type of attack is called a brute force attack.

## Want to join the conversation?

• I wrote a small vbscript program to perform the modulo shift, and for most cases it worked.
However, if the input minus the shift value results in a negative number, vbscript's mod function outputs a negative remainder. The work around I had to implement was ultimately found from a quote from news:borland.public.delphi.objectpascal which stated: "Henrick Hellström wrote: I would strongly prefer that (X mod Y) > 0 if Y > 0. The expression (((X mod Y) + Y) mod Y) one has to write from time to time just looks awful!"

This really seems like a hack to get the mod function to work, however, I noticed in several google searches, that not every programming language treats modulo the same. SO if you try to code up a encrypt/decrypt routine based on the mod function, you've been warned. • Here's an alternate approach.

Since A mod B is the remainder R when we divide A by B and all integers can be written as A=B*Q+R (where Q is the quotient which is floor(A/B) )

A mod B is:
A-floor(A/B)*B

Without getting too deep into it, the quirky behavior behind mod in many programming languages has its roots in how computers represent negative numbers and how integer division is done on computers (truncating integer division).

Hope this makes sense
• since there are 26 letters in the alphabet
Why isn't A= 1, B = 2, C = 3 ... Y = 25, Z = 26? • In cryptography, we use A=0 ... Z=25. This makes sense, because if a shift is 0 you can assume that the letters were shifted by 0, which means they were shifted by nothing. Any letter shifted by 0 is that letter. Think of it like addition: you are adding a number. Any number + 0 is that number, therefore A+0 is A, right?
Also, if we used A=1, it would get very confusing because some people would think that A shifted by 1 is A and others would disagree. This would make it very hard to decrypt an encrypted message because you would have to decrypt the message twice, once for each possibility!
• I may be missing something. Under 'decryption', using k=19, 19 is subtracted from the cryptic message (3, 0, 19, 6) to give the result of -16, -19, 0, -13. Under that result, a new result of 10, 7, 0 13 is provided to give us the message 'khan'. Specifically, I'm unclear how -16 & -19 converted to 10 and 7 to give us the letters k & h in the message. Please explain and clarify this result. • Decryption. How does dividing -16 by 26 give us a remainder of 10?
26 goes into -16 how many times?

I understand the logic in the encryption when the numerator is positive, but I'm lost in the decryption. • Correct me if i'm wrong. But with mod=26, and A=0; you end up with 27 alphabetical characters... • I am still really confused on how to do this • In the encryption process, mod 26 is mentioned. How does that apply to encrypt the message. For example, in 29 mod 26, how is the 3 obtained? Also, how is 19 mod 26, 19?
(1 vote) • The mod operator gives the remainder of the top number divided by the bottom. For example, if we want 10 mod 3, we will start by dividing and getting `10 / 3 = 3 R 1` (read: 3 remainder 1) because the 10 is 1 over being divisible by 3 (9 is divisible by 3 and 1 less than 10). The remainder that we get, in this case, 1, is mod, so 10 mod 3 = 1.
For the examples you gave:
``29 mod 2629 / 26 = 1 R 3 because 29 = 1*26 + 3              ^    This is the mod29 mod 26 = 3``

And
``19 mod 2619 / 26 = 0 R 19 because 19 = 0*26 + 19              ^    This is the mod19 mod 26 = 19``

Hope this helps!
Neil   