Top Banner
Introduction to Cryptography Brian Veitch July 2, 2013 Contents 1 Introduction 3 1.1 Alice, Bob, and Eve ........................... 3 1.2 Basic Terminology ........................... 4 1.3 Brief History of Cryptography ..................... 4 1.4 Kerckhoff’s Principles of Cryptographic Secuity ........... 11 2 Substitution Cipher 13 2.1 Caesar Cipher Activity ......................... 13 2.2 Keyword Cipher Activity ........................ 17 2.3 Vigenere Cipher Activity ........................ 20 3 Transposition 22 3.1 Easy Example .............................. 22 3.2 Railfence Cipher ............................ 22 3.3 Transposition Activity ......................... 23 4 Modular Arithmetic 24 4.1 Examples of Congruences ....................... 26 4.2 Arithmetic with Congruences ..................... 27 4.3 Multipication Tables .......................... 27 4.4 Finding the Inverse ........................... 29 4.5 Solving Equations (mod n) ....................... 32 4.6 The Extended Euclidean Algorithm .................. 34 1
61

Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

Sep 30, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

Introduction to Cryptography

Brian Veitch

July 2, 2013

Contents

1 Introduction 31.1 Alice, Bob, and Eve . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.2 Basic Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.3 Brief History of Cryptography . . . . . . . . . . . . . . . . . . . . . 41.4 Kerckhoff’s Principles of Cryptographic Secuity . . . . . . . . . . . 11

2 Substitution Cipher 132.1 Caesar Cipher Activity . . . . . . . . . . . . . . . . . . . . . . . . . 132.2 Keyword Cipher Activity . . . . . . . . . . . . . . . . . . . . . . . . 172.3 Vigenere Cipher Activity . . . . . . . . . . . . . . . . . . . . . . . . 20

3 Transposition 223.1 Easy Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223.2 Railfence Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223.3 Transposition Activity . . . . . . . . . . . . . . . . . . . . . . . . . 23

4 Modular Arithmetic 244.1 Examples of Congruences . . . . . . . . . . . . . . . . . . . . . . . 264.2 Arithmetic with Congruences . . . . . . . . . . . . . . . . . . . . . 274.3 Multipication Tables . . . . . . . . . . . . . . . . . . . . . . . . . . 274.4 Finding the Inverse . . . . . . . . . . . . . . . . . . . . . . . . . . . 294.5 Solving Equations (mod n) . . . . . . . . . . . . . . . . . . . . . . . 324.6 The Extended Euclidean Algorithm . . . . . . . . . . . . . . . . . . 34

1

Page 2: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

CONTENTS 2

5 Diffie-Hellman Key Exhange 375.1 What is it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375.2 The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375.3 Example of a Key Exhange . . . . . . . . . . . . . . . . . . . . . . 385.4 Why is Cracking this Exchange Difficult . . . . . . . . . . . . . . . 395.5 Key Exhange Activity . . . . . . . . . . . . . . . . . . . . . . . . . 40

6 RSA Encryption 426.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426.2 Example of RSA Encryption . . . . . . . . . . . . . . . . . . . . . . 426.3 RSA Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456.4 Using The Extended Euclidean Algorithm . . . . . . . . . . . . . . 466.5 ASCII Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506.6 Cracking RSA by finding the private key d. . . . . . . . . . . . . . 526.7 Your turn to crack an RSA encrypted message . . . . . . . . . . . . 53

7 Using RSA for Authentication 547.1 Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547.2 Authentication Activity . . . . . . . . . . . . . . . . . . . . . . . . 56

8 Important Information 578.1 Caesar Shift Wheel . . . . . . . . . . . . . . . . . . . . . . . . . . . 578.2 Letter Frequency Tables . . . . . . . . . . . . . . . . . . . . . . . . 588.3 Vigenere’s Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598.4 ASCII Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608.5 Useful Websites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

Page 3: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

1 INTRODUCTION 3

1 Introduction

You don’t have to be an expert in cryptogrphy to appreciate the field.Cryptography is all around us. We use it to store senstive data, send secretmessages, send financial information over the internet, hide messages fromparents, etc. The methods we will cover in this paper can be easily picked upwith some knowledge of basic arithmetic and some modular algebra.

There are two basic types of encryption: substitution and transposition. We willcover the basics of each with some easy examples. Since this is an introduction,the techniques will be easier. After reading a section, try to improve on themethod by coming up with your own versions.

1.1 Alice, Bob, and Eve

Suppose Alice wants to send Bob a message. She doesn’t want Eve to intercept itand read it, so she decides to disguise it. Here’s a diagram that outlines thewhat’s happening.

Your hope is that Eve doesn’t know how to figure out your message or alter it insome way.

Page 4: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

1 INTRODUCTION 4

1.2 Basic Terminology

1. Plaintext - this is the original message to be sent. It can letters, numbers,characters, symbols, etc.

2. Encryption - disgusing the plaintext using some method. These methodsusually have a key or algorithm to generate the ciphertext.

3. Ciphertext - the result of the encryption. It can be letters, numbers,characters, etc., but unreadable.

4. Decryption - the process of undoing the encryption. You should get theoriginal plaintext when finished.

5. Key - something that is used to encrypt the message. Think of it as apassword that disguises and un-disguises your message.

6. Keyspace - the set of all possible keys. For example, a four digit lock has10000 possible keys. (0000, 0001, 0002, ..., 9998, 9999)

7. Cryptography - the science of the enciphering and deciphering of messages insecret code or cipher. Also referred to as cryptology.

8. Cryptosystem - a system for encrypting information.

9. Cryptanalysis - the science (and art) of recovering information from cipherswithout knowledge of the key.

1.3 Brief History of Cryptography

Cryptography is an ancient science. The term comes from the Greek languagemeaning ”the study of hidden or secret writing.” Ever since there were multipletribes (or countries), we needed a way to communicate to one another withoutthe other tribe (country) understanding it. Today it’s vital to communicatations,authentication, sending sensative data over the internet, purchasing items from

