Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions ciphers/simple_substitution_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ def main() -> None:


def check_valid_key(key: str) -> None:
"""
Check if the key is valid (contains all 26 letters of the alphabet exactly once).
Exits the program if the key is invalid.

>>> check_valid_key('LFWOAYUISVKMNXPBDCRJTQEGHZ')
>>> check_valid_key('INVALIDKEY')
Traceback (most recent call last):
...
SystemExit: Error in the key or symbol set.
"""
key_list = list(key)
letters_list = list(LETTERS)
key_list.sort()
Expand Down Expand Up @@ -69,6 +79,13 @@ def translate_message(key: str, message: str, mode: str) -> str:


def get_random_key() -> str:
"""
Generate a random substitution cipher key.

>>> random.seed(0)
>>> get_random_key()
'OAXSGFHKWUECVDRLTJZPQIBNYM'
"""
key = list(LETTERS)
random.shuffle(key)
return "".join(key)
Expand Down