Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an...

35
Cryptography Programming Lab Mike Scott

Transcript of Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an...

Page 1: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Cryptography Programming Lab

Mike Scott

Page 2: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Why Cryptography?

• Astrachan’s Law: – “Do not give an assignment that computes something that is

more easily figured out without a computer. ... Show off the power of computation.”

• Secrets are interesting• Practical applications– Is it safe to use my credit card to purchase something via a

website?• Fascinating history– Mary Queen of Scots, Alan Turing

• Application of mathematics and programming

Page 3: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Plan for today

• Look at four different ciphers• Complete program involving each

• Caesar• Columnar • Random Substitution• Vigenère

Page 4: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Definitions

• Cryptography– The art and study of hiding information

• Cipher– Algorithm for performing encryption and decryption

• Encryption– Converting plain text (or information) to unintelligible

text (aka cipher text) that cannot be understood without knowing how the information was converted

• Decryption– recovering the original plain text from the cipher text

Page 5: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Caesar Cipher

• Named after Julius Caesar• Also called the shift cipher• Example of a substitution cipher• Each letter (or character) is replaced by

another letter in the alphabet

Page 6: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Caesar cipher

Example with a shift of 5

ABCDEFGHIJKLMNOPQRSTUVWXYZ PlainFGHIJKLMNOPQRSTUVWXYZABCDE Encrypted

COMPUTER SCIENCE PlainHTRUZYJWXHNJSHJ Encrypted

Assume all non letters removed.

Page 7: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Variations

• Using computer could simply apply shift to all characters, not just upper case letters– Printable ASCII characters space to ~ (32 – 126)

• Maintain or remove non letters?

• lower case to upper case?

Page 8: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Breaking Caesar Cipher

• Brute force• With only letters try all 25 possibilities• Still not hard if all ASCII

Page 9: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Caesar Programming Problem

• Log in• Go to http://userweb.cs.utexas.edu/~scottm/• Click on link to Crypto Resources at bottom of

page• Download Caesar.java to desktop• Start Eclipse (or other IDE if you prefer)• Create project• Add file• Complete method printAllShifts(String msg)

Page 10: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Columnar Cipher

• Example of a Transposition cipher• The characters from the original message are

used, but put in a different order, based on the cipher

Hook ‘em Horns! We bleed orange! Plain

• Pick a number of rows for the cipher• Fill in the grid in column major order

Page 11: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Columnar Encryption

Hook ‘em Horns! We bleed orange!

• Read off rows to create messageH’o loeoerWer!omneeak s dn H!b g

H ' o l o eo e r W e r !o m n e e ak s d n

H ! b g

Page 12: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Columnar Programming Problem

• Download Columnar.java

• Complete the method printColumnar(String clear,

int rows)

Page 13: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Random Substitution Cipher • How strong is the Caesar cipher?

• Pick a secret word with no repeat letters, computery

ABCDEFGHIJKLMNOPQRSTUVWXYZ PlainCOMPUTERYABDFGHIJKLNQSVWXZ

Encrypted

Page 14: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Example

ABCDEFGHIJKLMNOPQRSTUVWXYZ PlainCOMPUTERYABDFGHIJKLNQSVWXZ

Encrypted

THE ANSWER FOR NUMBER THREE IS ATHEANSWERTONUMBERTHREEISA PlainNRUCGLVUKNHGQFOUKNRKUUYLC Encrypted

Page 15: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Random Substitution Ciphers

• Instead of picking a keyword randomly pick letters

• Must share the whole key, but lots of possibilities

• 26! possible keys = 4.03291461 × 1026

• Assume we could check a billion keys a second• It would take 1.27882883 × 1010 years to check

them all.– About the age of the universe

Page 16: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

But ...

• But substitution ciphers turn out to be relatively easy to solve

• Why?

Page 17: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Letter Frequency

Page 18: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Cracking the Substitution Cipher

• Given an encrypted message count how often each character occurs

• If only letters, assume most frequent letter is e, next most frequent is t, next most frequent is a, and so forth

• Apply the potential key• Look for clear words• Alter key as appropriate

Page 19: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Substitution Programming Problem

• With a computer a key can easily be created that uses all printable characters not just the letters.

• Download DecryptSub.java• Complete the method

int[] createFreqTable(String encrypted)

• The method returns an array of length 128. All ASCII chars are counted.

• The index of the array maps to the ASCII value of the char

Page 20: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Substitution Programming Problem

• When run the program:– converts a hard coded file (which I have encrypted with

a randomly generated substitution key) to a String– creates a frequency table (using your method)– creates an initial key based on the frequency table and

the “normal” frequency of printable ASCII chars– applies the initial key to the encrypted message and

displays it– prompts for change in key, applies it and displays new

decrypted message (A bit of an art)

