From ee73aaafa7d827b574b58231a27e95327e912807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Fri, 15 Oct 2021 17:25:08 +0200 Subject: [PATCH] #6: Implementing Permutations-Chiffre - Also some refactoring to other ciphers --- chapter_one/Multiplikative_Chiffre.py | 12 +++--- chapter_one/Permutations-Chiffre.py | 55 +++++++++++++++++++++++++++ chapter_one/Tausch-Chiffre.py | 12 +++--- 3 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 chapter_one/Permutations-Chiffre.py diff --git a/chapter_one/Multiplikative_Chiffre.py b/chapter_one/Multiplikative_Chiffre.py index 5157422..1dc57b3 100644 --- a/chapter_one/Multiplikative_Chiffre.py +++ b/chapter_one/Multiplikative_Chiffre.py @@ -3,11 +3,11 @@ from utils import AlphabetUtils as au ALLOWED_KEYS = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25] -def encrypt_text(key: int = 11, cleartext: str = 'BonkRocks'): +def encrypt_text(cleartext: str, key: int): """ Encrypts the given text with the given key - :param key: The key to use. Has to be between 1 and 25 and a coprime to 26. :param cleartext: The text to encrypt + :param key: The key to use. Has to be between 1 and 25 and a coprime to 26. :return: The encrypted text """ # Key has to be coprime to 26 @@ -24,11 +24,11 @@ def encrypt_text(key: int = 11, cleartext: str = 'BonkRocks'): return resulting -def decrypt_text(key: int = 11, ciphertext: str = 'lyngfywgq'): +def decrypt_text(ciphertext: str, key: int): """ Decrypts the given ciphertext with the given key - :param key: The key to use. Has to be between 1 and 25 and a coprime to 26. :param ciphertext: The text to decrypt + :param key: The key to use. Has to be between 1 and 25 and a coprime to 26. :return: The decrypted text """ # Key has to be coprime to 26 @@ -61,5 +61,5 @@ def _get_key_reverse(key: int): if __name__ == '__main__': - print(encrypt_text()) - print(decrypt_text()) + print(encrypt_text('BonkRocks', 11)) + print(decrypt_text('lyngfywgq', 11)) diff --git a/chapter_one/Permutations-Chiffre.py b/chapter_one/Permutations-Chiffre.py new file mode 100644 index 0000000..153a091 --- /dev/null +++ b/chapter_one/Permutations-Chiffre.py @@ -0,0 +1,55 @@ +import random + +from utils import AlphabetUtils as au + + +def encrypt_text(cleartext: str, key: [int]): + """ + 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 + """ + resulting = '' + + for char in cleartext: + charIndex = au.get_index_of_letter(char) + cipherIndex = key.index(charIndex) + cipherChar = au.get_letter_at_index(cipherIndex) + resulting += cipherChar + + return resulting + + +def decrypt_text(ciphertext: str, key: [int]): + """ + Decrypts the given ciphertext with the given key + :param ciphertext: The text to decrypt + :param key: The key to use. Has to be a list of indices of the alphabet, so [0...25] shuffled + :return: The decrypted text + """ + resulting = '' + + for char in ciphertext: + charIndex = au.get_index_of_letter(char) + clearIndex = key[charIndex] + clearChar = au.get_letter_at_index(clearIndex) + resulting += clearChar + + return resulting + + +def _generate_key(): + indices = [i for i in range(26)] + + random.shuffle(indices) + + return indices + + +if __name__ == '__main__': + key = _generate_key() + print(key) + encrypted = encrypt_text('BonkRocks', key) + print(encrypted) + print(decrypt_text(encrypted, key)) diff --git a/chapter_one/Tausch-Chiffre.py b/chapter_one/Tausch-Chiffre.py index 761d30c..03fd32f 100644 --- a/chapter_one/Tausch-Chiffre.py +++ b/chapter_one/Tausch-Chiffre.py @@ -3,12 +3,12 @@ from utils import AlphabetUtils as au ALLOWED_KEYS = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25] -def encrypt_text(key1: int = 11, key2: int = 7, cleartext: str = 'BonkRocks'): +def encrypt_text(cleartext: str, key1: int, key2: int): """ Encrypts the given text with the given keys + :param cleartext: The text to encrypt :param key1: The first key. Has to be between 1 and 25 and a coprime to 26 :param key2: The second key. Has to be between 0 and 26 - :param cleartext: The text to encrypt :return: The encrypted text """ # key1 has to be coprime to 26 @@ -29,12 +29,12 @@ def encrypt_text(key1: int = 11, key2: int = 7, cleartext: str = 'BonkRocks'): return resulting -def decrypt_text(key1: int = 11, key2: int = 7, ciphertext: str = 'sfunmfdnx'): +def decrypt_text(ciphertext: str, key1: int, key2: int): """ Decrypts the given ciphertext with the given keys + :param ciphertext: The text to decrypt :param key1: The first key. Has to be between 1 and 25 and a coprime to 26 :param key2: The second key. Has to be between 0 and 26 - :param ciphertext: The text to decrypt :return: The decrypted text """ # key1 has to be coprime to 26 @@ -71,5 +71,5 @@ def _get_key_reverse(key: int): if __name__ == '__main__': - print(encrypt_text()) - print(decrypt_text()) + print(encrypt_text('BonkRocks', 11, 7)) + print(decrypt_text('sfunmfdnx', 11, 7))