Documente Academic
Documente Profesional
Documente Cultură
Brian Veitch
July 2, 2013
Contents
1 Introduction
1.1 Alice, Bob, and Eve . . . . . . . . . . .
1.2 Basic Terminology . . . . . . . . . . .
1.3 Brief History of Cryptography . . . . .
1.4 Kerckhoffs Principles of Cryptographic
.
.
.
.
3
3
4
4
11
2 Substitution Cipher
2.1 Caesar Cipher Activity . . . . . . . . . . . . . . . . . . . . . . . . .
2.2 Keyword Cipher Activity . . . . . . . . . . . . . . . . . . . . . . . .
2.3 Vigenere Cipher Activity . . . . . . . . . . . . . . . . . . . . . . . .
13
13
17
20
3 Transposition
3.1 Easy Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 Railfence Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3 Transposition Activity . . . . . . . . . . . . . . . . . . . . . . . . .
22
22
22
23
4 Modular Arithmetic
4.1 Examples of Congruences . . . . .
4.2 Arithmetic with Congruences . . .
4.3 Multipication Tables . . . . . . . .
4.4 Finding the Inverse . . . . . . . . .
4.5 Solving Equations (mod n) . . . . .
4.6 The Extended Euclidean Algorithm
24
26
27
27
29
32
34
.
.
.
.
.
.
.
.
.
.
.
.
. . . . .
. . . . .
. . . . .
Secuity
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
CONTENTS
.
.
.
.
.
37
37
37
38
39
40
.
.
.
.
.
.
.
42
42
42
45
46
50
52
53
54
54
56
8 Important Information
8.1 Caesar Shift Wheel . . .
8.2 Letter Frequency Tables
8.3 Vigeneres Table . . . . .
8.4 ASCII Table . . . . . . .
8.5 Useful Websites . . . . .
57
57
58
59
60
60
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
6 RSA Encryption
6.1 Introduction . . . . . . . . . . . . . . . . . . .
6.2 Example of RSA Encryption . . . . . . . . . .
6.3 RSA Algorithm . . . . . . . . . . . . . . . . .
6.4 Using The Extended Euclidean Algorithm . .
6.5 ASCII Table . . . . . . . . . . . . . . . . . . .
6.6 Cracking RSA by finding the private key d. .
6.7 Your turn to crack an RSA encrypted message
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1 INTRODUCTION
Introduction
Suppose Alice wants to send Bob a message. She doesnt want Eve to intercept it
and read it, so she decides to disguise it. Heres a diagram that outlines the
whats happening.
Your hope is that Eve doesnt know how to figure out your message or alter it in
some way.
1 INTRODUCTION
1.2
Basic Terminology
Cryptography is an ancient science. The term comes from the Greek language
meaning the study of hidden or secret writing. Ever since there were multiple
tribes (or countries), we needed a way to communicate to one another without
the other tribe (country) understanding it. Today its vital to communicatations,
authentication, sending sensative data over the internet, purchasing items from
1 INTRODUCTION
Another Greek tool for disguising a message was called the Scytale. It is a tool
used to perform a transposition cipher, consisting of a cylinder with a strip of
parchment wound around it on which is written a message. In other words, it was
a way to rearrange the letters.
You would wrap a long thin parchment around a rod. You then write your
message across the rod, SEND HELP!
1 INTRODUCTION
When you unwrap the parchment the letters appear rearranged. To understand
the message, the receiver would need a rod of the same size.
Julius Caesar used his own way of disguising messages to send to his military. We
know call it the Caesar Cipher. If he had anything important to send, he would
write out his message and then replace each letter of his message with another
letter down the alphabet. So A = D, B = E, C = F, D = G, etc. When it needed
to be read, the receiver of the message would reverse this process, changing D
back to A, E back to B, F back to C. We will go over this method in great detail
in the next chapter. The tool we will use will look something like this,
1 INTRODUCTION
This method is a form of substitution, where you replace one letter for another.
Over the years, the substitution method evolved. In this next chapter, we discuss
the Keyword Cipher, which is another form of substitution.
The Vigenere cipher was a substitution cipher where each letter of the message
used its own caesar shift. This means the first letter might be shifted by 3 letters,
the second letter of the message could be shifted by 5 letters, the third letter by
3, the fourth letter by 5, the fifth letter by 3, and so on. This particular pattern
would be two Caesar shifts with 3 and 5. For 300 years it was believed to be
indecipherable. It can now be broken by determining the number of different
caesar shifts and then do a frequency analysis on the letters that use the same
Caesar shift.
To give you an idea, suppose your keyword is DOG and you want to disguise
the message HELP ME, you use the Vigenere Table below to determine the
ciphertext.
1 INTRODUCTION
Message: H E L P M E
Keyword: D O G D O G
Cipher:
K S R S A K
Why was this cipher so strong? Consider the ciphertext. There are two Ks but
each corresonds to a different letter in the plaintext. One K decrypts back to H
while the other K decrypts to E.
In 1917 U.S. Army Major Joseph Mauborgne developed a technique known as the
One-Time Pad. It works the same way as a Vigenere Cipher. The only
1 INTRODUCTION
difference is the keyword is a random string of letters that is the same length as
the message. That way the keyword never repeats which makes frequency
analysis useless. If this method is done correctly, it is the only provably
unbreakable encryption algorithm.
Heres an example. Suppose the ciphertext is KNEXYLTCOW. Now suppose
you used two different one-time pad keys, AFTMRDARKF and
DJWMRDARKF. Lets see what happens. Use the Vigenere Table to convert
back to plaintext.
Cipher:
K N E X Y L T C O W
Key:
D J W M R D A R K F
Plaintext: H E I L H I T L E R
Cipher:
K N E X Y L T C O W
Key:
A F T M R D A R K F
Plaintext: K I L L H I T L E R
This means you can make the plaintext say anything you want. But there are two
main weaknessess to this method.
1. Making sure the key is random and of sufficient length.
2. Distributing the keys. How is the receiver suppose to know which key to use?
One way to accompish the distribution of keys is to have an agreed upon book.
Each day the random key starts with the first letter of a page in that book. Keep
reading the letters from the page until its the same length as the message.
Another interesting use of crytography was to send messages through newspapers.
Before the telegraph, people needed to send messages through the mail. Postage
could be expensive, plus someone could intercept the letter. Newspapers traveled
freely and without charge. So someone could publish an article in the newspaper
and put a dot above the letter that was part of the plaintext. For example,
1 INTRODUCTION
10
to
announce
I am
excited
1 INTRODUCTION
11
You might think that the power behind an encryption technique is keeping the
technique secret. This is not true. Kerckhoffs principles (some of them) state
1. The system must be practically indecipherable. In other words, if the data
can be deciphered, it must take a very long time.
1 INTRODUCTION
12
2. The system must not be secret. If you encrypted a message using Caesars
cipher, then your evesdropper Eve must also know that.
3. Any evesdroppers or attackers must be able to get their hands on the
ciphertext.
4. The security of the system should depend solely on the secrecy of the key.
To summarize, a good cryptosystem is one where everyone knows what it is and
how it works, everyone can get their hands on the ciphertext, and its strength lies
in the secrecy of the key.
2 SUBSTITUTION CIPHER
13
Substitution Cipher
2.1
With a Caesar Cipher, you replace each letter in a message with a letter along
the alphabet. A Caesar cipher shifts the alphabet and is therefore also called a
shift cipher. The key is the number of letters you shift. The Caesar Cipher is
named after Julius Caesar, who is said to have used it to send messages to his
generals over 2,000 years ago.
You should have a pdf of a Caesar Shift Wheel. Follow the directions to create
the wheel.
Message: B O W L I N G T O U R N A M E N T T O N I G H T
Cipher:
2 SUBSTITUTION CIPHER
14
Message: T H E S E C R E T P A S S W O R D I S B O O G E R
Cipher:
4. A KEY SPACE is the set of all possible keys. What is the size of the KEY
SPACE for the shift cipher?
2 SUBSTITUTION CIPHER
15
To decrypt a shift cipher, you just reverse the shift. If you shifted RIGHT 3,
then to decrypt the message we shift LEFT 3.
Example: Key shift: -3
Cipher:
PDWK HTXDOV PRQHB
Plaintext : M A T H E Q U A L S M O N E Y
1. Use a key shift of 4, decrypt the following ciphertext:
Ciphertext: G S H I R E Q I: K S S R M I W
Plaintext:
2. Choose your own key shift. Encrypt your name and high school grade level
Plaintext:
Ciphertext:
3. Pass your ciphertext to your neighbor. DO NOT TELL THEM YOUR KEY.
Have your neighbor decrypt your message.
4. Go to
http://www.brianveitch.com/cryptography/caesar_friendly.php. Use
this page to figure out the answer to this riddle. What did the one eye say to
the other?
Ciphertext: TWLOWWF
QGM
Plaintext:
Ciphertext: KGEWLZAFY KEWDDK!
Plaintext:
SFV EW
2 SUBSTITUTION CIPHER
6. Now that you got a chance to play around with the shift cipher, can you
think of any weaknesses in this encryption technique?
16
2 SUBSTITUTION CIPHER
2.2
17
With a Keyword Shift, you place a word underneath the original alphabet. The
Keyletter is the letter where you start your keyword. In this example below, the
keyword is KINGDOM and the keyletter is G
Starting after the letter M, you fill in the rest of the alphabet. If the letter was
already used in the keyword, then you skip it. Also, your keyword cannot have
repeated letters.
KFUPNJS
A D O W N E R!
N H U X B Q A Y F!
2 SUBSTITUTION CIPHER
Message:
18
CRYPTO IS COOL
Ciphertext:
2. Use the keyword: BACON, keyletter A to decrypt the following ciphertext.
Ciphertext: T R Y S T B P J G L E H N J J M T M B T R N N
Plaintext:
3. Go to http://www.brianveitch.com/cryptography/keyword.php. Use
this page to decrypt the following statement.
Keyword: THUNDERCLAP, Keyletter: E
ZPAJ SPK NWJT DJ ONTA SPKGT JTQJDAU WAZ CSDAU PA SPKG
XWYR ONTA SPKG BNPAT ZTYDZTI JP XT W ADAEW, ICDBI
JNGPKUN SPKG HDAUTGI, WAZ WJJWYRI SPKG HWYT!
2 SUBSTITUTION CIPHER
19
4. The following are quotes from famous movies. Each quote has been
encrypted with the same keyword and keyletter. Your job is to decrypt them
and determine its origin. Identify the keyword and keyletter.
Use the letter frequency calculator at
http://www.brianveitch.com/cryptography/relative_frequency.php to
help determine the plaintext.
WSC ZE RCVMTD, IV VEFVC M ISCOR FYMF ZD VEFZCVOK SGC
SIE.
TMK FYV WSCPV NV IZFY KSG.
FYVCVD ES AOMPV OZUV YSTV.
KSG PMEF YMEROV FYV FCGFY!
KSGCV XSEEM EVVR M NZXXVC NSMF.
YVCVD QSYEEK!
TK ACVPZSGD.
KSGC TSFYVC IMD M YMTDFVC MER KSGC WMFYVC DTVOF SW
VORVCNVCCZVD.
Z DYMOO PMOO YZT DBGZDYK. MER YV DYMOO NV TZEV. MER
YV DYMOO NV TK DBGZDYK.
FYVKHV RSEV DFGRZVD, KSG UESI. DZJFK AVCPVEF SW FYV
FZTV, ZF ISCUD VHVCK FZTV.
CMK, EVJF FZTV DSTVSEV MDUD KSG ZW KSGCV M XSR, KSG
DMK KVD!
2 SUBSTITUTION CIPHER
2.3
20
Each letter of the alphabet represents a shift. For example, A = 0 (no shift), B =
1 (shift 1 to the right), C = 2 (shift 2 to the right), D = 3 (shift 3 to the right), E
= 4 (shift 4 to the right), ...
If the keyword is BED, it means you shift the first letter of your cipher by B =
1, the second letter of the cipher by E = 4, third letter by D = 3. Repeat this
pattern. The keyword BED converts to the numeric key 143. Consider the
following example.
You can use your Caesar Shift Wheel to do this. Since you will have to keep
changing the shift for each letter, the wheel will help you do it quickly.
2 SUBSTITUTION CIPHER
21
3 TRANSPOSITION
22
Transposition
There are two basic ways of encrypting a message. One way is through some sort
of substitution, which was covered in the previous section. The other way is
through transposition. Transposition ciphers are formed by changing the
normal position of the letters that make up the message.
3.1
Easy Example
Railfence Cipher
H
E
A
T
T
A
C
K
W
E
A
R
E
O
U
T
N
U
M
B
E
R
E
D
3 TRANSPOSITION
23
(b) Vertical: 2
S O T E T A K E R O T U B R D
T P H A T C W A E U N M E E
2. Next, you would write out the message from left to right like you were
reading a book. The cipher text would be
(a) Vertical 5 by Horizontal 6:
SHAATE TECRNR OAKEUE PTWOMD TTEUB
or (without spaces)
SHAATETECRNROAKEUEPTWOMDTTEUB
(b) Vertical 2 by Horizontal 15:
SOTETAKEROTUBRD TPHATCWAEUNMEE
or (without spaces)
SOTETAKEROTUBRDTPHATCWAEUNMEE
3. You can keep changing the vertical length to get a new cipher for the exact
same message. You just need to agree upon a length with the receiver
beforehand.
4. To decrypt this message, youll need the vertical and horizontal length. Its
possible to do it without, but it will take a lot of trial and error.
3.3
Transposition Activity
Choose a vertical length and encrypt a message. Give your encrypted message to
your partner. Try to decipher your partners message without knowledge of the
vertical and horizontal length. After 5 minutes, if you have not deciphered the
message, have your partner tell you the vertical and horizontal lengths.
4 MODULAR ARITHMETIC
24
Modular Arithmetic
Clock Example
What if we kept counting after 12 oclock? Where would 13 oclock be? Turn the
time back into our normal 12 hour time.
13 =
oclock
17 =
oclock
23 =
oclock
24 =
oclock
4 MODULAR ARITHMETIC
25
means congruent
a is congruent to b mod n if b a is a integer multiple of n
Thats great for a math class, but a bit confusing for your first exposure to mod.
Another way to look at it is,
Informal Definition of a b (mod n)
4 MODULAR ARITHMETIC
26
So when we talk about (mod n), we really only care about the remainder when
you divide by n. Your answer must be a number between
0 and n 1
4.1
Examples of Congruences
1. 27
2. 145
(mod 12)
3. 214
(mod 12)
4. 28
5. 16
(mod 3)
6. 1563
7. 10625
8. 783
(mod 27)
(mod 101) - answer must be between 0 and 100
(mod 27)
If the number is negative, you must add multiples of n until you get a
number between 0 and n
3 5 (mod 8) because -3 + 8 = 5
28 4 (mod 8) because -28 + 8 + 8 + 8 + 8 = 4
9. 8
(mod 12)
10. 15
(mod 4)
11. 126
12. 54
(mod 18)
(mod 13)
4 MODULAR ARITHMETIC
27
4.2
1. 11 + 4
2. 23 17
3. 16 2
4. 4 8
5. 16 3
6. 154 + 27
(mod 12)
(mod 12)
(mod 12)
(mod 5)
(mod 3)
(mod 27)
7. 24
(mod 27)
8. 36
(mod 13)
9. 73 1
10. 94
4.3
(mod 7)
(mod 5)
Multipication Tables
Suppose you multiply each number (mod 5) with another number from (mod 5).
We can create a table to keep track of the results.
* mod 5 0 1 2 3 4
0
0 0 0 0 0
1
0 1 2 3 4
2
0 2 4 1 3
3
0 3 1 4 2
4
0 4 3 2 1
Notice how 2 3 1 (mod 5). This will be important very soon.
4 MODULAR ARITHMETIC
28
Example:
Now you try creating a multiplication table for (mod 4), (mod 7) and (mod 11).
* mod 4
* mod 7
* mod 11
4 MODULAR ARITHMETIC
29
1
1
And is the inverse of 3. So,
3
3
4/3 = 4 31
So whats the inverse of 3 (mod 7)? Recall that the inverse of 3 is a number than
when you multiply it to 3 you get 1.
3x=1
Lets look at an example.
4.4
1. 31 (mod 7)
We need to solve
3x 1 (mod 7)
There are three ways we can do this. Lets first do it by trial and error,
3 1 3 (mod 7)
3 2 6 (mod 7)
3 3 9 2 (mod 7)
3 4 12 5 (mod 7)
3 5 15 1 (mod 7)
4 MODULAR ARITHMETIC
30
3. 71 (mod 11)
4 MODULAR ARITHMETIC
31
Please note that some inverses and divisions do not exist. For example, 21
(mod 4) did not exist. Therefore, you cannot divide by 2 in (mod 4).
However, every number (except 0) in (mod 7) and (mod 11) did have
inverses. There is a reason for this. Take some time to think about it. Try
creating a multipication table for (mod 12) and see what happens.
* mod 12
Example: Find
11
(mod 12)
5
4 MODULAR ARITHMETIC
4.5
32
How did we solve an equation like 3x = 5 from basic algebra? We divided both
sides by 3.
3x = 5
3x 5
=
3
3
x=
5
3
Instead of dividing both sides by 3 (we dont do division like this in (mod n)), we
multiply both sides by the inverse of 3, 31 .
3x 5 (mod 11)
31 3x 31 5 (mod 11)
x 31 5 (mod 11)
So whats 31 (mod 11)?
From the multiplication table, 31 4 (mod 11) since 3 4 1 (mod 11).
Therefore, our solution to 3x 5 (mod 11) is
x 31 5 (mod 11)
x 4 5 (mod 11)
4 MODULAR ARITHMETIC
33
x 9 mod 11
Now lets verify.
3 9 = 27 5 (mod 11)
Your turn.
1. 5x 7 (mod 12)
2. 7x 9 (mod 12)
3. x2 1 (mod 12) - use the multiplication table for (mod 12)
4 MODULAR ARITHMETIC
4.6
34
A very useful tool to use when trying to find the inverse of a number (mod n) is
called the Extended Euclidean Algorithm. As you probably figured out by now, a
number will have an inverse (mod n) as long as that number is relativly prime to
n.
This method requires the division algorithm on the left hand side, and a recursive
formula on the right. Please follow the example below.
We need the recursive formula: pi = pi2 pi1 qi2 where p0 = 0 and p1 = 1.
When you move onto the next step, you use ai = bi1 and bi = ri1
3 d 1 (mod 20)
ai =
qi bi + ri
Step 0: 20 = (6) 3 + 2
p0 = 0
Step 1:
3 = (1) 2 + 1
p1 = 1
Step 2:
2 = (2) 1 + 0
p2 = p0 p1 q0 = 0 1(6) = 6
p3 = p1 p2 q1 = 1 (6) 1 = 7
4 MODULAR ARITHMETIC
ai =
35
qi bi + ri
Step 0: 40 = (5) 7 + 5
p0 = 0
Step 1:
7 = (1) 5 + 2
p1 = 1
Step 2:
5 = (2) 2 + 1
p2 = p0 p1 q0 = 0 1(5) = 5
Step 3:
2 = (2) 1 + 0
p3 = p1 p2 q1 = 1 (5)(1) = 6
p4 = p2 p3 q2 = 5 6 2 = 17
9 d 1 (mod 16)
Youll find a table on the next page to complete the Extended Euclidean
Algorithm.
4 MODULAR ARITHMETIC
ai
36
= qi bi + ri
Step 0:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Your inverse is the last pi value in the table. Make sure its (mod n).
The inverse of 91 (mod 16) is
91
(mod 16)
37
5.1
What is it?
Suppose Alice sends Bob an encrypted message using the Vigenere Cipher. Bob
and Alice need to also agree upon the key, otherwise Bob would have to decipher
Alices message the hard way. If Eve, our evesdropper, is aware of the key then
she can decipher all of Alices messages to Bob. Diffie-Hellman Key
Exchange allows the sender, Alice, and the receiver, Bob, to come up with a key
with public information. The upside to this method is Eve, our evesdropper, is
aware of the exchange the whole time but will have a difficult time cracking it
and finding the secret key. Heres how it works.
5.2
The Algorithm
1. Alice and Bob agree, publicly, on a prime number P , and another number N ,
where N < P . Everyone, including Eve, will know these two numbers.
2. Alice chooses her own personal (private) number A. She does not tell anyone
this number. This number should be relatively prime to N .
3. Bob chooses his own personal (private) number B. He does not tell anyone
this number. This number should be relatively prime to N .
4. Alice calculates the number
S = N A (mod P )
and sends this number S to Bob.
5. Bob calculates
R = N B (mod P )
and sends this number R to Alice.
6. So now Bob and Alice have two numbers. Eve, who is watching this
exchange, is fully aware of the numbers S and R. But since she doesnt know
A and B, she doesnt know how Alice and Bob calculated the R and S.
7. Alice takes the number she received from Bob and calculates
C = RA (mod P )
and Bob takes the number he received from Alice and calculates
C = S B (mod P )
8. C is their shared key. Since the last step is done in private, Eve does not
know C.
5.3
38
39
5.4
In the previous example, here is all the information Eve has during this key
exchange.
P = 983, N = 20, , S = 312, and R = 942
In order for Eve, our evesdropper, to determine the keyword, she needs to solve
the following equations
312 20A (mod 983)
or
942 20B (mod 983)
This is difficult to do, especially when P and N are extremely large. Our example,
they arent large. But even for us, our best bet is probably trial and error. We
would try to plug in all the values (mod 983) for A and B and see which work.
5.5
40
Get in groups of 3. One of you will be Alice, another will be Bob, and the third
will be Alice. In this activity, Alice and Bob will come up with their Vigenere
Cipher key right in front of Eve.
1. Alice writes a message. It doesnt matter how long since you will be using
the online Vigenere Cipher. You want to send this message to Bob. Do not
show this message to anyone.
2. Alice and Bob now speak to each other publicly. Eve is there listening and
taking notes. Alice and Bob agree on two numbers. Choose a prime number
P and another number N less than P . You may use the website
http://www.brianveitch.com/cryptography/prime_generator to
generate the prime number.
P =
and N =
3. Alice and Bob choose their private number. If youre Alice write it below
A=
and calculate
NA
(mod P)
(mod P)
4. Alice and Bob exchange numbers. Eve receives these numbers as well.
5. Alice and Bob finish the calculations to come with the key. Alice uses the
Vigenere Cipher at
http://www.brianveitch.com/cryptography/vigenere.php to create the
ciphertext.
41
6 RSA ENCRYPTION
6
6.1
42
RSA Encryption
Introduction
I can think of two uses for RSA encryption that should make sense to us. In the
last chapter, we discussed a key exhange. This is a method for Alice and Bob to
commuicate publicly to create a secret key. If done correctly, Eve should have a
very hard time figuring this key out.
RSA encryption can also be used to send the key. Its very similar to the
Diffie-Hellman Key Exhange. RSA is called public key encryption (or
asymmetric-key encryption). It means that there is a public key to encrypt the
data and a secret key (only the receiver of the message knows) that will decrypt
the data. So Alice chooses the keyword for the Vigenere Cipher. She uses the
public key to encrypt her keyword and sends the ciphertext to Bob. Bob uses his
secret key, known only to him, to decrypt it. With the keyword now agreed upon,
Alice can use a different method to encrypt the data and send it to Bob.
Another use of RSA encryption is called Authentication. Suppose Bob recevies
a message, supposedly from Alice. How does he actually know this message is
from Alice? Its possible Eve intercepted the message and created her own to
send to Bob.
6.2
ATTACK NOW
Pre-Cipher: 0 19 19 0 2 10 13 14 22
6 RSA ENCRYPTION
43
A:
T:
T:
A:
C:
K:
N:
O:
W:
03 0 (mod 33)
193 = 6859 28 (mod 33)
193 = 6859 28 (mod 33)
03 0 (mod 33)
23 8 (mod 33)
103 = 1000 10 (mod 33)
133 = 2197 19 (mod 33)
143 = 2744 5 (mod 33)
223 = 10648 22 (mod 33)
Message: A T T A C K N O W
Cipher:
0 28 28 0 8 10 19 5 22
Everyone has access to the public key. Only the receiver of the encrypted data
has the private key. This is called asymmetric key encryption. It means there is
one key to encrypt and a completely different key to decrypt.
The private key for our example is d = 7. Lets see what happens when we raise
our ciphertext numbers to the power of d = 7.
07 0
287 19
287 19
07 0
87 2
107 10
197 13
57 14
227 22
(mod
(mod
(mod
(mod
(mod
(mod
(mod
(mod
(mod
33)
33)
33)
33)
33)
33)
33)
33)
33)
6 RSA ENCRYPTION
44
ATTACK NOW
0 19 19 0 2 10 13 14 22
Instead of enrypting 0, 19, 19, 0, 2, 10, 13, 14, and 22, we can regroup the
numbers. One example would be,
01, 91, 90, 21, 01, 31, 42, 2
6 RSA ENCRYPTION
45
The problem is our modulus is 33, which is too small (since we have numbers
larger than 33 - 91, 90, 42) We need to choose a larger modulus and figure out a
new public and private key. Lets figure out how to do this now.
6.3
RSA Algorithm
Suppose Alice wants to send Bob a message. She uses Bobs public key (e, n).
Lets see how Bob came up with his public/private key by coming up with our
own public and private key.
1. Bob chooses two large primes. Call them p and q. He must keep these
secret!
Now you choose two primes (preferably between 10 and 20).
p=
q=
4. Bob chooses an integer e, 1 < e < (n) where e does not share any factors
with (n). This is called copime (or relatively prime).
Example: 8 and 15 are coprime because they share no factors.
Example: 9 and 24 are NOT coprime because they share a common factor 3.
6 RSA ENCRYPTION
46
e=
5. Bob now has his public key (e, n). Your public key is
(e, n) = (
6. Bob then computes his private key d. To find d, you must solve the following
equation
e d 1 (mod n)
Your equation is
d 1 (mod
Lets go over how Bob found d to the following equation. Then follow the
pattern. This may take a few tries to get right.
6.4
6 RSA ENCRYPTION
ai =
47
qi bi + ri
Step 0: 20 = (6) 3 + 2
p0 = 0
Step 1:
3 = (1) 2 + 1
p1 = 1
Step 2:
2 = (2) 1 + 0
p2 = p0 p1 q0 = 0 1(6) = 6
p3 = p1 p2 q1 = 1 (6) 1 = 7
Your turn!
d 1 (mod
ai
Step 0:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
= qi bi + ri
6 RSA ENCRYPTION
48
Your d is the last pi value in the table. Make sure its (mod n).
Your private key is
d=
E Q U A L S
M O N E Y
6 RSA ENCRYPTION
M:
A:
T:
H:
E:
Q:
U:
A:
L:
S:
M:
O:
N:
E:
Y:
49
(mod
(mod
(mod
(mod
(mod
(mod
(mod
(mod
(mod
(mod
(mod
(mod
(mod
(mod
(mod
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
To practice RSA encryption, work with a partner. Write a short message (no
more than 20 letters) and encrypt it using your partners public key. Do not tell
your partner your plaintext message. Give them only the ciphertext.
Your partner then decrypts the ciphertext using his or her private key to reveal
your message. Remember, to decrypt the message do the following
N C d (mod n)
Using the website
http://www.brianveitch.com/cryptography/generate_rsa_keys.php,
genereate your own public and private key.
Exchange public keys with your partner. Use your partners public key to encrypt
a word (at most 7 letters). Have your partner decrypt the word. Since the
modular exponentiation will be large, use the following websites to help
6 RSA ENCRYPTION
50
http://www.brianveitch.com/cryptography/modexp.php
and
http://www.brianveitch.com/cryptography/modinverse.php
ASCII Table
6 RSA ENCRYPTION
51
After converting the characters to their corresponding ASCII numbers, we get the
following,
Message:
Plaintext: 65 84 84 65 67 75 32 78 79 87
Lets use the public key (e = 709, n = 923). To make this harder to crack, we
regroup the numbers into blocks of 3. There is nothing special about the size of
the block. You just have to make sure the numbers youre encrypting are less
than n = 923.
658|484|656|775|327|879|87
Since the numbers will get large, use the calculator at
http://www.brianveitch.com/cryptography/modexp.php
658709
484709
656709
775709
327709
879709
87709
(mod
(mod
(mod
(mod
(mod
(mod
(mod
923)
923)
923)
923)
923)
923)
923)
6 RSA ENCRYPTION
52
Suppose youre Eve and you come across this encrypted message. You know the
method used and you know Bobs public key (709, 923). How do we crack it?
6.6
To decrypt the ciphertext, we need the private key d. So lets try to crack it.
1. Factor n = 923 into two prime numbers p and q.
n=
2. Calculte (n) = (p 1) (q 1)
(923) =
6 RSA ENCRYPTION
53
(mod 923)
(mod 923)
(mod 923)
(mod 923
(mod 923)
(mod 923)
(mod 923)
6.7
You are Eve and you intercept a message sent from Alice to Bob. It was
encrypted using Bobs public key (n = 1224825487, e = 1118107197).
The ciphertext is
71218284 237148343 889555017 473479370 649324334 6660643 260396109
637251342 924334351 935096349 271621588 366934580 271621588 576453518
423052470 618017834 332813753 397822860
54
To crack the code, find the private key d. Follow the method we used to crack the
RSA example from before. Start by factoring n.
You can decrypt it using the private key d using the website
http://www.brianveitch.com/cryptography/modexp.php. If youre doing it by
hand, the original block size (before encryption) was 4.
or the website http://www.brianveitch.com/cryptography/rsa.php. Use a
block size of 2 on this website.
There you go. This is the general idea behind the RSA encryption. Now before
we finish with our RSA introduction, Id like to show you the size of a typical
modulus n.
16152174667064029642647365822885998430666314431815268152405470907824573
65903662972483772980826569393306732864932303362619914669385966910731129
68626710792148904239628873374506302653492009810626437582587089465395941
37549600473991849827667633423824146549803003658606392990236819200423317
2032080188726965600617167
Can you see why trying to factor n to crack RSA is hard? Even if you can factor
it, you still need to find the private key d.
7.1
Algorithm
As noted earlier, how does Bob actually know the message is sent from Alice.
Heres what she does.
1. Alice pubicly announces her RSA public key, say (n = 2263, e = 917).
2. She converts her name to ASCII numbers: 65 76 73 67 69. She then encrypts
her name with her private key d = 1133. Remember that only Alice knows
the private key. You may need the calculator at
http://www.brianveitch.com/cryptography/modexp.php
651133
761133
731133
671133
691133
55
3. She sends her encrypted name along with the encrypted message meant for
Bob.
ALICE Encrypted: 228 1630 1314 645 1405
4. Bob receives two ciphers. One is Alices encrypted name. The other is the
actual encrypted message. Bob decrypts her message, but isnt convinced it
was sent from Alice.
5. Bob uses Alices public key, which is accessible by everyone, to decrypt the
name.
228917
1630917
1314917
645917
1405917
65
76
73
67
69
(mod
(mod
(mod
(mod
(mod
2263)
2263)
2263)
2263)
2263)
65 76 73 67 69 ALICE
6. Since Alice sent her name by encrypting it with her private key, no one can
fake this. The only way Eve can fake her name is by encrypting ALICE
with her private key. And the only way Eve can do that is by cracking her
RSA public key.
7.2
56
Authentication Activity
1. Get in groups of 3. One person is Alice, another is Bob, and the third is
Alice.
2. Alice needs to generate a public key. She also needs her find her private key.
She can do this by hand or use the RSA Key Generator at
http://www.brianveitch.com/cryptography/generate_rsa_keys. Alice
will create a message and encrypt it. She may use any method she wants.
3. Eve will create her own message (something that may contradict Alices
message).
Example:
(a) Alice may encrypt the message ATTACK AT DAWN using the Caesar
Cipher.
(b) Eve may encrypt DO NOT ATTACK AT DAWN using the Caesar
Cipher.
4. Using Alices public key, she encrypts her name by using her private key.
5. Eve will also encrypt Alices name, but she must guess on the private key.
6. Both Alice and Eve send Bob their messages with the encrypted name.
7. Bob decrypts both message. Not sure which message came from Alice, use
Alices public key to decrypt the names.
8. Unless Eve guessed correctly on Alices private key, Alices encrypted name
should decrypt back to Alice and Eves should decrypt to jibberish.
8 IMPORTANT INFORMATION
8
8.1
Important Information
Caesar Shift Wheel
57
8 IMPORTANT INFORMATION
8.2
58
8 IMPORTANT INFORMATION
8.3
Vigeneres Table
59
8 IMPORTANT INFORMATION
8.4
ASCII Table
8.5
Useful Websites
60
8 IMPORTANT INFORMATION
Vigenere Ciphere
http://www.brianveitch.com/cryptography/vigenere.php
Letter Frequency
http://www.brianveitch.com/cryptography/relative_frequency.php
Modular Exponentiation
http://www.brianveitch.com/cryptography/modexp.php
Modular Inverse
http://www.brianveitch.com/cryptography/modinverse.php
String to ASCII
http://www.brianveitch.com/cryptography/string_to_number.php
ASCII to String
http://www.brianveitch.com/cryptography/number_to_string.php
Easier RSA - No Blocking Before Encrypting
http://www.brianveitch.com/cryptography/rsa_easy.php
RSA - Blocking Before Encrypting
http://www.brianveitch.com/cryptography/rsa.php
Generate RSA Public and Private Keys
http://www.brianveitch.com/cryptography/generate_rsa_keys.php
Generate Prime Numbers
http://www.brianveitch.com/cryptography/generate_primes.php
61