diff --git a/chapter_one/Permutations-Chiffre.py b/chapter_one/Permutations-Chiffre.py index 153a091..dbcc97e 100644 --- a/chapter_one/Permutations-Chiffre.py +++ b/chapter_one/Permutations-Chiffre.py @@ -39,16 +39,33 @@ def decrypt_text(ciphertext: str, key: [int]): return resulting -def _generate_key(): +def generate_key(): + """ + Generates a key that can be used for this cipher. + :return: The key as a list of indices + """ indices = [i for i in range(26)] random.shuffle(indices) return indices +def generate_key_with_keyword(keyword: str): + result = [] + + for char in keyword: + charIndex = au.get_index_of_letter(char) + if charIndex not in result: + result.append(charIndex) + + remainingIndices = [i for i in range(26-len(result))] + + result.extend(remainingIndices) + + return result if __name__ == '__main__': - key = _generate_key() + key = generate_key_with_keyword('patrick') print(key) encrypted = encrypt_text('BonkRocks', key) print(encrypted)