Page 21: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Vigenère Cipher

• Named after Blaise de Vigenère• “The Unbreakable Cipher”• A poly-alphabetic substitution cipher• Each letter in the plain text can encrypt to

multiple letters

Page 22: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Vigenère Cipher All 26 Caesar Ciphers Pick a secret word Repeat secret word over

the plain text The secret word letter

gives the row, the plain text gives the column

The letter at the intersection is the cipher text

Page 23: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Vigenère Cipher ExampleSecret word: TEXASPlain text: MEET AT THE TOWERTEXASTEXASTEXAMEETATTHETOWER1st letter, row T, column M -> F2nd letter, row E, column E -> I3rd letter, row X, column E -> B

FIBTSMXEELHABR

Page 24: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Large Vigenère Example

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z0

2

4

6

8

10

12

14

ActualExpected

Page 25: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Frequencies In Cipher text

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z0

1

2

3

4

5

6

7

Longer Secret Words with more of the letters flattens it more!

Page 26: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Breaking the Vigenère Cipher

• Given a long enough sample of cipher text it is possible to break the Vigenère cipher

• Assume the secret word is TEXAS which has a length of 5.

• Notice then there are 5 ways to encode the plain text word “the”

• Some words show up a lot in regular language• So let’s look for 3 letter sequences that are

repeated in a cipher text

Page 27: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Cipher Text

• BLXDLAMPSLHVVFJHQLNWPLLHSWRLBMLMKEKLXLTWEPFTLHQBOJMSXNQHXEEJBQXYUKIAILMLBSWWYZTAOIFNXEYBNUXSCAFHPAVAGXXGWNTLNLAIKAJKEQOJYSOTZXFBGAGRFNYHJFTSGHJYGPRPKWIXFCSEMKCJXHRLAMCAUJBRDTZXHXYKMLXTXHPIOOXHCOJMLBBSEEKCWHJQHWLXOAFZIQADXAEEFFCZOFOMSISELLSLWMPCGOIOEVMLXTZXLXDLHPAMWLSJUUAEKDLAEQIOTWMRGGIQOVHYYTXNPKEKL

Page 28: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

3 Letter Repeated Sequences

DLA [270]EKL [265]HPA [165]KEK [265]LAM [159]LXD [255]

LXT [70]MLB [125]MLX [70]OJM [145]TZX [50, 130, 80]XDL [255]

Numbers are distances between the repeated 3 letter sequence

Page 29: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Using Repeated Sequences

• Some repeated sequences will just be random.• But, some will be due to the same word being

encoded with the same parts of the secret word!

• If this is the case the secret word is a factor of the distance between the repeated sequences

Page 30: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Factors of DistancesDLA: [2, 3, 5, 6, 9, 10, 15, 18, 27, 30, 45, 54, 90, 135, 270]EKL: [5, 53, 265]HPA: [3, 5, 11, 15, 33, 55, 165]KEK: [5, 53, 265]LAM: [3, 53, 159]LXD: [3, 5, 15, 15, 17, 51, 85, 255]LXT: [2, 5, 7, 10, 14, 35, 70]MLB: [5, 25, 125]MLX: [2, 5, 7, 10, 14, 35, 70]OJM: [5, 29, 145]TZX: [2, 5, 10, 25, 50]TZX: [2, 5, 10, 13, 26, 65, 130]TZX: [2, 4, 5, 8, 8, 10, 16, 20, 40, 80]XDL: [3, 5, 15, 15, 17, 51, 85, 255]

Page 31: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Frequency of Factors2 - 63 - 54 - 15 - 136 - 17 - 28 - 29 - 110 - 611 - 1

13 - 114 - 215 - 616 - 117 - 218 - 120 - 125 - 226 - 127 - 129 - 1

30 - 133 - 135 - 240 - 145 - 150 - 151 - 253 - 354 - 155 - 165 - 170 - 2

80 - 185 - 290 - 1125 - 1130 - 1135 - 1145 - 1159 - 1165 - 1255 - 2265 - 2270 - 1

Page 32: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Secret Code Word

• Strong evidence the code word is length 5• So start with first character and do frequency

analysis on every 5th character. Will just be a simple Caesar shift

• Repeat starting at second character and every 5th

• 5 frequency analysis problems

Page 33: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Simon Singh Vigenère Cracking Tool

Page 34: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Slide for Best Fit

First Letter of Secret Word is V in this example

Page 35: Cryptography Programming Lab Mike Scott. Why Cryptography? Astrachan’s Law: – “Do not give an assignment that computes something that is more easily figured.

Vigenère Programming Problem

• Download FindSecretWordLength.java

• Complete the printFactors(String repeatedSection, int distance)

method that prints all factors of distance in order

• If you finish add a method to find the most frequent factor. Feel free to change printFactors to return the factors found.