From 9c302501224dced3aaa5263f1dcb68401cc00553 Mon Sep 17 00:00:00 2001 From: Loosen-IT Date: Fri, 10 Nov 2023 19:46:21 +0100 Subject: [PATCH] Uebung 03 --- .idea/misc.xml | 2 +- .idea/python.iml | 3 +- .idea/vcs.xml | 6 ++ ha_02/loosen_janniclas_1540907_03.py | 114 +++++++++++++++++++++++++++ 4 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 ha_02/loosen_janniclas_1540907_03.py diff --git a/.idea/misc.xml b/.idea/misc.xml index 8961d95..ce0c400 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/.idea/python.iml b/.idea/python.iml index 74d515a..b567092 100644 --- a/.idea/python.iml +++ b/.idea/python.iml @@ -2,9 +2,10 @@ + - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ha_02/loosen_janniclas_1540907_03.py b/ha_02/loosen_janniclas_1540907_03.py new file mode 100644 index 0000000..da9c9e2 --- /dev/null +++ b/ha_02/loosen_janniclas_1540907_03.py @@ -0,0 +1,114 @@ +# APPROACH: All keyboard keys can be interpreted as an ASCII-Code. +# => Shift them on the ascii table, with the factor of shift +# COMMENT: Admittedly, this cipher everything else then secure, but it serves its purpose. +# => This implementation can create 256 different encryption mappings +import time + + +def shift_cipher(shift): + codes = {} + for char_code in range(0, 255): # all ASCII-letters + char = chr((char_code + shift) % 255) + codes[chr(char_code)] = char + return codes + + +# OBSERVATION: If the encryption map is known, anyone can decode the text. +# => Security of this procedure depends on how many combinations the cipher algorithm creates. +# COMMENT: Thanks to the weak cipher, a brute force attack cracks the encryption after a maximum of 256 attempts. +def caesar(text, cipher, mode): + mode = mode.upper() + if mode not in ["ENCODE", "DECODE"]: # Use 'in' to check for multiple conditions + return text + + tokens = list(text) + if mode == "DECODE": + cipher = {value: key for key, value in cipher.items()} + + for i in range(len(tokens)): + if tokens[i] in cipher: # Check if the character exists in the cipher before replacing + tokens[i] = cipher[tokens[i]] + return "".join(tokens) + + +# COMMENT: Ciphers are binary bijective mappings over an alphabet A, of which |A|! exist (for ours 256!). +# In practice, security should be strengthened through better cipher algorithms. +# Because our cipher works so poorly, here is an optimized variant of the caesar process. +# APPROACH: Set a starting position for the encryption and then encrypt recursively to the right and left. +# => An Encryption can only be successful, if the starting point is known. +# => An brute force attack cracks the encryption after a maximum of 256*len(text) attempts. +def caesar_amplified(text, pos, cipher, mode): + mode = mode.upper() + if mode not in ["ENCODE", "DECODE"]: # Use 'in' to check for multiple conditions + return text + + tokens = list(text) + if mode == "DECODE": + cipher = {value: key for key, value in cipher.items()} + + tokens[pos] = cipher[tokens[pos]] + tokens = caesar_recursive(tokens, pos - 1, cipher, "l") # start left recursion + tokens = caesar_recursive(tokens, pos + 1, cipher, "r") # start right recursion + return "".join(tokens) + + +def caesar_recursive(tokens, pos, cipher, mode): + if pos == -1 or pos == len(tokens): + return tokens + cipher = update_cipher(cipher) + tokens[pos] = cipher[tokens[pos]] + if mode == "l": + return caesar_recursive(tokens, pos - 1, cipher, "l") + else: + return caesar_recursive(tokens, pos + 1, cipher, "r") + + +def update_cipher(codes_old): + codes_new = {} + for key, value in codes_old.items(): + codes_new[key] = codes_old[codes_old[key]] + return codes_new + + +# Write message into these lines: +string = ''' +Liebe/r Empfänger, + +Herzliche Grüße aus dem hohen Norden! Ich hoffe, diese Nachricht erreicht dich in guter Verfassung und fügt einen Hauch +von festlicher Stimmung zu deinem Tag hinzu. + +Santa und seine Wichtel arbeiten fleißig daran, diese Jahreszeit für alle magisch zu gestalten. Die Werkstatt ist voller +Aktivität, während Spielzeuge mit Sorgfalt hergestellt werden. + +Ich wünsche dir Freude, Gelächter und einen festlichen Geist, der dein Herz erwärmt. Genieße die Magie der Feiertage! + +Herzliche Grüße, +Rudolph +''' +enc_time = time.time() +encoded = caesar(string, shift_cipher(2), "ENCODE") +enc_time = (time.time() - enc_time) * 1000 # Convert to milliseconds + +dec_time = time.time() +decoded = caesar(encoded, shift_cipher(2), "DECODE") +dec_time = (time.time() - dec_time) * 1000 # Convert to milliseconds + +print("Encoded Caesar-Encryption: (calculated in " + str(enc_time) + "ms)") +print(encoded + "\n") +print("Decoded Caesar-Encryption: (calculated in " + str(dec_time) + "ms)") +print(decoded) + +print("\n") + +enc_rec_time = time.time() +encoded_rec = caesar_amplified(string, 5, shift_cipher(2), "ENCODE") +enc_rec_time = (time.time() - enc_rec_time) * 1000 # Convert to milliseconds + +dec_rec_time = time.time() +decoded_rec = caesar_amplified(encoded_rec, 5, shift_cipher(2), "DECODE") +dec_rec_time = (time.time() - dec_rec_time) * 1000 # Convert to milliseconds + +print("Encoded Caesar-Encryption amplified: (calculated in " + str(enc_rec_time) + "ms)") +print(encoded_rec + "\n") +print("Decoded Caesar-Encryption amplified: (calculated in " + str(dec_rec_time) + "ms)") +print(decoded_rec)