From 6aedd8f4118deada8b750569fb9443fe786c6cfa Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Wed, 2 Jul 2014 22:14:55 +0100 Subject: [PATCH] Added transposition cipher slides --- cipher.py | 28 ++-- slides/further-work.html | 92 ++++++++++ slides/keyword-encipher.html | 4 - slides/transposition-encipher.html | 258 +++++++++++++++++++++++++++++ 4 files changed, 367 insertions(+), 15 deletions(-) create mode 100644 slides/further-work.html create mode 100644 slides/transposition-encipher.html diff --git a/cipher.py b/cipher.py index e39abce..a2df81b 100644 --- a/cipher.py +++ b/cipher.py @@ -27,7 +27,7 @@ def every_nth(text, n, fillvalue=''): >>> every_nth(string.ascii_lowercase, 5, fillvalue='!') ['afkpuz', 'bglqv!', 'chmrw!', 'dinsx!', 'ejoty!'] """ - split_text = [text[i:i+n] for i in range(0, len(text), n)] + split_text = chunks(text, n, fillvalue) return [''.join(l) for l in zip_longest(*split_text, fillvalue=fillvalue)] def combine_every_nth(split_text): @@ -471,15 +471,18 @@ def scytale_encipher(message, rows, fillvalue=' '): >>> scytale_encipher('thequickbrownfox', 4) 'tubnhirfecooqkwx' >>> scytale_encipher('thequickbrownfox', 5) - 'tubnhirfecooqkwx' + 'tubn hirf ecoo qkwx ' >>> scytale_encipher('thequickbrownfox', 6) 'tqcrnxhukof eibwo ' >>> scytale_encipher('thequickbrownfox', 7) - 'tqcrnxhukof eibwo ' + 'tqcrnx hukof eibwo ' """ - transpositions = [i for i in range(math.ceil(len(message) / rows))] + # transpositions = [i for i in range(math.ceil(len(message) / rows))] + # return column_transposition_encipher(message, transpositions, + # fillvalue=fillvalue, fillcolumnwise=False, emptycolumnwise=True) + transpositions = [i for i in range(rows)] return column_transposition_encipher(message, transpositions, - fillcolumnwise=False, emptycolumnwise=True) + fillvalue=fillvalue, fillcolumnwise=True, emptycolumnwise=False) def scytale_decipher(message, rows): """Deciphers using the scytale transposition cipher. @@ -489,16 +492,19 @@ def scytale_decipher(message, rows): 'thequickbrownfox ' >>> scytale_decipher('tubnhirfecooqkwx', 4) 'thequickbrownfox' - >>> scytale_decipher('tubnhirfecooqkwx', 5) - 'thequickbrownfox' + >>> scytale_decipher('tubn hirf ecoo qkwx ', 5) + 'thequickbrownfox ' >>> scytale_decipher('tqcrnxhukof eibwo ', 6) 'thequickbrownfox ' - >>> scytale_decipher('tqcrnxhukof eibwo ', 7) - 'thequickbrownfox ' + >>> scytale_decipher('tqcrnx hukof eibwo ', 7) + 'thequickbrownfox ' """ - transpositions = [i for i in range(math.ceil(len(message) / rows))] + # transpositions = [i for i in range(math.ceil(len(message) / rows))] + # return column_transposition_decipher(message, transpositions, + # fillcolumnwise=False, emptycolumnwise=True) + transpositions = [i for i in range(rows)] return column_transposition_decipher(message, transpositions, - fillcolumnwise=False, emptycolumnwise=True) + fillcolumnwise=True, emptycolumnwise=False) if __name__ == "__main__": diff --git a/slides/further-work.html b/slides/further-work.html new file mode 100644 index 0000000..64a9729 --- /dev/null +++ b/slides/further-work.html @@ -0,0 +1,92 @@ + + + + Breaking keyword ciphers + + + + + + + + + + + + + diff --git a/slides/keyword-encipher.html b/slides/keyword-encipher.html index bb9e679..168bb5f 100644 --- a/slides/keyword-encipher.html +++ b/slides/keyword-encipher.html @@ -161,10 +161,6 @@ Write out the rest of the alphabet... 2. and 3. How to find the appropriate letter of the keyword. -Indexing pulls out letters. `'keyword'[0]` = 'k' ; `'keyword'[3]` = 'w' ; `'keyword'[-1]` = 'd' - -Slices pulls out substrings. `'keyword'[1:4]` = 'eyw' ; `'keyword'[:3]` = 'key' ; `'keyword'[5:]` = 'rd' - `deduplicate(keyword + string_ascii_lowercase[from:] + string.ascii_lowercase)` Question: why not take a slice of the second alphabet copy? diff --git a/slides/transposition-encipher.html b/slides/transposition-encipher.html new file mode 100644 index 0000000..0c09a4b --- /dev/null +++ b/slides/transposition-encipher.html @@ -0,0 +1,258 @@ + + + + Keyword ciphers + + + + + + + + + + + + -- 2.34.1