parent
d2c39984fd
commit
9c30250122
@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (python)" project-jdk-type="Python SDK" />
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (UNI_Python)" project-jdk-type="Python SDK" />
|
||||
</project>
|
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -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)
|
Loading…
Reference in new issue