src.ch04.challenge package

Submodules

src.ch04.challenge.c1_encode_route module

Encode a route cipher and replace code words.

src.ch04.challenge.c1_encode_route.encode_route(plaintext: str, keys: list, rows: int) → list[source]

Encode plaintext message with route cipher.

Clean plaintext with format_plaintext(), replace sensitive intel with replace_words(), fill with dummy words using fill_dummy() until keys and rows are factors, then encrypt with a route cipher using keys.

Parameters
  • plaintext (str) – Plaintext message to encode with route cipher.

  • keys (list) – List of positive/negative integers representing cipher route.

  • rows (int) – Number of rows to use in the route cipher table.

Returns

List of strings of transposed words.

Note

Assumes vertical encoding routes.

src.ch04.challenge.c1_encode_route.fill_dummy(plainlist: list, factors: list, dummy_words: list = None) → list[source]

Fill a plainlist with dummy words.

Adds pseudorandom dummy words to the end until the factors of the length of plainlist includes factors.

Parameters
  • plainlist (list) – List of words of plaintext message.

  • factors (list) – List of integers that must be factors of the length of plainlist.

  • dummy_words (list) – List of dummy words to use as filler. If not provided, defaults to DICTIONARY_FILE_PATH using cleanup_dict().

Returns

Same list as plainlist, but with dummy words added.

src.ch04.challenge.c1_encode_route.format_plaintext(plaintext: str) → list[source]

Format plaintext message for encoding.

Prepare plaintext for route cipher encoding. Convert to lowercase, remove punctuation.

Parameters

plaintext (str) – Plaintext message to format.

Returns

List of strings of each word in plaintext message.

src.ch04.challenge.c1_encode_route.main()[source]

Demonstrate the route cipher encoder.

src.ch04.challenge.c1_encode_route.replace_words(plainlist: list, code_words: dict = None) → list[source]

Replace sensitive words with code words.

Replace words that shouldn’t be transmitted with code words.

Parameters
  • plainlist (list) – List of strings of each word in plaintext message.

  • code_words (dict) – Dictionary of sensitive words and their code words. If not provided, defaults to the book’s code words. Use lowercase strings in dictionary.

Returns

Same list, but with sensitive words replaced with code words.

src.ch04.challenge.c2_encode_rail module

Encode message with a 3-rail fence cipher.

src.ch04.challenge.c2_encode_rail.encode_rail(plaintext: str, split: int = 5) → str[source]

Encode rail fence cipher.

Encode plaintext with a 3-rail fence cipher. Scrub the plaintext with format_plaintext(), then encrypt it with split_rails().

Parameters
  • plaintext (str) – Message to encrypt with 3-rail fence cipher.

  • split (int) – How many letter segments to split message into. Defaults to 5.

Returns

String with encrypted message split into split chunks for easier transmission.

src.ch04.challenge.c2_encode_rail.main()[source]

Demonstrate 3-rail fence cipher encoder.

src.ch04.challenge.c2_encode_rail.split_rails(plaintext: str) → str[source]

Split plaintext into 3 rails for encryption.

Split the rails where the top rail is every 4th letter, the middle rail is every other letter starting at 1, and the bottom rail is every 4th letter starting at 2. After splitting, concatenate each rail and return the result.

Parameters

plaintext (str) – Plain text message without spaces or punctuation.

Returns

String with message encrypted using 3 rail fence cipher.

Module contents

Chapter 4 Challenge Projects.