#7: Implementing Schlüsselwort-Chiffre

This commit is contained in:
Patrick Müller 2021-10-15 17:55:16 +02:00
parent 8373534224
commit 80ebb362a5

View File

@ -39,16 +39,33 @@ def decrypt_text(ciphertext: str, key: [int]):
return resulting 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)] indices = [i for i in range(26)]
random.shuffle(indices) random.shuffle(indices)
return 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__': if __name__ == '__main__':
key = _generate_key() key = generate_key_with_keyword('patrick')
print(key) print(key)
encrypted = encrypt_text('BonkRocks', key) encrypted = encrypt_text('BonkRocks', key)
print(encrypted) print(encrypted)