Page 5: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

1 INTRODUCTION 5

eBay, Amazon, and more.

One of the earliest examples of cryptography was in Egypt around 2000 B.C.where they used hieroglyphics to decorate their tombs. Even though they weren’ttrying to completely hide the meaning, it still wasn’t easy to interpret.

The Greek writer Polybius used a 5x5 or a 6x6 (with out alphabet) to disguisethe message. Below is an example of how it would work,

If you wanted to write the message ”SAVE ME”, it would be disguised as

S - A - V - E - M - E

41-11-44-15-31-15

Another Greek tool for disguising a message was called the Scytale. It is a toolused to perform a transposition cipher, consisting of a cylinder with a strip ofparchment wound around it on which is written a message. In other words, it wasa way to rearrange the letters.

You would wrap a long thin parchment around a rod. You then write yourmessage across the rod, ”SEND HELP!”

Page 6: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

1 INTRODUCTION 6

When you unwrap the parchment the letters appear rearranged. To understandthe 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. Weknow call it the Caesar Cipher. If he had anything important to send, he wouldwrite out his message and then replace each letter of his message with anotherletter down the alphabet. So A = D, B = E, C = F, D = G, etc. When it neededto be read, the receiver of the message would reverse this process, changing Dback to A, E back to B, F back to C. We will go over this method in great detailin the next chapter. The tool we will use will look something like this,

Page 7: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

1 INTRODUCTION 7

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 discussthe Keyword Cipher, which is another form of substitution.

The Vigenere cipher was a substitution cipher where each letter of the messageused 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 by3, the fourth letter by 5, the fifth letter by 3, and so on. This particular patternwould be two Caesar shifts with 3 and 5. For 300 years it was believed to beindecipherable. It can now be broken by determining the number of differentcaesar shifts and then do a frequency analysis on the letters that use the sameCaesar shift.

To give you an idea, suppose your keyword is ”DOG” and you want to disguisethe message ”HELP ME”, you use the Vigenere Table below to determine theciphertext.

Page 8: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

1 INTRODUCTION 8

Message: H E L P M EKeyword: D O G D O GCipher: K S R S A K

Why was this cipher so strong? Consider the ciphertext. There are two ’K’s buteach corresonds to a different letter in the plaintext. One ’K’ decrypts back to Hwhile the other ’K’ decrypts to E.

In 1917 U.S. Army Major Joseph Mauborgne developed a technique known as theOne-Time Pad. It works the same way as a Vigenere Cipher. The only

Page 9: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

1 INTRODUCTION 9

difference is the keyword is a random string of letters that is the same length asthe message. That way the keyword never repeats which makes frequencyanalysis useless. If this method is done correctly, it is the only provablyunbreakable encryption algorithm.

Here’s an example. Suppose the ciphertext is ”KNEXYLTCOW.” Now supposeyou used two different one-time pad keys, ”AFTMRDARKF” and”DJWMRDARKF”. Let’s see what happens. Use the Vigenere Table to convertback to plaintext.

Cipher: K N E X Y L T C O WKey: D J W M R D A R K FPlaintext: H E I L H I T L E R

Cipher: K N E X Y L T C O WKey: A F T M R D A R K FPlaintext: K I L L H I T L E R

This means you can make the plaintext say anything you want. But there are twomain 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. Keepreading the letters from the page until it’s 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. Postagecould be expensive, plus someone could intercept the letter. Newspapers traveledfreely and without charge. So someone could publish an article in the newspaperand put a dot above the letter that was part of the plaintext. For example,

Page 10: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

1 INTRODUCTION 10

I am excited to announce the wedding of my brother. He is back from

his journey overseas and is looking forward to a new life with his beautiful bride.

Excluding i’s, which would have two dots, the letters with dots above them spell

attack

World War II

The Germans adopted the use of a family of electro-mechanical encryptiondevices invented by the German engineer Arthur Scherbuis. They became knownas the Enigma Machines, and were extremely effective during the war.

The details about how the Engima machine works is a bit complicated. TheEnigma looks roughly like a typewriter.

Here’s an excerpt from www.bbc.co.uk/history/tpics/enigma

”Enigma allowed an operator to type in a message, then scramble it byusing three to five notched wheels, which displayed different letters ofthe alphabet. The receiver needed to know the exact settings of theserotors in order to reconstitute the coded text. Over the years the basicmachines became more complicated as German code experts addedplugs with electronic circuits.”

When a key was pressed, a set of electrical contacts in the disks would complete acircuit lighting up the ciphertext letter. The disks would rotate after each letter ispressed providing a different encryption.

In 1932, Polish cryptologists broke the Enigma by reverse engineering themachine. As Germany added more complexity to their machine, the initial

Page 11: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

1 INTRODUCTION 11

decryption techniques became unsuccessful. Poland presented their initialdecryption technqiues to the British military intelligence. The cryptanalysts atBletchley Park used this information to greatly increase their success ofdecrypting Enigma.

Modern Era

In 1972, the U.S. identified a need for a government-wide data encryptionstandard. They went with the encryption technique designed by cryptographersfrom IBM. It became known as DES. AES, the advanced encrpytion standardreplaced DES in 2001.

In 1977, the RSA algorithm was presented by its creators Ron Rivest, AdiShamir, and Leonard Adleman from MIT. With many of the classicalcryptosystems, the key used to encrypt a message was also used to decrypt themessage. Suppose 300 people send financial information to a bank. Each personcould encrypt it and have the bank decrypt it. This would, however, require thebank to know 300 separate keys. Not only is it inefficient, how would the bankknow each person’s key?

