22 caesar

14
§7.0-7.6: Applications: Caesar cipher §7.0-7.6: Applications: Caesar cipher 15 Oct 2007 CMPT14x Dr. Sean Ho Trinity Western University Quiz04 today (ch5-6)

Upload: spizzard

Post on 08-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 22 Caesar

8/7/2019 22 Caesar

http://slidepdf.com/reader/full/22-caesar 1/14

Page 2: 22 Caesar

8/7/2019 22 Caesar

http://slidepdf.com/reader/full/22-caesar 2/14

15 Oct 200715 Oct 2007CMPT14x: caesar cipher CMPT14x: caesar cipher  22

Review of §6.5-6.10Review of §6.5-6.10

Library modules:

● Public interface (header) vs.

● Private implementation

Car: owner's manual vs. shop manual Defining an abstract data type

Accessor (set/get) functions

Using (import) our library

Page 3: 22 Caesar

8/7/2019 22 Caesar

http://slidepdf.com/reader/full/22-caesar 3/14

15 Oct 200715 Oct 2007CMPT14x: caesar cipher CMPT14x: caesar cipher  33

Quiz 04 (ch5-6):Quiz 04 (ch5-6): 10 minutes, 20 points10 minutes, 20 points

Contrast aliasing a list with copying a list.

● Write Python code to demonstrate the difference

Contrast a header (DEF) file with an implementation (IMP) file.

Why should we use accessor (set/get) functions in anADT?

Write a Python function matrix(n_rows, n_cols) to

create and return a 2D list with the given number of rows/cols.

Page 4: 22 Caesar

8/7/2019 22 Caesar

http://slidepdf.com/reader/full/22-caesar 4/1415 Oct 200715 Oct 2007CMPT14x: caesar cipher CMPT14x: caesar cipher  44

Quiz 04 (ch5-6):Quiz 04 (ch5-6): answers #1answers #1

Contrast aliasing a list with copying a list.● Aliasing: another name for the same list; modifying

elements of the alias also modifies elements of theoriginal list

● Copying: a separate data structure with the samecontents

origList = [ 1, 2, 3 ]

aliasList = origList

copyList = origList[:]

aliasList[0] = 5

copyList[1] = 7

origList[0] # is 5

Page 5: 22 Caesar

8/7/2019 22 Caesar

http://slidepdf.com/reader/full/22-caesar 5/1415 Oct 200715 Oct 2007CMPT14x: caesar cipher CMPT14x: caesar cipher  55

Quiz 04 (ch5-6):Quiz 04 (ch5-6): answers #2-3answers #2-3

Contrast a header (DEF) file with an implementation (IMP) file.

● Header: public interface, doesn't define bodies of functions

● Implementation: contains bodies of the functions

Why should we use accessor (set/get) functions in anADT?

Hide implementation details from user ● Maintain the “illusion” of the ADT

● Ease future upgrades of internal implementation

Page 6: 22 Caesar

8/7/2019 22 Caesar

http://slidepdf.com/reader/full/22-caesar 6/1415 Oct 200715 Oct 2007CMPT14x: caesar cipher CMPT14x: caesar cipher  66

Quiz 04 (ch5-6):Quiz 04 (ch5-6): answers #4answers #4

Write a Python function matrix(n_rows, n_cols) tocreate and return a 2D list with the given number of rows/cols.

def create_matrix(n_rows, n_cols):

matrix = range(n_rows)for row in range(n_rows):

matrix[row] = range(n_cols)

return matrix

Page 7: 22 Caesar

8/7/2019 22 Caesar

http://slidepdf.com/reader/full/22-caesar 7/14

15 Oct 200715 Oct 2007CMPT14x: caesar cipher CMPT14x: caesar cipher  77

What's on for today (§7.0-7.6)What's on for today (§7.0-7.6)

Strings: manipulating text● Null-terminated strings

Application: cryptography (substitution cipher)

Creating a library for cryptography● Public interface

● Library-internal helper functions

Page 8: 22 Caesar

8/7/2019 22 Caesar

http://slidepdf.com/reader/full/22-caesar 8/14

15 Oct 200715 Oct 2007CMPT14x: caesar cipher CMPT14x: caesar cipher  88

Null-termination in stringsNull-termination in strings

