You are on page 1of 10

11/24/2016 Shift cipher | Ciphers | Journey into cryptography | Computer science | Khan Academy

Subjects New user / Sign up

COMPUTER SCIENCE | JOURNEY INTO CRYPTOGRAPHY | CIPHERS

Shift cipher
Share to Google Classroom Share Tweet
Email

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
COMPUTER SCIENCE JOURNEY
INTO CRYPTOGRAPHY will only share this key with people that we want to
Ciphers see our message.

Ciphers vs. codes


How to Encrypt:
Shift cipher For every letter in the message M :

XOR bitwise operation 1. Convert the letter into the number that matches
its order in the alphabet starting from 0, and call
XOR and the one-time this number X.
pad
( A=0, B=1, C=2, ...,Y=24, Z=25)
Practice: Bitwise
operators 2. Calculate: Y = (X + K) mod 26

Feedback

https://www.khanacademy.org/computing/computer­science/cryptography/ciphers/a/shift­cipher 1/10
11/24/2016 Shift cipher | Ciphers | Journey into cryptography | Computer science | Khan Academy

3. Convert the number Y into a letter that matches


Next tutorial
Cryptography challenge … 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:

COMPUTER SCIENCE
INTO CRYPTOGRAPHY
JOURNEY
So, after applying the Shift Cipher with key K=19
our message text "KHAN" gave us cipher text
Ciphers
"DATG".

Ciphers vs. codes


We give the message "DATG" to our friend.

Shift cipher

How to decrypt:
XOR bitwise operation
For every letter in the cipher text C :
XOR and the one-time
pad 1. Convert the letter into the number that matches
its order in the alphabet starting from 0, and call
Practice: Bitwise
operators this number Y.
(A=0, B=1, C=2, ..., Y=24, Z=25)
Feedback

https://www.khanacademy.org/computing/computer­science/cryptography/ciphers/a/shift­cipher 2/10
11/24/2016 Shift cipher | Ciphers | Journey into cryptography | Computer science | Khan Academy

2. Calculate: X= (Y - K) mod 26
Next tutorial
Cryptography challenge …
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


COMPUTER SCIENCE JOURNEY
INTO CRYPTOGRAPHY our friend deciphers the cipher text "DATG" into the
Ciphers message text "KHAN".

Ciphers vs. codes


Why is the Shift Cipher insecure?
Shift cipher A cipher should prevent an attacker, who has a
copy of the cipher text but does not know the key,
XOR bitwise operation from discovering the contents of the message.
Since we only have 26 choices for the key,
XOR and the one-time someone can easily try all of the 26 keys, one by
pad
one, until they recover the message. This type of
attack is called a brute force attack.
Practice: Bitwise
operators

Feedback

https://www.khanacademy.org/computing/computer­science/cryptography/ciphers/a/shift­cipher 3/10
11/24/2016 Shift cipher | Ciphers | Journey into cryptography | Computer science | Khan Academy

Ask a question...
Next tutorial
Cryptography challenge …
Questions Tips & Thanks Top Recent

I thought A=1, B-2, C=3, ... Z=0.


https://www.khanacademy.org/math/applied-
math/cryptography/crypt/v/polyalphabetic-cipher
Here, A is obviously 0.
Could someone please clarify? It was also shifted like this in
the challenge.
• 19 votes • 2 comments • Flag
3 years ago by The4thdimentionpro

Both are commonly used schemes.

As long as you use the same scheme in your encryption


and decryption step it doesn't matter (but your cipher
text will be different)
You could even use A=3,B=4,C=5,...X=0,Y=1,Z=2 and it
COMPUTER SCIENCE JOURNEY will still work (try it out).
INTO CRYPTOGRAPHY

Ciphers Many prefer to use A=0,B=1,C=2...Z=25 as it makes A


the identity element
i.e. the element you can add to another letter and it
Ciphers vs. codes won't change

Hope this makes sense


Shift cipher
• 33 votes • 2 comments • Flag
3 years ago by Cameron
XOR bitwise operation
Show all 5 answers • Answer this question

XOR and the one-time


pad
What does "mod 26" mean? There is a button on my
calculator that says "Mod" on it, and I've always wondered
Practice: Bitwise what it meant.
operators
• 16 votes • Comment • Flag
3 years ago by Roshni Kainthan
Feedback

https://www.khanacademy.org/computing/computer­science/cryptography/ciphers/a/shift­cipher 4/10
11/24/2016 Shift cipher | Ciphers | Journey into cryptography | Computer science | Khan Academy