RSA is called public key encrpytion (asymmetric key encription). To encrypt amessage, someone uses a public key known to everyone to encrypt their data.Only the receiver of the message knows the private key to decipher the message.This means 300 people can send their personal info using the bank’s public key toencrypt it. Only the bank can decrypt it with their private key. We will discussmore of RSA later on.

Now this was only a taste of the more famous works in cryptography. Moreinformation can be found through reserach in your libraries or online.

1.4 Kerckhoff’s Principles of Cryptographic Secuity

You might think that the power behind an encryption technique is keeping thetechnique secret. This is not true. Kerckhoff’s principles (some of them) state

1. The system must be practically indecipherable. In other words, if the datacan be deciphered, it must take a very long time.

Page 12: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

1 INTRODUCTION 12

2. The system must not be secret. If you encrypted a message using Caesar’scipher, then your evesdropper Eve must also know that.

3. Any evesdroppers or attackers must be able to get their hands on theciphertext.

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 andhow it works, everyone can get their hands on the ciphertext, and its strength liesin the secrecy of the key.

Page 13: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

2 SUBSTITUTION CIPHER 13

2 Substitution Cipher

2.1 Caesar Cipher Activity

With a Caesar Cipher, you replace each letter in a message with a letter alongthe alphabet. A Caesar cipher shifts the alphabet and is therefore also called ashift cipher. The key is the number of letters you shift. The Caesar Cipher isnamed after Julius Caesar, who is said to have used it to send messages to hisgenerals over 2,000 years ago.

You should have a pdf of a Caesar Shift Wheel. Follow the directions to createthe wheel.

Example: Key: Shift 3

Message: M A T H E Q U A L S M O N E YCipher: P D W K H T X D O V P R Q H B

1. Use a key shift of 7, encrypt the following message:

Message: W E L C O M E T O M A T H C A M P

Cipher:

2. Use a key shift of 13, encrypt the following message:

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:

Page 14: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

2 SUBSTITUTION CIPHER 14

3. Use a key shift of 20, encrypt the following message:

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 KEYSPACE for the shift cipher?

Page 15: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

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: P D W K H T X D O V P R Q H BPlaintext : 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 tohttp://www.brianveitch.com/cryptography/caesar_friendly.php. Usethis page to figure out the answer to this riddle. What did the one eye say tothe other?

Ciphertext: TWLOWWF QGM SFV EW

Plaintext:

Ciphertext: KGEWLZAFY KEWDDK!

Plaintext:

Page 16: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

2 SUBSTITUTION CIPHER 16

5. Why can’t you tell an egg a joke? Key = 23

Ciphertext: FQ JFDEQ ZOXZH RM.

Plaintext:

6. Now that you got a chance to play around with the shift cipher, can youthink of any weaknesses in this encryption technique?

Page 17: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

2 SUBSTITUTION CIPHER 17

2.2 Keyword Cipher Activity

With a Keyword Shift, you place a word underneath the original alphabet. TheKeyletter is the letter where you start your keyword. In this example below, thekeyword is KINGDOM and the keyletter is G

Starting after the letter M, you fill in the rest of the alphabet. If the letter wasalready used in the keyword, then you skip it. Also, your keyword cannot haverepeated letters.

Example: With the keyword: KINGDOM and keyletter M

Message: G R A V I T Y I S A D O W N E R!

Cipher: K F U P N J S N H U X B Q A Y F!

1. Use the keyword: BACON, keyletter: C

Page 18: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

2 SUBSTITUTION CIPHER 18

Message: C R Y P T O I S C O O L

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. Usethis page to decrypt the following statement.

Keyword: THUNDERCLAP, Keyletter: E

ZPA’J SPK NWJT DJ ONTA SPK’GT JTQJDAU WAZ CSDAU PA SPKGXWYR ONTA SPKG BNPAT ZTYDZTI JP XT W ADAEW, ICDBIJNGPKUN SPKG HDAUTGI, WAZ WJJWYRI SPKG HWYT!

Page 19: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

2 SUBSTITUTION CIPHER 19

4. The following are quotes from famous movies. Each quote has beenencrypted with the same keyword and keyletter. Your job is to decrypt themand determine its origin. Identify the keyword and keyletter.

Use the letter frequency calculator athttp://www.brianveitch.com/cryptography/relative_frequency.php tohelp determine the plaintext.

WSC ZE RCVMTD, IV VEFVC M ISCOR FYMF ZD VEFZCVOK SGCSIE.

TMK FYV WSCPV NV IZFY KSG.

FYVCV’D ES AOMPV OZUV YSTV.

KSG PME’F YMEROV FYV FCGFY!

KSG’CV XSEEM EVVR M NZXXVC NSMF.

YVCV’D QSYEEK!

TK ACVPZSGD.

KSGC TSFYVC IMD M YMTDFVC MER KSGC WMFYVC DTVOF SWVORVCNVCCZVD.

Z DYMOO PMOO YZT DBGZDYK. MER YV DYMOO NV TZEV. MERYV DYMOO NV TK DBGZDYK.

FYVKHV RSEV DFGRZVD, KSG UESI. DZJFK AVCPVEF SW FYVFZTV, ZF ISCUD VHVCK FZTV.

CMK, EVJF FZTV DSTVSEV MDUD KSG ZW KSGCV M XSR, KSGDMK KVD!

Page 20: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

2 SUBSTITUTION CIPHER 20

2.3 Vigenere Cipher Activity

The Vigenre Cipher is a method of encrypting alphabetic text by using a seriesof different Caesar ciphers based on the letters of a keyword. Instead of shiftingevery letter of your plaintext by C = 2, each letter of the plaintext will have adifferent shift.

Though the cipher is easy to understand and implement, for threecenturies it resisted all attempts to break it; this earned it thedescription le chiffre indchiffrable (French for ’the indecipherablecipher’) (wikipedia).

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 thispattern. The keyword ’BED’ converts to the numeric key 143. Consider thefollowing example.

