Geocrack

Enigma_2

Jul 19th, 2022 (edited)
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.70 KB | None | 0 0
  1. def dafault():
  2.     # 2 mal das Alphabet in eine Liste packen
  3.     alphabet = [[chr(i) for i in range(65, 91)], [chr(i)
  4.                                                   for i in range(65, 91)]]
  5.     rotor_1, rotor_2 = rotor_update(alphabet) # die Grundeinstellungen der Rotoren berrechnen
  6.     return alphabet, rotor_1, rotor_2
  7.  
  8.  
  9. def rotor_update(alphabet):
  10.     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]}
  11.     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]}
  12.     return rotor_1, rotor_2
  13.  
  14.     # Grundeinstellungen der Rotoren
  15.     # könnte ich zwar auch in einer Liste mit 2 Dictionaries packen, aber das is
  16.  
  17.  
  18. def rotor_position(alphabet): # die Positionen der Rotoren werden festgelegt
  19.     for i in range(2):
  20.         position = (ord(
  21.             input(f"Enter the position of the {i+1}. rotor (A-Z): ").upper()) - 65)
  22.         for _ in range(position): # das Alphabet wird um n Positionen nach links verschoben
  23.             alphabet[i].append(alphabet[i][0]) # das Alphabet um eine Stelle nach links schieben
  24.             alphabet[i].pop(0)
  25.     return alphabet
  26.  
  27.  
  28. def encrypt_enigma(alphabet, rotor_1, rotor_2,message):
  29.     decripted_message = ""
  30.     rotor_1, rotor_2 = rotor_update(alphabet)
  31.     for item in message: # der Text wird verschlüsselt
  32.         if item == " ":
  33.             decripted_message += " "
  34.             continue
  35.         temp = rotor_1[item]
  36.         decripted_message += rotor_2[temp]
  37.         for x in range(2): # das Alphabet um eine Stelle nach links schieben
  38.             alphabet[x].append(alphabet[x][0])
  39.             alphabet[x].pop(0)
  40.         rotor_1, rotor_2 = rotor_update(alphabet)
  41.            
  42.     print(f"Your decripted_message is: \"{decripted_message}\"") # der Text wird ausgegeben
  43.     print("-"*50)
  44.  
  45.  
  46. def decrypt_enigma(alphabet, rotor_1, rotor_2, message):
  47.     encripted_message = ""
  48.     rotor_1, rotor_2 = rotor_update(alphabet)
  49.    
  50.     for key, value in rotor_1.items():
  51.         rotor_1[value] = key
  52.     for key, value in rotor_2.items():
  53.         rotor_2[value] = key
  54.    
  55.     for item in message: # der Text wird entschlüsselt
  56.         if item == " ":
  57.             decripted_message += " "
  58.             continue
  59.  
  60.         temp = rotor_2[item]
  61.         encripted_message += rotor_1[temp]
  62.         for x in range(2): # das Alphabet um eine Stelle nach links schieben
  63.             alphabet[x].append(alphabet[x][0])
  64.             alphabet[x].pop(0)
  65.             rotor_1, rotor_2 = rotor_update(alphabet)
  66.  
  67.     print(f"Your encripted_message is: \"{encripted_message}\"") # der Text wird ausgegeben
  68.     print("-"*50)
  69.  
  70.  
  71. def main(): # die Hauptfunktion
  72.     choice = None
  73.     while not choice == "x": # die Abfrage, ob das Programm beendet werden soll
  74.         alphabet, rotor_1, rotor_2 = dafault() # die Grundeinstellungen werden festgelegt
  75.         alphabet = rotor_position(alphabet) # die Positionen der Rotoren werden festgelegt
  76.         choice = input(
  77.             "Do you want to encrypt, decript or leave? (E/D/X): ").upper() # die Abfrage, was geschehen soll
  78.         print("-"*50)
  79.         if choice == "E": # wenn die Nachricht verschlüsselt werden soll
  80.             print("Encrypting...")
  81.             message = input("Enter the message: ").upper() # die Nachricht wird eingelesen
  82.             encrypt_enigma(alphabet, rotor_1, rotor_2,message)
  83.         elif choice == "D": # wenn die Nachricht entschlüsselt werden soll
  84.             print("Decrypt...")
  85.             message = input("Enter the message: ").upper() # die Nachricht wird eingelesen
  86.             decrypt_enigma(alphabet, rotor_1, rotor_2,message)
  87.         else:
  88.             print("Wrong input!")
  89.  
  90.  
  91. if __name__ == "__main__": # wenn das Programm direkt ausgeführt wird
  92.     main()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment