ciphers.columnar_transposition

Columnar Transposition cipher.

This classical cipher writes the plaintext in rows under a keyword and reads columns in the order of the alphabetical rank of the keyword letters.

Reference: https://en.wikipedia.org/wiki/Transposition_cipher#Columnar_transposition

We keep spaces and punctuation. Key must be alphabetic (case-insensitive).

>>> pt = "WE ARE DISCOVERED. FLEE AT ONCE"
>>> ct = encrypt(pt, "ZEBRAS")
>>> decrypt(ct, "ZEBRAS") == pt
True

Edge cases: >>> encrypt(“HELLO”, “A”) ‘HELLO’ >>> decrypt(“HELLO”, “A”) ‘HELLO’ >>> encrypt(“HELLO”, “HELLO”) ‘EHLLO’ >>> decrypt(“EHLLO”, “HELLO”) ‘HELLO’ >>> encrypt(“HELLO”, “”) Traceback (most recent call last):

ValueError: Key must be a non-empty alphabetic string

Functions

_column_order(→ list[int])

_normalize_key(→ str)

decrypt(→ str)

Decrypt columnar transposition ciphertext.

encrypt(→ str)

Encrypt using columnar transposition.

Module Contents

ciphers.columnar_transposition._column_order(key: str) list[int]
ciphers.columnar_transposition._normalize_key(key: str) str
ciphers.columnar_transposition.decrypt(ciphertext: str, key: str) str

Decrypt columnar transposition ciphertext.

Parameters:
  • ciphertext – Encrypted text

  • key – Alphabetic keyword

Returns:

Decrypted plaintext

Raises:

ValueError – on invalid key

ciphers.columnar_transposition.encrypt(plaintext: str, key: str) str

Encrypt using columnar transposition.

Parameters:
  • plaintext – Input text (any characters)

  • key – Alphabetic keyword

Returns:

Ciphertext

Raises:

ValueError – on invalid key