You can use your Caesar Shift Wheel to do this. Since you will have to keepchanging the shift for each letter, the wheel will help you do it quickly.

Example: Using the keyword: NEMO, encrypt the following message.

NEMO = (corresponding numeric key)

Page 21: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

2 SUBSTITUTION CIPHER 21

Example: Using the keyword: CRYPTO, encrypt the following message.

CRYPTO = (corresponding numeric key)

Decrypting the Ciphertext

Decrypting a Vigenere message requires you to reverse the shift from thekeyword. You would line up your ciphertext with the numeric key and reverse theshift, just like you reversed the shift with the Caesar Cipher.

Example: Using the keyword: AVENGERS, decrypt the following message.

Decryping by hand can be tedious. Go tohttp://www.brianveitch.com/cryptography/vigenere.php. This allows youto encrypt a message with the Vigenere keyword and also decrypt messages veryquickly.

Page 22: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

3 TRANSPOSITION 22

3 Transposition

There are two basic ways of encrypting a message. One way is through some sortof substitution, which was covered in the previous section. The other way isthrough transposition. Transposition ciphers are formed by changing thenormal position of the letters that make up the message.

3.1 Easy Example

A common transposition can be simply writing out the letters of a wordbackwards.

ATTACK AT DAWN

translates intoKCATTA TA NWAD

You could also read the entire message right to left, which would encrypt to

NWAD TA KCATTA

3.2 Railfence Cipher

Suppose you have the message ”STOP THE ATTACK WE AREOUTNUMBERED”

1. Write the message vertical. You must choose how many letters you go down.When you reach the bottom of the column, you start the next column.

Some examples,

(a) Vertical: 5

S H A A T ET E C R N RO A K E U EP T W O M DT T E U B

Page 23: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

3 TRANSPOSITION 23

(b) Vertical: 2

S O T E T A K E R O T U B R DT 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 werereading 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 exactsame message. You just need to agree upon a length with the receiverbeforehand.

4. To decrypt this message, you’ll need the vertical and horizontal length. It’spossible 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 toyour partner. Try to decipher your partner’s message without knowledge of thevertical and horizontal length. After 5 minutes, if you have not deciphered themessage, have your partner tell you the vertical and horizontal lengths.

Page 24: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

4 MODULAR ARITHMETIC 24

4 Modular Arithmetic

Clock Example

What if we kept counting after 12 o’clock? Where would 13 o’clock be? Turn thetime back into our normal 12 hour time.

13 = o’clock

17 = o’clock

23 = o’clock

24 = o’clock

The 24 hour clock face:

Page 25: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

4 MODULAR ARITHMETIC 25

With modular arithmetic, we would use the following notation,

13 ≡ 1 (mod 12)

17 ≡ 5 (mod 12)

23 ≡ 11 (mod 12)

24 ≡ 0 (mod 12)

Formal Definition of a ≡ b (mod n)

≡ means congruent

a is congruent to b mod n if b− a is a integer multiple of n

That’s 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)

b is the remainder when you divide a by n

Examples:

13÷ 12 = 1 R 1

17÷ 12 = 1 R 5

23÷ 12 = 1 R 11

24÷ 12 = 2 R 0

Page 26: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

4 MODULAR ARITHMETIC 26

So when we talk about (mod n), we really only care about the remainder whenyou divide by n. Your answer must be a number between

0 and n− 1

4.1 Examples of Congruences

1. 27 ≡ (mod 12) - answer must be between 0 and 11

2. 145 ≡ (mod 12)

3. 214 ≡ (mod 12)

4. 28 ≡ (mod 5) - answer must be between 0 and 4

5. 16 ≡ (mod 3)

6. 1563 ≡ (mod 27)

7. 10625 ≡ (mod 101) - answer must be between 0 and 100

8. 783 ≡ (mod 27)

If the number is negative, you must add multiples of n until you get anumber 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 ≡ (mod 18)

12. −54 ≡ (mod 13)

Page 27: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

4 MODULAR ARITHMETIC 27

We can do addition, subtraction, multipication, and exponentiation exactly thesame way. Give them a try,

4.2 Arithmetic with Congruences

1. 11 + 4 ≡ (mod 12)

2. 23− 17 ≡ (mod 12)

3. 16− 2 ≡ (mod 12)

4. 4 · 8 ≡ (mod 5)

5. 16 · 3 ≡ (mod 3)

6. 154 + 27 ≡ (mod 27)

7. 24 ≡ (mod 27)

8. 36 ≡ (mod 13)

9. 73 − 1 ≡ (mod 7)

10. 94 ≡ (mod 5)

4.3 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 01 0 1 2 3 42 0 2 4 1 33 0 3 1 4 24 0 4 3 2 1

Notice how 2 · 3 ≡ 1 (mod 5). This will be important very soon.

Page 28: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

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

Page 29: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

4 MODULAR ARITHMETIC 29

Division is very different. What is 4/3 (mod 7)?

Division like this doesn’t make sense. When we added, subtracted, and multipliedwe still got an integer. We then divided that integer by n. But 4/3 is not aninteger!

You might recall that 4/3 is the same thing as 4 · 13

And1

3is the inverse of 3. So,

if we want to compute 4/3 we need to do,

4/3 = 4 · 3−1

So what’s the inverse of 3 (mod 7)? Recall that the inverse of 3 is a number thanwhen you multiply it to 3 you get 1.

3 · x = 1

Let’s look at an example.

4.4 Finding the Inverse

1. 3−1 (mod 7)

We need to solve

3x ≡ 1 (mod 7)

There are three ways we can do this. Let’s 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)

Page 30: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

4 MODULAR ARITHMETIC 30

From the multiplication table, you should have

3 · 5 ≡ 1 (mod 7)

So if an inverse exists, you can find it from the multiplication table. Thethird way will be covered in the section The Extended EuclideanAlgorithm.

