6: cryptography and cryptanalysis · """encode the message m with caesar cipher and...

Post on 06-Jul-2020

53 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

6: Cryptographyand Cryptanalysis

Today• End of Q3

• Homework #3 – Solution

• Project #3‣Caesar cipher decryption‣Caesar cipher cryptanalysis

End of Q3 Schedule• Monday, November 25 (10:45 12:15)‣Lecture 7: Summary, practice, Q/A,…‣Work on Project #3, ask questions, …

• Thursday, November 28 (10:45 12:15)‣Room W833, Ookayama West Building 8 (19 on this map)‣Final exam. Written test, without computer‣Document allowed: a single A4 paper

Eve

Cryptography (last week)

4

SenderReceiver

Eavesdropper

Encode the message such that its contents will be hidden for the outside observer.

Can be used for stored data as well,rather than for transmitted data.Model of encrypted communication

Alice BobAliceReceiveSend

Plaintext mCiphertext c

Encrypt Decrypt

Cryptanalysis

Decryptedplaintext m

dear bdo youknow …

bcxm g …

Ciphertext cbcxm g …

dear bdo youknow …

② ③

Caesar Cipher(last week)

• Invented and used byGaius Julius Caesar (100BC-44BC)

• Algorithm• Each letter is replaced by the k-th letter of the alphabet, which follows it.

5

a b c d e f g h ... w x y za b c d e f g h ... w x y zalright!

d e f g h i j k ... z a b calright!

k = 3

douljkw!

(k-letter shift in the alphabet)

Ex:

Homework #3 – Solutiondef enc(k, m):

"""Encode the message m with Caesar cipher and shift key k."""

# Convert the message in a list of character codescc_plaintext = list(m.encode("ascii"))

# Duplicate the list, to store character codes of the ciphertexcc_ciphertext = cc_plaintext.copy()

for i in range(len(m)):if ord('a') <= cc_plaintext[i] <= ord('z'): # Check if the i-th character is a lowercase letter

offset_plain = cc_plaintext[i] - ord('a') # Compute the "index" of this letter wrt. the letter 'a'offset_cipher = (offset_plain + k) % 26 # Compute the "index" shifted by kcc_ciphertext[i] = offset_cipher + ord('a') # Compute the ascii code of the encrypted letter

c = bytes(cc_ciphertext).decode("ascii")return c

# Main programk = 3plaintext = input("Plaintext: ")ciphertext = enc(k, plaintext)print(ciphertext)

caesar.py

Project #3

Project #3 – Part 1• Goal: Have a single file caesar.py that can be used

for both encryption and decryption$ python caesar.py enc 10Hello, I love you!Hovvy, I vyfo iye!$

yellow: to be typed by userend with Control+D

$ python caesar.py dec 10Hovvy, I vyfo iye!Hello, I love you!$

Standard input

$ python caesar.py enc 10 < m.txtHovvy, I vyfo iye!

Hello, I love you!m.txt

Project #3 – Part 1import sys # Needed to access the arguments given in command line

def enc(k, plaintext):...return plaintext # To change here

def dec(k, ciphertext):...return ciphertext # To change here

####################### MAIN PROGRAM ######################## You do not need to modify anything below# But (of course) you could/should try to understand it.

# To read arguments given in command lines# sys.argv is an array (list) of arguments# ex: typing "python caesar.py enc 3" gives the following# sys.argv = ["caesar.py", "enc", "3"]# We can check the length of sys.argv to detect the commandsif len(sys.argv) > 1:

command = sys.argv[1]else: # give a default value

command = "enc"

caesar.py (available on the website) # No error handling. We assume that input is valid !# There will be an error if sys.argv[2] is not an int# How to do error handling? (hint: try statement)if len(sys.argv) > 2:

key = int(sys.argv[2])else: # give a default value

key = 3

# Read the input text, either the plaintext or the ciphertext# sys.stdin is the STandarD Input# it can be also given using the '<' symbol in command lineinput_text = [ line.rstrip() for line in sys.stdin ]

# Execute the appropriate commandif command == "help":

print("python caesar.py <command> <key>")print(" command: enc | dec")print(" key: number between 0 and 25")

elif command == "dec" or command == "enc":for line in input_text:

if command == "dec":print( dec(key, line) )

else:print( enc(key, line) )

else:print("ERROR: unknown command")

Ciphertext (example)qxuvnb qjm knnw bnjcnm oxa bxvn qxdab rw brunwln frcq qrbuxwp, cqrw kjlt ldaenm xena j lqnvrlju enbbnu rw fqrlq qnfjb kanfrwp j yjacrldujauh vjuxmxaxdb yaxmdlc. qrb qnjmfjb bdwt dyxw qrb kanjbc, jwm qn uxxtnm oaxv vh yxrwc xo

Cryptanalysis: Main Idea

10

qxuvnb qjm knnw bnjcnm oxa bxvn qxdab rw brunwln frcq qrbuxwp, cqrw kjlt ldaenm xena j lqnvrlju enbbnu rw fqrlq qnfjb kanfrwp j yjacrldujauh vjuxmxaxdb yaxmdlc. qrb qnjmfjb bdwt dyxw qrb kanjbc, jwm qn uxxtnm oaxv vh yxrwc xo

n appears 19 times

Letter Frequency (English)(Source: Wikipedia)

The most frequent letter is "e"In the case of English

When a (long enough) English sentenceis encrypted, how to decipher it?

Learning the plaintext (message) from the ciphertext without knowing the key.

Cryptanalysis: Main Idea

11

qxuvnb qjm knnw bnjcnm oxa bxvn qxdab rw brunwln frcq qrbuxwp, cqrw kjlt ldaenm xena j lqnvrlju enbbnu rw fqrlq qnfjb kanfrwp j yjacrldujauh vjuxmxaxdb yaxmdlc. qrb qnjmfjb bdwt dyxw qrb kanjbc, jwm qn uxxtnm oaxv vh yxrwc xo

Frequencyarray

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

19

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 zOffset

maxj0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

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 zEnglish

13 - 4 = 9 and then ⇒ k = 91. Create a frequency array freq.2. Find the maximum frequency location maxj.3. Set k = maxj - 4, and run dec( k, cipher ) to obtain the plaintext.

Idea:

n appears 19 times

Question:What if maxj < 4 ?!

Project #3 – Part 2• Goal: Write a file caesar-cryptanalysis.py that can

decipher a ciphertext without knowing the key• You can assume that the text is long enough (with the

“meaning” of slide 10)• Usage of the file:

• Hint 1: Take strong inspiration from the caesar.py file and using array (list) is recommended.

• Hint 2: Test your program with previous example!

$ python caesar-cryptanalysis.py < cipthertext.txt

Project #3 – Guidance• Submit two files: caesar.py with enc and dec functions completed.

Add explanations (in comments) about your dec function.A clever/short solution for dec is perfectly acceptable.

caesar-cryptanalysis.pyAdd explanations (in comments) about your code

You do not need to submit a report.pdf this time.You are lucky; normally I was supposed to ask you a report, but due to time constraint, no report!

• Grading (20 points): dec function/explanations (5) + cryptanalysis code (10) + cryptanalysis explanations (5) Submit via OCW-i before December 4, 23:50

13

top related