Adding util method to replace invalid input characters

- Also better type hints
This commit is contained in:
2021-10-17 16:54:25 +02:00
parent d4bd9638a4
commit 2bd8dbc6db
4 changed files with 74 additions and 11 deletions
+6 -3
View File
@@ -1,9 +1,10 @@
from utils import AlphabetUtils as au
from utils import CipherUtils as cu
ALLOWED_KEYS = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]
def encrypt_text(cleartext: str, key: int):
def encrypt_text(cleartext: str, key: int) -> str:
"""
Encrypts the given text with the given key
:param cleartext: The text to encrypt
@@ -14,6 +15,8 @@ def encrypt_text(cleartext: str, key: int):
if key not in ALLOWED_KEYS:
raise AttributeError
cleartext = cu.transform_invalid_chars(cleartext)
resulting = ''
for char in cleartext:
@@ -24,7 +27,7 @@ def encrypt_text(cleartext: str, key: int):
return resulting
def decrypt_text(ciphertext: str, key: int):
def decrypt_text(ciphertext: str, key: int) -> str:
"""
Decrypts the given ciphertext with the given key
:param ciphertext: The text to decrypt
@@ -47,7 +50,7 @@ def decrypt_text(ciphertext: str, key: int):
return resulting
def _get_key_reverse(key: int):
def _get_key_reverse(key: int) -> int:
"""
Calculates the decryption key (inverse of the original key) based on the following formula:
s * s' mod 26 = 1
+13 -4
View File
@@ -1,15 +1,19 @@
import random
from utils import AlphabetUtils as au
from utils import CipherUtils as cu
def encrypt_text(cleartext: str, key: [int]):
def encrypt_text(cleartext: str, key: [int]) -> str:
"""
Encrypts the given text with the given key
:param cleartext: The text to encrypt
:param key: The key to use. Has to be a list of indices of the alphabet, so [0...25] shuffled
:return: The encrypted text
"""
cleartext = cu.transform_invalid_chars(cleartext)
resulting = ''
for char in cleartext:
@@ -21,7 +25,7 @@ def encrypt_text(cleartext: str, key: [int]):
return resulting
def decrypt_text(ciphertext: str, key: [int]):
def decrypt_text(ciphertext: str, key: [int]) -> str:
"""
Decrypts the given ciphertext with the given key
:param ciphertext: The text to decrypt
@@ -39,7 +43,7 @@ def decrypt_text(ciphertext: str, key: [int]):
return resulting
def generate_key():
def generate_key() -> [int]:
"""
Generates a key that can be used for this cipher.
:return: The key as a list of indices
@@ -51,7 +55,12 @@ def generate_key():
return indices
def generate_key_with_keyword(keyword: str):
def generate_key_with_keyword(keyword: str) -> [int]:
"""
Generates a key that can be used for this cipher by using the given keyword as a starting point
:param keyword: The keyword to use to generate the key
:return: The key as a list of indices
"""
result = []
for char in keyword:
+6 -3
View File
@@ -1,9 +1,10 @@
from utils import AlphabetUtils as au
from utils import CipherUtils as cu
ALLOWED_KEYS = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]
def encrypt_text(cleartext: str, key1: int, key2: int):
def encrypt_text(cleartext: str, key1: int, key2: int) -> str:
"""
Encrypts the given text with the given keys
:param cleartext: The text to encrypt
@@ -19,6 +20,8 @@ def encrypt_text(cleartext: str, key1: int, key2: int):
if key2 < 0 or key2 > 25:
raise AttributeError
cleartext = cu.transform_invalid_chars(cleartext)
resulting = ''
for char in cleartext:
@@ -29,7 +32,7 @@ def encrypt_text(cleartext: str, key1: int, key2: int):
return resulting
def decrypt_text(ciphertext: str, key1: int, key2: int):
def decrypt_text(ciphertext: str, key1: int, key2: int) -> str:
"""
Decrypts the given ciphertext with the given keys
:param ciphertext: The text to decrypt
@@ -57,7 +60,7 @@ def decrypt_text(ciphertext: str, key1: int, key2: int):
return resulting
def _get_key_reverse(key: int):
def _get_key_reverse(key: int) -> int:
"""
Calculates the decryption key (inverse of the original key) based on the following formula:
s * s' mod 26 = 1