Krypto_Grundlagen/chapter_one/Verschiebe-Chiffre.py

60 lines
2.0 KiB
Python
Raw Permalink Normal View History

from utils import CipherUtils
2021-10-15 11:29:56 +00:00
def encrypt_text(cleartext: str = 'Bitcoin', incrementation: int = 13):
"""
This method encrypts a text by shifting each letter by the value of incrementation
:param cleartext: The secret message to encrypt
:param incrementation: How much the letters should be shifted
:return: Encrypted text
"""
return shift_text(cleartext, (incrementation % 26)) # '%' to repeat after 26 with 0
def decrypt_text(encrypted_text: str = 'Ovgpbva', incrementation: int = 13):
"""
This method decrypts a text by shifting each letter by the value of incrementation
:param encrypted_text: The encrypted secret message to decrypt
:param incrementation: How much the letters should be shifted
:return: Decrypted text
"""
return shift_text(encrypted_text, 26 - (incrementation % 26)) # '%' to repeat after 26 with 0
def shift_text(cleartext: str, incrementation: int):
"""
This method shifts every letter of a string by the value of incrementation
:param cleartext: The string to be shifted
:param incrementation: How much the letters should be shifted
:return: Shifted text
"""
shifted_text = ''
2021-10-15 11:29:56 +00:00
for char in cleartext:
shifted_text += CipherUtils.shift_char(char, incrementation)
2021-10-15 11:29:56 +00:00
return shifted_text
2021-10-15 11:29:56 +00:00
def brute_force_example(cleartext: str = 'Bitcoin', incrementation: int = 7):
"""
This is an example of how to bruteforce an encrypted text. (Encrypted with Letter-Shifting)
2021-10-15 11:29:56 +00:00
:param cleartext: The secret message to encrypt and decrypt during bruteforcing
:param incrementation: How much the letters should be shifted
"""
encrypted_text = encrypt_text(cleartext, incrementation=incrementation)
2021-10-15 11:29:56 +00:00
for x in range(26):
if cleartext == decrypt_text(encrypted_text, incrementation=x):
print(f'Text was shifted by {x} letters.')
2021-10-15 11:29:56 +00:00
if __name__ == '__main__':
print(encrypt_text())
print(decrypt_text())
brute_force_example()