2021-10-15 15:25:08 +00:00
|
|
|
import random
|
|
|
|
|
|
|
|
from utils import AlphabetUtils as au
|
2021-10-17 14:54:25 +00:00
|
|
|
from utils import CipherUtils as cu
|
2021-10-15 15:25:08 +00:00
|
|
|
|
|
|
|
|
2021-10-17 14:54:25 +00:00
|
|
|
def encrypt_text(cleartext: str, key: [int]) -> str:
|
2021-10-15 15:25:08 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
2021-10-17 14:54:25 +00:00
|
|
|
|
|
|
|
cleartext = cu.transform_invalid_chars(cleartext)
|
|
|
|
|
2021-10-15 15:25:08 +00:00
|
|
|
resulting = ''
|
|
|
|
|
|
|
|
for char in cleartext:
|
2021-10-15 15:57:35 +00:00
|
|
|
char_index = au.get_index_of_letter(char)
|
|
|
|
cipher_index = key.index(char_index)
|
|
|
|
cipher_char = au.get_letter_at_index(cipher_index)
|
|
|
|
resulting += cipher_char
|
2021-10-15 15:25:08 +00:00
|
|
|
|
|
|
|
return resulting
|
|
|
|
|
|
|
|
|
2021-10-17 14:54:25 +00:00
|
|
|
def decrypt_text(ciphertext: str, key: [int]) -> str:
|
2021-10-15 15:25:08 +00:00
|
|
|
"""
|
|
|
|
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:
|
2021-10-15 15:57:35 +00:00
|
|
|
char_index = au.get_index_of_letter(char)
|
|
|
|
clear_index = key[char_index]
|
|
|
|
clear_char = au.get_letter_at_index(clear_index)
|
|
|
|
resulting += clear_char
|
2021-10-15 15:25:08 +00:00
|
|
|
|
|
|
|
return resulting
|
|
|
|
|
|
|
|
|
2021-10-17 14:54:25 +00:00
|
|
|
def generate_key() -> [int]:
|
2021-10-15 15:55:16 +00:00
|
|
|
"""
|
|
|
|
Generates a key that can be used for this cipher.
|
|
|
|
:return: The key as a list of indices
|
|
|
|
"""
|
2021-10-15 15:25:08 +00:00
|
|
|
indices = [i for i in range(26)]
|
|
|
|
|
|
|
|
random.shuffle(indices)
|
|
|
|
|
|
|
|
return indices
|
|
|
|
|
2021-10-15 15:57:35 +00:00
|
|
|
|
2021-10-17 14:54:25 +00:00
|
|
|
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
|
|
|
|
"""
|
2021-10-15 15:55:16 +00:00
|
|
|
result = []
|
|
|
|
|
|
|
|
for char in keyword:
|
2021-10-15 15:57:35 +00:00
|
|
|
char_index = au.get_index_of_letter(char)
|
|
|
|
if char_index not in result:
|
|
|
|
result.append(char_index)
|
2021-10-15 15:55:16 +00:00
|
|
|
|
2021-10-15 15:57:35 +00:00
|
|
|
remaining_indices = [i for i in range(26 - len(result))]
|
2021-10-15 15:55:16 +00:00
|
|
|
|
2021-10-15 15:57:35 +00:00
|
|
|
result.extend(remaining_indices)
|
2021-10-15 15:55:16 +00:00
|
|
|
|
|
|
|
return result
|
2021-10-15 15:25:08 +00:00
|
|
|
|
2021-10-15 15:57:35 +00:00
|
|
|
|
2021-10-15 15:25:08 +00:00
|
|
|
if __name__ == '__main__':
|
2021-10-15 15:55:16 +00:00
|
|
|
key = generate_key_with_keyword('patrick')
|
2021-10-15 15:25:08 +00:00
|
|
|
print(key)
|
|
|
|
encrypted = encrypt_text('BonkRocks', key)
|
|
|
|
print(encrypted)
|
|
|
|
print(decrypt_text(encrypted, key))
|