MOD is short for modulus, which means ' the remainder


Next tutorial
left over after a value is divided by something'
Cryptography challenge …

Examples:
27 mod 26... 27 / 26 is 1 R 1, so 27 mod 26 is 1
53 mod 26... 53 / 26 is 2 R 1 so 53 mod 26 is also 1
10 mod 3 is also 1 (since 10 / 3 = 3 R 1)
2 mod 2 = 0
3 mod 2 = 1
4 mod 2 = 0
5 mod 2 = 1
6 mod 2 = 0
25 mod 26 = 25
50 mod 26 = 24

The function often associated with MOD is DIV, which


means the integer part of a division answer (2 div 2 = 1,
4 div 2 = 2, 5 div 2 = 2, etc.)
• 38 votes • 2 comments • Flag
3 years ago by DeWain Molter

Show all 2 answers • Answer this question


COMPUTER SCIENCE JOURNEY
INTO CRYPTOGRAPHY

Ciphers 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
Ciphers vs. codes
negative number, vbscript's mod function outputs a negative
remainder. The work around I had to implement was
Shift cipher ultimately found from a quote from
news:borland.public.delphi.objectpascal which stated:
"Henrick Hellström wrote: I would strongly prefer that (X
XOR bitwise operation
mod Y) > 0 if Y > 0. The expression (((X mod Y) + Y) mod Y)
one has to write from time… (more)
XOR and the one-time 8 votes • Comment • Flag
pad 3 years ago by Ken Sommerville

Practice: Bitwise
operators I just do this.
var a = x % y; 
Feedback a += y; 
a %= y;
https://www.khanacademy.org/computing/computer­science/cryptography/ciphers/a/shift­cipher 5/10
11/24/2016 Shift cipher | Ciphers | Journey into cryptography | Computer science | Khan Academy

Next tutorial Short and easy to write. I think it looks quite nice.
Cryptography challenge …
7 votes • 2 comments • Flag
3 years ago by Noble Mushtak

Show all 4 answers • Answer this question

since there are 26 letters in the alphabet


Why isn't A= 1, B = 2, C = 3 ... Y = 25, Z = 26?
4 votes • Comment • Flag
3 years ago by Snippet Studio

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
COMPUTER SCIENCE JOURNEY
INTO CRYPTOGRAPHY A and others would disagree. This would make it very
hard to decrypt an… (more)
Ciphers
5 votes • 1 comment • Flag
3 years ago by Lucie le Blanc
Ciphers vs. codes
Show all 2 answers • Answer this question

Shift cipher
Please explain "mod" for the examples.

XOR bitwise operation 3 votes • Comment • Flag


2 years ago by theanswerguys

XOR and the one-time


pad See
https://www.khanacademy.org/computing/computer-
Practice: Bitwise science/cryptography/modarithmetic/a/what-is-
operators modular-arithmetic
3 votes • Comment • Flag
Feedback 2 years ago by Cameron

https://www.khanacademy.org/computing/computer­science/cryptography/ciphers/a/shift­cipher 6/10
11/24/2016 Shift cipher | Ciphers | Journey into cryptography | Computer science | Khan Academy

Next tutorial
Cryptography challenge …

Do you have to add 26 for mod 26 after ever decryption?


For example:

H e l l o K=1
7 4 16 16 19
6 3 15 15 18
+26 26 26 26 26?
-----------------------------
32 29 41 41 44
Those aren't even letters on the number to letter step.
Please help I'm confused.
1 vote • 1 comment • Flag 2 years ago by Andrea

Mod 26 means you take the remainder after dividing by


26. So 36 mod 26 would give you 10. As a result,
shifting by 26 is the same as not shifting by zero. Here's
COMPUTER SCIENCE JOURNEY the steps to perform:
INTO CRYPTOGRAPHY
1. Convert the characters to numbers 
Ciphers 2. Pick a number to shift by 
3. Add the number to each character 
4. Mod each resulting number by 26 
Ciphers vs. codes
5. You message is now encrypted!
5 votes • 2 comments • Flag
Shift cipher 2 years ago by jdsutton

Show all 2 answers • Answer this question


XOR bitwise operation

XOR and the one-time


I understand that the caesar cipher can be broken in just 26
pad or less tries but here, in this shift cipher we're taking the
letter positions in the alphabet and then adding it to a shift
number. next we use modulus 26. How is the Cryptographer
Practice: Bitwise
operators going to know it was mod 26 and not mod 24 or 22? Or,
how is he even going to know we did something like
Modulus? And, can't we use mod 22 or any other number
Feedback closer to 26?