In Python, strings are a basic type (immutable seq) But in M2/C, strings are fixed-len arrays of CHAR:

VAR myName : ARRAY [0..14] OF CHAR;

But the array is not always completely filled:myName := “AppleMan”;

How to know where the string ends?

Strings are null-terminated:

● The null character CHR(0) is added to the end

● Anything past the termination char is ignored

A p p l e M a n Ø

Page 9: 22 Caesar

8/7/2019 22 Caesar

http://slidepdf.com/reader/full/22-caesar 9/14

15 Oct 200715 Oct 2007CMPT14x: caesar cipher CMPT14x: caesar cipher  99

Cryptography exampleCryptography example

Cæsar substitution cipher :● Key: e.g., QAZXSWEDCVFRTGBNHYUJMKIOLP

● Cleartext: input text to encrypt

Ciphertext: output encrypted text● Encoding: replace each letter in source with

corresponding letter from code key

● Decoding: same, using the decode key

ROT13 was an example of a substitution cipher 

● Key: NOPQRSTUVWXYZABCDEFGHIJKLM

Page 10: 22 Caesar

8/7/2019 22 Caesar

http://slidepdf.com/reader/full/22-caesar 10/14

15 Oct 200715 Oct 2007CMPT14x: caesar cipher CMPT14x: caesar cipher  1010

Write aWrite a SubstitutionSubstitution cipher librarycipher library

What public interface do we want for the library?def encode (src, key):

"""Encode the source string using the given codestring.

Returns the encoded string.

pre: src must be a string;key must be a permutation of the 26 letters."""

def decode (src, key):

"""Decode the source string using the given codestring.

Returns the decoded string.

pre: src must be a string;

key must be a permutation of the 26 letters."""

Page 11: 22 Caesar

8/7/2019 22 Caesar

http://slidepdf.com/reader/full/22-caesar 11/14

15 Oct 200715 Oct 2007CMPT14x: caesar cipher CMPT14x: caesar cipher  1111

Internal helper functionsInternal helper functions

In the implementation it is handy to have some helper functions for internal use:

def isalpha (ch):

"""Return true if ch is a letter."""

def alpha_pos (ch):"""Return index of a letter in the range 0 .. 25"""

def decode_key (enckey):

"""Create a decode key from an encoding key"""

How to implement these?● isalpha() is built-in: ch.isalpha()

Page 12: 22 Caesar

8/7/2019 22 Caesar

http://slidepdf.com/reader/full/22-caesar 12/14

15 Oct 200715 Oct 2007CMPT14x: caesar cipher CMPT14x: caesar cipher  1212

Implementing Substitution libraryImplementing Substitution library

Main function to encode strings:def  encode(src, key):

"""Encode the source string using the given codestring.

Returns the encoded string.

pre: src must be a string;

key must be a permutation of the 26 letters.

"""

dst = ""

for ch in src:

if ch.isalpha():

dst += key[alpha_pos(ch)]

else:

dst += ch

return dst

Page 13: 22 Caesar

8/7/2019 22 Caesar

http://slidepdf.com/reader/full/22-caesar 13/14

15 Oct 200715 Oct 2007CMPT14x: caesar cipher CMPT14x: caesar cipher  1313

Implementing decode()Implementing decode()

Decoding is just encoding using a reverse key:def  decode (src, key):

"""Decode the source string using the given codestring.

Returns the decoded string.

pre: src must be a string;

key must be a permutation of the 26 letters.

"""

return encode(src, decode_key(key))

Library: http://twu.seanho.com/python/substitution.py

Testbed: http://twu.seanho.com/python/caesartest.py

Page 14: 22 Caesar

8/7/2019 22 Caesar

http://slidepdf.com/reader/full/22-caesar 14/14

15 Oct 200715 Oct 2007CMPT14x: caesar cipher CMPT14x: caesar cipher  1414

TODO itemsTODO items

Lab05 due Wed: ch6 # 33 / 35 HW05 due Fri:

● ch6 # 25 (hint does not apply in Python)

ch6 # 28 (write a Python program to do this) 140 Final / 141 midterm next week

● Wed 24Oct 14:35-15:50 (part 1)

● Thu 25Oct 13:10-14:15 (part 2)