Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def dafault():
- # 2 mal das Alphabet in eine Liste packen
- alphabet = [[chr(i) for i in range(65, 91)], [chr(i)
- for i in range(65, 91)]]
- rotor_1, rotor_2 = rotor_update(alphabet) # die Grundeinstellungen der Rotoren berrechnen
- return alphabet, rotor_1, rotor_2
- def rotor_update(alphabet):
- rotor_1 = {"A": alphabet[0][1], "B": alphabet[0][0],"C": alphabet[0][3], "D": alphabet[0][2], "E": alphabet[0][5], "F": alphabet[0][4], "G": alphabet[0][7], "H": alphabet[0][6], "I": alphabet[0][9], "J": alphabet[0][8], "K": alphabet[0][11], "L": alphabet[0][10], "M": alphabet[0][13], "N": alphabet[0][12], "O": alphabet[0][15], "P": alphabet[0][14], "Q": alphabet[0][17], "R": alphabet[0][16], "S": alphabet[0][19], "T": alphabet[0][18], "U": alphabet[0][21], "V": alphabet[0][20], "W": alphabet[0][23], "X": alphabet[0][22], "Y": alphabet[0][25], "Z": alphabet[0][24]}
- rotor_2 = {"A": alphabet[1][1], "B": alphabet[1][0],"C": alphabet[1][3], "D": alphabet[1][2], "E": alphabet[1][5], "F": alphabet[1][4], "G": alphabet[1][7], "H": alphabet[1][6], "I": alphabet[1][9], "J": alphabet[1][8], "K": alphabet[1][11], "L": alphabet[1][10], "M": alphabet[1][13], "N": alphabet[1][12], "O": alphabet[1][15], "P": alphabet[1][14], "Q": alphabet[1][17], "R": alphabet[1][16], "S": alphabet[1][19], "T": alphabet[1][18], "U": alphabet[1][21], "V": alphabet[1][20], "W": alphabet[1][23], "X": alphabet[1][22], "Y": alphabet[1][25], "Z": alphabet[1][24]}
- return rotor_1, rotor_2
- # Grundeinstellungen der Rotoren
- # könnte ich zwar auch in einer Liste mit 2 Dictionaries packen, aber das is
- def rotor_position(alphabet): # die Positionen der Rotoren werden festgelegt
- for i in range(2):
- position = (ord(
- input(f"Enter the position of the {i+1}. rotor (A-Z): ").upper()) - 65)
- for _ in range(position): # das Alphabet wird um n Positionen nach links verschoben
- alphabet[i].append(alphabet[i][0]) # das Alphabet um eine Stelle nach links schieben
- alphabet[i].pop(0)
- return alphabet
- def encrypt_enigma(alphabet, rotor_1, rotor_2,message):
- decripted_message = ""
- rotor_1, rotor_2 = rotor_update(alphabet)
- for item in message: # der Text wird verschlüsselt
- if item == " ":
- decripted_message += " "
- continue
- temp = rotor_1[item]
- decripted_message += rotor_2[temp]
- for x in range(2): # das Alphabet um eine Stelle nach links schieben
- alphabet[x].append(alphabet[x][0])
- alphabet[x].pop(0)
- rotor_1, rotor_2 = rotor_update(alphabet)
- print(f"Your decripted_message is: \"{decripted_message}\"") # der Text wird ausgegeben
- print("-"*50)
- def decrypt_enigma(alphabet, rotor_1, rotor_2, message):
- encripted_message = ""
- rotor_1, rotor_2 = rotor_update(alphabet)
- for key, value in rotor_1.items():
- rotor_1[value] = key
- for key, value in rotor_2.items():
- rotor_2[value] = key
- for item in message: # der Text wird entschlüsselt
- if item == " ":
- decripted_message += " "
- continue
- temp = rotor_2[item]
- encripted_message += rotor_1[temp]
- for x in range(2): # das Alphabet um eine Stelle nach links schieben
- alphabet[x].append(alphabet[x][0])
- alphabet[x].pop(0)
- rotor_1, rotor_2 = rotor_update(alphabet)
- print(f"Your encripted_message is: \"{encripted_message}\"") # der Text wird ausgegeben
- print("-"*50)
- def main(): # die Hauptfunktion
- choice = None
- while not choice == "x": # die Abfrage, ob das Programm beendet werden soll
- alphabet, rotor_1, rotor_2 = dafault() # die Grundeinstellungen werden festgelegt
- alphabet = rotor_position(alphabet) # die Positionen der Rotoren werden festgelegt
- choice = input(
- "Do you want to encrypt, decript or leave? (E/D/X): ").upper() # die Abfrage, was geschehen soll
- print("-"*50)
- if choice == "E": # wenn die Nachricht verschlüsselt werden soll
- print("Encrypting...")
- message = input("Enter the message: ").upper() # die Nachricht wird eingelesen
- encrypt_enigma(alphabet, rotor_1, rotor_2,message)
- elif choice == "D": # wenn die Nachricht entschlüsselt werden soll
- print("Decrypt...")
- message = input("Enter the message: ").upper() # die Nachricht wird eingelesen
- decrypt_enigma(alphabet, rotor_1, rotor_2,message)
- else:
- print("Wrong input!")
- if __name__ == "__main__": # wenn das Programm direkt ausgeführt wird
- main()
Advertisement
Add Comment
Please, Sign In to add comment