src.ch04.practice package¶
Submodules¶
src.ch04.practice.p1_hack_lincoln module¶
Hack route cipher sent by Abraham Lincoln.
-
src.ch04.practice.p1_hack_lincoln.
decode_route
(keys: list, cipherlist: list) → list[source]¶ Decode route cipher.
Decode cipherlist encoded with a route cipher using keys.
- Parameters
- Returns
List of strings representing plaintext message.
Note
Assumes vertical encoding route.
-
src.ch04.practice.p1_hack_lincoln.
get_factors
(integer: int) → list[source]¶ Get factors of integer.
Calculate factors of a given integer.
- Parameters
integer (int) – Number to get factors of.
- Returns
List of integer factors of integer.
-
src.ch04.practice.p1_hack_lincoln.
hack_route
(ciphertext: str) → None[source]¶ Hack route cipher.
Hack route cipher by using
get_factors()
to find all possible key lengths. Then usekeygen()
to generate all possible keys and pass each one throughdecode_route()
.- Parameters
ciphertext (str) – Message encoded with route cipher.
- Returns
None. Prints all possible decoded messages.
-
src.ch04.practice.p1_hack_lincoln.
keygen
(length: int) → list[source]¶ Generate all possible route cipher keys.
Generates a list of all possible route cipher keys of length.
- Parameters
length (int) – Length of route cipher key.
- Returns
List of lists of integers representing all possible route cipher keys of length.
Example
>>> from src.ch04.practice.p1_hack_lincoln import keygen >>> keygen(2) [[-1, -2], [-1, 2], [1, -2], [1, 2]]
src.ch04.practice.p2_identify_cipher module¶
Identify letter transposition or substitution cipher.
-
src.ch04.practice.p2_identify_cipher.
identify_cipher
(ciphertext: str, threshold: float) → bool[source]¶ Identify letter transposition or substitution cipher.
Compare most frequent letters in ciphertext with the most frequent letters in the English alphabet. If above threshold, it is a letter transposition cipher. If not, it is a letter substitution cipher.
-
src.ch04.practice.p2_identify_cipher.
is_substitution
(ciphertext: str) → bool[source]¶ Identify letter substitution cipher.
Wrapper for
identify_cipher()
. threshold defaults to0.45
.
-
src.ch04.practice.p2_identify_cipher.
is_transposition
(ciphertext: str) → bool[source]¶ Identify letter transposition cipher.
Wrapper for
identify_cipher()
. threshold defaults to0.75
.
src.ch04.practice.p2_identify_cipher_deco module¶
Identify letter transposition or substitution cipher using decorator.
Note
Not part of the book, I was just curious about decorators and decided to tinker with them a bit.
-
src.ch04.practice.p2_identify_cipher_deco.
identify
(threshold: float = 0.5)[source]¶ Make decorator for identify_cipher.
Decorator factory to replace a decorated function with
identify_cipher()
. A bit like going around the world to reach the teleporter across the street, but at import time instead of runtime, so it doesn’t matter.Luciano Ramalho’s book Fluent Python appropriately calls decorators “syntactic sugar” when they aren’t used in classes. It also references the
wrapt
module’s blog on GitHub for a deeper explanation of decorators.Not sure what a decorator factory would be called…syntactic caramel?
- Parameters
threshold (float) – Percent match in decimal form.
- Returns
Whatever the output of
identify_cipher()
would be given the decorated function’s input.
-
src.ch04.practice.p2_identify_cipher_deco.
is_substitution
(ciphertext: str) → bool[source]¶ Identify letter substitution cipher.
Empty function to wrap with
identify_cipher()
usingidentify()
. threshold defaults to0.45
.
-
src.ch04.practice.p2_identify_cipher_deco.
is_transposition
(ciphertext: str) → bool[source]¶ Identify letter transposition cipher.
Empty function to wrap with
identify_cipher()
usingidentify()
. threshold defaults to0.75
.
src.ch04.practice.p3_get_keys module¶
Get route cipher key from user and store as dictionary.
Note
Assumes vertical cipher routes.
-
src.ch04.practice.p3_get_keys.
get_keys
() → list[source]¶ Get route cipher keys from user.
User only has to enter positive/negative integers. Each gets added to a list and returned when the user has no other keys to add.
- Returns
List of integers as column numbers and positive/negative values as route direction.
-
src.ch04.practice.p3_get_keys.
key_to_dict
(keys: list) → dict[source]¶ Convert route cipher key to dictionary.
Take a route cipher key in list format where integers are column numbers and positive/negative is the route direction and convert to a dictionary where the column numbers are keys and the route direction as
up
/down
are the values.- Parameters
keys (list) – List of integers with direction as positive/negative.
- Returns
Integers keys and
up
/down
as values.
src.ch04.practice.p4_generate_keys module¶
Generate route cipher keys for brute-forcing a route cipher.
Already implemented with keygen()
,
but this version will return a list of tuples.
-
src.ch04.practice.p4_generate_keys.
generate_keys
(length: int) → list[source]¶ Generate all possible route cipher keys.
Generates a list of all possible route cipher keys of length.
- Parameters
length (int) – Length of route cipher key.
- Returns
List of tuples of integers representing all possible route cipher keys of length.
src.ch04.practice.p5_hack_route module¶
Another way to hack a route cipher.
Already implemented in p1_hack_lincoln
, but this
version will use the building blocks made in
p2_identify_cipher
,
p3_get_keys
, and
p4_generate_keys
.
-
src.ch04.practice.p5_hack_route.
decode_route
(keys: dict, cipherlist: list) → list[source]¶ Decode route cipher.
Decode cipherlist encoded with a route cipher using keys.
- Parameters
- Returns
List of strings representing plaintext message.
Note
Assumes vertical encoding route.
-
src.ch04.practice.p5_hack_route.
hack_route
(ciphertext: str, columns: int) → None[source]¶ Hack route cipher using brute-force attack.
Determine if ciphertext is a transposition cipher. If so, use columns to generate all possible keys. Convert each key to an
up
/down
dictionary for each route to take, then print the result of each key.
Module contents¶
Chapter 4 Practice Projects.