Krypto_Grundlagen/chapter_one/Multiplikative_Chiffre.py

66 lines
1.5 KiB
Python
Raw Normal View History

from utils import AlphabetUtils as au
ALLOWED_KEYS = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]
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 between 1 and 25 and a coprime to 26.
:return: The encrypted text
"""
# Key has to be coprime to 26
if key not in ALLOWED_KEYS:
raise AttributeError
resulting = ''
for char in cleartext:
index = au.get_index_of_letter(char)
newIndex = (index * key) % 26
resulting += au.get_letter_at_index(newIndex)
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 between 1 and 25 and a coprime to 26.
:return: The decrypted text
"""
# Key has to be coprime to 26
if key not in ALLOWED_KEYS:
raise AttributeError
decrypt_key = _get_key_reverse(key)
resulting = ''
for char in ciphertext:
index = au.get_index_of_letter(char)
newIndex = (index * decrypt_key) % 26
resulting += au.get_letter_at_index(newIndex)
return resulting
def _get_key_reverse(key: int):
"""
Calculates the decryption key (inverse of the original key) based on the following formula:
s * s' mod 26 = 1
:param key: The key to reverse
:return: The decryption key
"""
for possibility in ALLOWED_KEYS:
if (key * possibility) % 26 == 1:
return possibility
if __name__ == '__main__':
print(encrypt_text('BonkRocks', 11))
print(decrypt_text('lyngfywgq', 11))