So when we are in (mod 7), 3−1 is 5 (mod 7).

So 4/3 (mod 7) can be calculated as

4 · 3−1 ≡ 4 · 5 = 20 ≡ 6 (mod 7)

2. 3−1 (mod 4)

3. 7−1 (mod 11)

Page 31: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

4 MODULAR ARITHMETIC 31

Please note that some inverses and divisions do not exist. For example, 2−1

(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 haveinverses. There is a reason for this. Take some time to think about it. Trycreating a multipication table for (mod 12) and see what happens.

* mod 12

Example: Find11

5(mod 12)

Page 32: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

4 MODULAR ARITHMETIC 32

4.5 Solving Equations (mod n)

How did we solve an equation like 3x = 5 from basic algebra? We divided bothsides by 3.

3x = 5

3x

3=

5

3

x =5

3

So how would we solve the equation

3x ≡ 5 (mod 11)

Instead of dividing both sides by 3 (we don’t do division like this in (mod n)), wemultiply both sides by the inverse of 3, 3−1.

3x ≡ 5 (mod 11)

3−1 · 3x ≡ 3−1 · 5 (mod 11)

x ≡ 3−1 · 5 (mod 11)

So what’s 3−1 (mod 11)?

From the multiplication table, 3−1 ≡ 4 (mod 11) since 3 · 4 ≡ 1 (mod 11).

Therefore, our solution to 3x ≡ 5 (mod 11) is

x ≡ 3−1 · 5 (mod 11)

x ≡ 4 · 5 (mod 11)

Page 33: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

4 MODULAR ARITHMETIC 33

x ≡ 9 mod 11

Now let’s 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)

Page 34: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

4 MODULAR ARITHMETIC 34

4.6 The Extended Euclidean Algorithm

A very useful tool to use when trying to find the inverse of a number (mod n) iscalled the Extended Euclidean Algorithm. As you probably figured out by now, anumber will have an inverse (mod n) as long as that number is relativly prime ton.

This method requires the division algorithm on the left hand side, and a recursiveformula on the right. Please follow the example below.

We need the recursive formula: pi = pi−2 − pi−1 · qi−2 where p0 = 0 and p1 = 1.

When you move onto the next step, you use ai = bi−1 and bi = ri−1

3 · d ≡ 1 (mod 20)

ai = qi · bi + ri pi = pi−2 − pi−1 · qi−2

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

Therefore, the solution to 3 · d ≡ 1 (mod 20) is d = 7 (mod 20).

This means 3−1 ≡ 7 (mod 20)

Here’s another example:

7 · d ≡ 1 (mod 40)

Remember, ai = bi−1 and bi = ri−1

Page 35: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

4 MODULAR ARITHMETIC 35

ai = qi · bi + ri pi = pi−2 − pi−1 · qi−2

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

Therefore, the solution to 7 · d ≡ 1 (mod 40) is

d = −17 ≡ 23 (mod 40)

Just to check,

7 · 23 = 161 ≡ 1 (mod 40)

Therefore, 7−1 ≡ 23 (mod 40).

Your turn! Find 9−1 (mod 16) by solving the following equation.

9 · d ≡ 1 (mod 16)

You’ll find a table on the next page to complete the Extended EuclideanAlgorithm.

Page 36: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

4 MODULAR ARITHMETIC 36

ai = qi · bi + ri pi = pi−2 − pi−1 · qi−2

Step 0:

Step 1:

Step 2:

Step 3:

Step 4:

Step 5:

Your inverse is the last pi value in the table. Make sure it’s (mod n).

The inverse of 9−1 (mod 16) is

9−1 ≡ (mod 16)

Use the website http://www.brianveitch.com/cryptography/modinverse.php

to confirm the inverse.

Page 37: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

5 DIFFIE-HELLMAN KEY EXHANGE 37

5 Diffie-Hellman Key Exhange

5.1 What is it?

Suppose Alice sends Bob an encrypted message using the Vigenere Cipher. Boband Alice need to also agree upon the key, otherwise Bob would have to decipherAlice’s message the hard way. If Eve, our evesdropper, is aware of the key thenshe can decipher all of Alice’s messages to Bob. Diffie-Hellman KeyExchange allows the sender, Alice, and the receiver, Bob, to come up with a keywith public information. The upside to this method is Eve, our evesdropper, isaware of the exchange the whole time but will have a difficult time cracking itand finding the secret key. Here’s 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 anyonethis number. This number should be relatively prime to N .

3. Bob chooses his own personal (private) number B. He does not tell anyonethis number. This number should be relatively prime to N .

4. Alice calculates the number

S = NA (mod P )

and sends this number S to Bob.

5. Bob calculates

R = NB (mod P )

and sends this number R to Alice.

6. So now Bob and Alice have two numbers. Eve, who is watching thisexchange, is fully aware of the numbers S and R. But since she doesn’t knowA and B, she doesn’t know how Alice and Bob calculated the R and S.

Page 38: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

5 DIFFIE-HELLMAN KEY EXHANGE 38

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 = SB (mod P )

8. C is their shared key. Since the last step is done in private, Eve does notknow C.

5.3 Example of a Key Exhange

Alice wants to send the message ”ATTACK AT DAWN” to Bob. Beforeencrypting it with the Vigenere Cipher, her and Bob need to agree upon thekeyword (in numeric form). Alice calls Bob, with Eve evesdropping on thisconversation, and come up with a public key

P = 983 and N = 20

You’ll probably want to use the exponentiation calculator athttp://www.brianveitch.com/cryptography/modexp.php

1. Alice chooses the number A = 7 and Bob chooses the number B = 9.Remember, these must be relatively prime to N = 20.

2. Alice calculates

S = 207 = 1280000000 ≡ 312 (mod 983)

and sends this number to Bob.

3. Bob calculates

R = 209 = 512000000000 ≡ 942 (mod 983)

