ciphers.skytale_cipher

Scytale (Skytale) transposition cipher.

A classical transposition cipher used in ancient Greece. The sender wraps a strip of parchment around a rod (scytale) and writes the message along the rod. The recipient with a rod of the same diameter can read the message.

Reference: https://en.wikipedia.org/wiki/Scytale

Functions here keep characters as-is (including spaces). The key is a positive integer representing the circumference count (number of rows).

>>> encrypt("WE ARE DISCOVERED FLEE AT ONCE", 3)
'WA SVEFETNERDCEDL  C EIOR EAOE'
>>> decrypt('WA SVEFETNERDCEDL  C EIOR EAOE', 3)
'WE ARE DISCOVERED FLEE AT ONCE'

Edge cases: >>> encrypt(“HELLO”, 1) ‘HELLO’ >>> decrypt(“HELLO”, 1) ‘HELLO’ >>> encrypt(“HELLO”, 5) # key equals length ‘HELLO’ >>> decrypt(“HELLO”, 5) ‘HELLO’ >>> encrypt(“HELLO”, 0) Traceback (most recent call last):

ValueError: Key must be a positive integer >>> decrypt(“HELLO”, -2) Traceback (most recent call last):

ValueError: Key must be a positive integer

Functions

decrypt(→ str)

Decrypt Scytale ciphertext.

encrypt(→ str)

Encrypt plaintext using Scytale transposition.

Module Contents

ciphers.skytale_cipher.decrypt(ciphertext: str, key: int) str

Decrypt Scytale ciphertext.

Reconstruct rows by their lengths and interleave by columns.

Parameters:
  • ciphertext – Encrypted string

  • key – Positive integer number of rows

Returns:

Decrypted plaintext

Raises:

ValueError – if key <= 0

ciphers.skytale_cipher.encrypt(plaintext: str, key: int) str

Encrypt plaintext using Scytale transposition.

Write characters around a rod with key rows, then read off by rows.

Parameters:
  • plaintext – Input message to encrypt

  • key – Positive integer number of rows

Returns:

Ciphertext string

Raises:

ValueError – if key <= 0