https://www.khanacademy.org/computing/computer­science/cryptography/ciphers/a/shift­cipher 7/10
11/24/2016 Shift cipher | Ciphers | Journey into cryptography | Computer science | Khan Academy

2 votes • Comment • Flag 8 months ago by Sam D


Next tutorial
Cryptography challenge …
Here are some issues to think about. What symbols will
you use to send the text? The reason we use modulus
26 is we are limited to only 26 recognizable symbols to
use. We could expand that list, however, the difficulty to
guess it only increases linearly...and we have
introduced more symbols which may make it more
difficult to decrypt at the other end for the intended
person. A similar problem arises when we use a smaller
modulus. If we have only 22 symbols to use then we
can really only… (more)
2 votes • Comment • Flag 7 months ago by CZ

If I were to decrypt B using k=3:


B=2, so 2- 3 = -1, mod(-1); wouldn't the answer still be -1? not
COMPUTER SCIENCE JOURNEY 25 which is Z, the correct answer?
INTO CRYPTOGRAPHY
I'm sorry for this noob question.
Ciphers
2 votes • 1 comment • Flag 2 years ago by shrumms

Ciphers vs. codes


B=2
B-k = -1
Shift cipher out of range of 0 to 25,
so add 26 26+ (-1) = 25
25 =Z
XOR bitwise operation
2 votes • Comment • Flag
6 months ago by TomM8132
XOR and the one-time
pad Show all 4 answers • Answer this question

Practice: Bitwise
operators Why we cannot comment also under the exercises or
games? It would be more convenient to discuss possible
setbacks right there, won´t it?
Feedback
2 votes • Comment • Flag 2 years ago by galvanek.m
https://www.khanacademy.org/computing/computer­science/cryptography/ciphers/a/shift­cipher 8/10
11/24/2016 Shift cipher | Ciphers | Journey into cryptography | Computer science | Khan Academy

Next tutorial
Cryptography challenge …
I have a feeling it might be to discourage people from
finding the answers they need in the comments. You
could click on the "request for features" link on the right
if you wanted to ask them though!
2 votes • Comment • Flag
2 years ago by jon.email

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.
2 votes • Comment • Flag 2 years ago by mike boy
COMPUTER SCIENCE JOURNEY
INTO CRYPTOGRAPHY

Ciphers
As per step 2:
2. Calculate: X= (Y - K) mod 26
Ciphers vs. codes (3-19)mod 26 = -16 mod 26 = 10
(0-19)mod 26 = -19 mod 26 = 7
(19-19)mod 26 = 0 mod 26= 0
Shift cipher (6-19 )mod 26 = -13 mod 26 = 13
See
https://www.khanacademy.org/computing/computer-
XOR bitwise operation
science/cryptography/modarithmetic/a/what-is-
modular-arithmetic
XOR and the one-time for more info on the mod operator
pad
3. Convert the number X into a letter that matches its
Practice: Bitwise order in the alphabet starting from 0.
operators (A=0, B=1, C=2, ..., Y=24, Z=25)
(Note letters start at 0)
10 maps to the 11th letter in the… (more)
Feedback
2 votes • Comment • Flag
https://www.khanacademy.org/computing/computer­science/cryptography/ciphers/a/shift­cipher 9/10
11/24/2016 Shift cipher | Ciphers | Journey into cryptography | Computer science | Khan Academy

2 years ago by Cameron


Next tutorial
Cryptography challenge … Show all 3 answers • Answer this question

Show more comments

Ciphers vs. codes XOR bitwise operation

ABOUT SUPPORT COACHING CAREERS INTERNATIONAL

Our Mission Help center Teacher and Full Time English


Coach Resources
You Can Learn Support Internships Translate our content
Anything Community Teacher and
Coach Community
Our Team
CONTRIBUTE SOCIAL
Our Interns CONTACT US
Donate Facebook
Our Content Share Your Story
Volunteer Twitter
Specialists
Press
Our Supporters Blog
COMPUTER
Our Board SCIENCE JOURNEY
INTO CRYPTOGRAPHY Life at KA

Ciphers
Terms of Use Privacy Notice © 2016 Khan Academy
Except where noted, all rights reserved.
Ciphers vs. codes
Free classes and courses available for online learning at every level: Elementary, High school, and College lessons.

Shift cipher

XOR bitwise operation

XOR and the one-time


pad

Practice: Bitwise
operators

Feedback

https://www.khanacademy.org/computing/computer­science/cryptography/ciphers/a/shift­cipher 10/10

You might also like