and sends this number to Alice.

Page 39: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

5 DIFFIE-HELLMAN KEY EXHANGE 39

4. Alice takes 942 (mod 983) and raises it to her number A = 7.

9427 ≡ 101 (mod 983)

5. Bob takes 312 (mod 983) and raises it to his number B = 9.

3129 ≡ 101 (mod 983)

6. So Alice encrypts the message ”ATTACK AT DAWN” with the Vigenerenumeric key 101, or its equivalent alphabetic key BAB. The cipher text is

5.4 Why is Cracking this Exchange Difficult

In the previous example, here is all the information Eve has during this keyexchange.

P = 983, N = 20, , S = 312, and R = 942

In order for Eve, our evesdropper, to determine the keyword, she needs to solvethe following equations

312 ≡ 20A (mod 983)

or942 ≡ 20B (mod 983)

This is difficult to do, especially when P and N are extremely large. Our example,they aren’t large. But even for us, our best bet is probably trial and error. Wewould try to plug in all the values (mod 983) for A and B and see which work.

Page 40: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

5 DIFFIE-HELLMAN KEY EXHANGE 40

5.5 Key Exhange Activity

Get in groups of 3. One of you will be Alice, another will be Bob, and the thirdwill be Alice. In this activity, Alice and Bob will come up with their VigenereCipher key right in front of Eve.

1. Alice writes a message. It doesn’t matter how long since you will be usingthe online Vigenere Cipher. You want to send this message to Bob. Do notshow this message to anyone.

2. Alice and Bob now speak to each other publicly. Eve is there listening andtaking notes. Alice and Bob agree on two numbers. Choose a prime numberP and another number N less than P . You may use the websitehttp://www.brianveitch.com/cryptography/prime_generator togenerate the prime number.

P = and N =

3. Alice and Bob choose their private number. If you’re Alice write it below

A =

and calculate

NA ≡ (mod P)

If you are Bob, write it below

B =

and calculate

NB ≡ (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 theVigenere Cipher athttp://www.brianveitch.com/cryptography/vigenere.php to create theciphertext.

Page 41: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

5 DIFFIE-HELLMAN KEY EXHANGE 41

6. Alice sends the message to Bob for him to decipher.

7. Eve should write down the equations she must solve in order to calculatetheir private key. She may try to crack it by using trial and error.

Change positions and redo this until everyone gets a chance to be Alice.

Page 42: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

6 RSA ENCRYPTION 42

6 RSA Encryption

6.1 Introduction

I can think of two uses for RSA encryption that should make sense to us. In thelast chapter, we discussed a key exhange. This is a method for Alice and Bob tocommuicate publicly to create a secret key. If done correctly, Eve should have avery hard time figuring this key out.

RSA encryption can also be used to send the key. It’s very similar to theDiffie-Hellman Key Exhange. RSA is called public key encryption (orasymmetric-key encryption). It means that there is a public key to encrypt thedata and a secret key (only the receiver of the message knows) that will decryptthe data. So Alice chooses the keyword for the Vigenere Cipher. She uses thepublic key to encrypt her keyword and sends the ciphertext to Bob. Bob uses hissecret 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 receviesa message, supposedly from Alice. How does he actually know this message isfrom Alice? It’s possible Eve intercepted the message and created her own tosend to Bob.

6.2 Example of RSA Encryption

Let’s do an example using RSA. Public key is (e, n) = (3, 33)

Message: A T T A C K N O W

First, let’s convert the letters to numbers using A = 0, B = 1, C = 2, D = 3, ...,X = 23, Y = 24, Z = 25. Later on we will convert letters to their ASCII number.

Message: A T T A C K N O W

Pre-Cipher: 0 19 19 0 2 10 13 14 22

e = 3 is called the encryption key. Raise each number in the pre-cipher to e = 3,(mod 33).

Page 43: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

6 RSA ENCRYPTION 43

A: 03 ≡ 0 (mod 33)

T: 193 = 6859 ≡ 28 (mod 33)

T: 193 = 6859 ≡ 28 (mod 33)

A: 03 ≡ 0 (mod 33)

C: 23 ≡ 8 (mod 33)

K: 103 = 1000 ≡ 10 (mod 33)

N: 133 = 2197 ≡ 19 (mod 33)

O: 143 = 2744 ≡ 5 (mod 33)

W: 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 datahas the private key. This is called asymmetric key encryption. It means there isone key to encrypt and a completely different key to decrypt.

The private key for our example is d = 7. Let’s see what happens when we raiseour ciphertext numbers to the power of d = 7.

07 ≡ 0 (mod 33)

287 ≡ 19 (mod 33)

287 ≡ 19 (mod 33)

07 ≡ 0 (mod 33)

87 ≡ 2 (mod 33)

107 ≡ 10 (mod 33)

197 ≡ 13 (mod 33)

57 ≡ 14 (mod 33)

227 ≡ 22 (mod 33)

Page 44: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

6 RSA ENCRYPTION 44

We get back the original set of numbers

Plaintext: 0 19 19 0 2 10 13 14 22

Message: A T T A C K N O W

Suppose you want to send the number M . The ciphertext is

C ≡M 3 (mod 33)

To decrypt the ciphertext, you compute

M ≡ C7 (mod 33)

So to encrypt a number, we raised it to the 3rd power. To undo it, we raise it tothe 7th power. The question now is how do you come up with the private keyd = 7?

Warning:

Notice the same letter encrypted to the same number. Those numbers carry thesame statistical information as the Ceasar method. Once I figure out T = 28, Ican replace all 28s with T. This is no good to us. So let’s go back to

Message: A T T A C K N O W

Cipher: 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 thenumbers. One example would be,

01, 91, 90, 21, 01, 31, 42, 2

Page 45: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

6 RSA ENCRYPTION 45

The problem is our modulus is 33, which is too small (since we have numberslarger than 33 - 91, 90, 42) We need to choose a larger modulus and figure out anew public and private key. Let’s figure out how to do this now.

6.3 RSA Algorithm

Suppose Alice wants to send Bob a message. She uses Bob’s public key (e, n).Let’s see how Bob came up with his public/private key by coming up with ourown public and private key.

1. Bob chooses two large primes. Call them p and q. He must keep thesesecret!

Now you choose two primes (preferably between 10 and 20).

p = q =

2. He then computes n = p · q. This is part of the public key.

n =

3. Bob computes φ(n) = (p− 1) · (q − 1)

φ(n) =

4. Bob chooses an integer e, 1 < e < φ(n) where e does not share any factorswith φ(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.

Page 46: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

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 followingequation

e · d ≡ 1 (mod n)

Your equation is

· d ≡ 1 (mod )

Let’s go over how Bob found d to the following equation. Then follow thepattern. This may take a few tries to get right.

6.4 Using The Extended Euclidean Algorithm

We need the recursive formula: pi = pi−2− pi−1 · qi−2 where p0 = 0 and p1 = 1.

When you move onto the next step, you use ai = bi−1 and bi = ri−1

3 · d ≡ 1 (mod 20)

Page 47: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

6 RSA ENCRYPTION 47

ai = qi · bi + ri pi = pi−2 − pi−1 · qi−2

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

Therefore, the solution to 3 · d ≡ 1 (mod 20) is d = 7 (mod 20). This is howBob got his private key.

To view another example, please check out the Extended EuclideanAlgorithm section in Modular Arithmetic.

Your turn!

· d ≡ 1 (mod )

ai = qi · bi + ri pi = pi−2 − pi−1 · qi−2

Step 0:

Step 1:

Step 2:

Step 3:

Step 4:

Step 5:

Page 48: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

6 RSA ENCRYPTION 48

Your d is the last pi value in the table. Make sure it’s (mod n).

Your private key is

d =

Use the website http://www.brianveitch.com/cryptography/modinverse.php

to confirm the inverse.

Example: Use your public key to encrypt the following. First, convert the lettersto numbers.

Message: M A T H E Q U A L S M O N E YPre-Cipher:

To encrypt each number N into the cipher C

C ≡ N e (mod n)

Use my expontentiation calculator at

http://www.brianveitch.com/cryptography/modexp.php

Page 49: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

6 RSA ENCRYPTION 49

M: ≡ (mod )

A: ≡ (mod )

T: ≡ (mod )

H: ≡ (mod )

E: ≡ (mod )

Q: ≡ (mod )

U: ≡ (mod )

A: ≡ (mod )

L: ≡ (mod )

S: ≡ (mod )

M: ≡ (mod )

O: ≡ (mod )

N: ≡ (mod )

E: ≡ (mod )

Y: ≡ (mod )

To practice RSA encryption, work with a partner. Write a short message (nomore than 20 letters) and encrypt it using your partner’s public key. Do not tellyour partner your plaintext message. Give them only the ciphertext.

Your partner then decrypts the ciphertext using his or her private key to revealyour message. Remember, to decrypt the message do the following

N ≡ Cd (mod n)

Using the websitehttp://www.brianveitch.com/cryptography/generate_rsa_keys.php,genereate your own public and private key.

Exchange public keys with your partner. Use your partner’s public key to encrypta word (at most 7 letters). Have your partner decrypt the word. Since themodular exponentiation will be large, use the following websites to help

Page 50: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

6 RSA ENCRYPTION 50

http://www.brianveitch.com/cryptography/modexp.php

andhttp://www.brianveitch.com/cryptography/modinverse.php

How do we make this harder to crack?

Let me return to our previous example of encrypting

ATTACK NOW

Use the ASCII table provided below to convert every character (including thespace, periods, etc.) into its correponding ASCII number.

6.5 ASCII Table

Page 51: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

6 RSA ENCRYPTION 51

After converting the characters to their corresponding ASCII numbers, we get thefollowing,

Message: A T T A C K N O W

Plaintext: 65 84 84 65 67 75 32 78 79 87

Let’s use the public key (e = 709, n = 923). To make this harder to crack, weregroup the numbers into blocks of 3. There is nothing special about the size ofthe block. You just have to make sure the numbers you’re encrypting are lessthan n = 923.

658|484|656|775|327|879|87

Since the numbers will get large, use the calculator athttp://www.brianveitch.com/cryptography/modexp.php

658709 ≡ (mod 923)

484709 ≡ (mod 923)

656709 ≡ (mod 923)

775709 ≡ (mod 923)

327709 ≡ (mod 923)

879709 ≡ (mod 923)

87709 ≡ (mod 923)

Therefore, the message

ATTACK NOW

encrypts to697 419 188 541 288 333 854

Page 52: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

6 RSA ENCRYPTION 52

Suppose you’re Eve and you come across this encrypted message. You know themethod used and you know Bob’s public key (709, 923). How do we crack it?

6.6 Cracking RSA by finding the private key d.

To decrypt the ciphertext, we need the private key d. So let’s try to crack it.

1. Factor n = 923 into two prime numbers p and q.

n = ×

2. Calculte φ(n) = (p− 1) · (q − 1)

φ(923) =

3. Now solve the equation using the Extended Euclidean Algorithm.

709 · d ≡ 1 (mod 840)

qi pi = pi−2 − pi−1 · qi−2

Step 0:

Step 1:

Step 2:

Step 3:

Step 4:

Step 5:

Step 6:

Page 53: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

6 RSA ENCRYPTION 53

Good! You now have the private key d =

Use http://www.brianveitch.com/cryptography/modexp.php to completethe exponentiation. Raise each block of the ciphertext by the power of d.

≡ (mod 923)

≡ (mod 923)

≡ (mod 923)

≡ (mod 923

≡ (mod 923)

≡ (mod 923)

≡ (mod 923)

4. After decrypting the numbers, you should have

658 484 656 775 327 879 87

5. Regroup the numbers back into blocks of 2 (starting from the left)

6. Finally rewrite the ASCII number back into its characters.

6.7 Your turn to crack an RSA encrypted message

You are Eve and you intercept a message sent from Alice to Bob. It wasencrypted using Bob’s public key (n = 1224825487, e = 1118107197).

The ciphertext is

71218284 237148343 889555017 473479370 649324334 6660643 260396109637251342 924334351 935096349 271621588 366934580 271621588 576453518423052470 618017834 332813753 397822860

Page 54: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

7 USING RSA FOR AUTHENTICATION 54

To crack the code, find the private key d. Follow the method we used to crack theRSA example from before. Start by factoring n.

You can decrypt it using the private key d using the websitehttp://www.brianveitch.com/cryptography/modexp.php. If you’re doing it byhand, the original block size (before encryption) was 4.

or the website http://www.brianveitch.com/cryptography/rsa.php. Use ablock size of 2 on this website.

There you go. This is the general idea behind the RSA encryption. Now beforewe finish with our RSA introduction, I’d like to show you the size of a typicalmodulus n.

161521746670640296426473658228859984306663144318152681524054709078245736590366297248377298082656939330673286493230336261991466938596691073112968626710792148904239628873374506302653492009810626437582587089465395941375496004739918498276676334238241465498030036586063929902368192004233172032080188726965600617167

Can you see why trying to factor n to crack RSA is hard? Even if you can factorit, you still need to find the private key d.

7 Using RSA for Authentication

7.1 Algorithm

As noted earlier, how does Bob actually know the message is sent from Alice.Here’s 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 encryptsher name with her private key d = 1133. Remember that only Alice knowsthe private key. You may need the calculator athttp://www.brianveitch.com/cryptography/modexp.php

Page 55: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

7 USING RSA FOR AUTHENTICATION 55

651133 ≡ 228 (mod 2263)

761133 ≡ 1630 (mod 2263)

731133 ≡ 1314 (mod 2263)

671133 ≡ 645 (mod 2263)

691133 ≡ 1405 (mod 2263)

3. She sends her encrypted name along with the encrypted message meant forBob.

ALICE Encrypted: → 228 1630 1314 645 1405

4. Bob receives two ciphers. One is Alice’s encrypted name. The other is theactual encrypted message. Bob decrypts her message, but isn’t convinced itwas sent from Alice.

5. Bob uses Alice’s public key, which is accessible by everyone, to decrypt thename.

228917 ≡ 65 (mod 2263)

1630917 ≡ 76 (mod 2263)

1314917 ≡ 73 (mod 2263)

645917 ≡ 67 (mod 2263)

1405917 ≡ 69 (mod 2263)

65 76 73 67 69 → ALICE

6. Since Alice sent her name by encrypting it with her private key, no one canfake 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 herRSA public key.

Page 56: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

7 USING RSA FOR AUTHENTICATION 56

7.2 Authentication Activity

1. Get in groups of 3. One person is Alice, another is Bob, and the third isAlice.

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 athttp://www.brianveitch.com/cryptography/generate_rsa_keys. Alicewill create a message and encrypt it. She may use any method she wants.

3. Eve will create her own message (something that may contradict Alice’smessage).

Example:

(a) Alice may encrypt the message ”ATTACK AT DAWN” using the CaesarCipher.

(b) Eve may encrypt ”DO NOT ATTACK AT DAWN” using the CaesarCipher.

4. Using Alice’s public key, she encrypts her name by using her private key.

5. Eve will also encrypt Alice’s 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, useAlice’s public key to decrypt the names.

8. Unless Eve guessed correctly on Alice’s private key, Alice’s encrypted nameshould decrypt back to Alice and Eve’s should decrypt to jibberish.

Page 57: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

8 IMPORTANT INFORMATION 57

8 Important Information

8.1 Caesar Shift Wheel

Page 58: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

8 IMPORTANT INFORMATION 58

8.2 Letter Frequency Tables

Page 59: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

8 IMPORTANT INFORMATION 59

8.3 Vigenere’s Table

Page 60: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

8 IMPORTANT INFORMATION 60

8.4 ASCII Table

8.5 Useful Websites

Random Substitution Generatorhttp://www.brianveitch.com/cryptography/sub_shift.php

Easier Caesar Cipherhttp://www.brianveitch.com/cryptography/caesar_friendly.php

Caesar Cipherhttp://www.brianveitch.com/cryptography/caesar.php

Keyword Substitution Cipherhttp://www.brianveitch.com/cryptography/keyword.php

Page 61: Introduction to Cryptographybrianveitch.com/cryptography/cryptography/y.pdf1 INTRODUCTION 3 1 Introduction You don’t have to be an expert in cryptogrphy to appreciate the eld. Cryptography

8 IMPORTANT INFORMATION 61

Vigenere Cipherehttp://www.brianveitch.com/cryptography/vigenere.php

Letter Frequencyhttp://www.brianveitch.com/cryptography/relative_frequency.php

Modular Exponentiationhttp://www.brianveitch.com/cryptography/modexp.php

Modular Inversehttp://www.brianveitch.com/cryptography/modinverse.php

String to ASCIIhttp://www.brianveitch.com/cryptography/string_to_number.php

ASCII to Stringhttp://www.brianveitch.com/cryptography/number_to_string.php

Easier RSA - No Blocking Before Encryptinghttp://www.brianveitch.com/cryptography/rsa_easy.php

RSA - Blocking Before Encryptinghttp://www.brianveitch.com/cryptography/rsa.php

Generate RSA Public and Private Keyshttp://www.brianveitch.com/cryptography/generate_rsa_keys.php

Generate Prime Numbershttp://www.brianveitch.com/cryptography/generate_primes.php