Geocrack

Enigma

Jul 19th, 2022
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 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.  
  6.     rotor_1 = {"A": alphabet[0][18], "B": alphabet[0][16], "C": alphabet[0][15], "D": alphabet[0][5], "E": alphabet[0][25], "F": alphabet[0][0], "G": alphabet[0][14], "H": alphabet[0][23], "I": alphabet[0][8], "J": alphabet[0][20], "K": alphabet[0][12], "L": alphabet[0][10], "M": alphabet[0]
  7.                [1], "N": alphabet[0][11], "O": alphabet[0][21], "P": alphabet[0][7], "Q": alphabet[0][3], "R": alphabet[0][4], "S": alphabet[0][24], "T": alphabet[0][17], "U": alphabet[0][13], "V": alphabet[0][22], "W": alphabet[0][2], "X": alphabet[0][6], "Y": alphabet[0][9], "Z": alphabet[0][19]}
  8.     # Grundeinstellungen der Rotoren
  9.     # könnte ich zwar auch in einer Liste mit 2 Dictionaries packen, aber das ist schöner
  10.     rotor_2 = {"A": alphabet[1][20], "B": alphabet[1][15], "C": alphabet[1][2], "D": alphabet[1][8], "E": alphabet[1][13], "F": alphabet[1][1], "G": alphabet[1][6], "H": alphabet[1][9], "I": alphabet[1][5], "J": alphabet[1][25], "K": alphabet[1][23], "L": alphabet[1][12], "M": alphabet[1][24],
  11.                "N": alphabet[1][0], "O": alphabet[1][14], "P": alphabet[1][22], "Q": alphabet[1][17], "R": alphabet[1][7], "S": alphabet[1][21], "T": alphabet[1][3], "U": alphabet[1][18], "V": alphabet[1][11], "W": alphabet[1][16], "X": alphabet[1][4], "Y": alphabet[1][19], "Z": alphabet[1][10], }
  12.     return alphabet, rotor_1, rotor_2
  13.  
  14.  
  15. def rotor_position(alphabet): # die Positionen der Rotoren werden festgelegt
  16.     for i in range(2):
  17.         position = (ord(
  18.             input(f"Enter the position of the {i+1}. rotor: ").upper()) - 65)
  19.  
  20.         for _ in range(position):
  21.             alphabet[i].append(alphabet[i][0])
  22.             alphabet[i].pop(0)
  23.     return alphabet
  24.  
  25.  
  26. def decript_enigma(alphabet, rotor_1, rotor_2):
  27.     print("-"*50)
  28.     print("Decripting...")
  29.     message = input("Enter the message: ").upper()
  30.     decripted_message = ""
  31.     temp = rotor_1[message[0]]
  32.     decripted_message += rotor_2[temp]
  33.  
  34.     for i in message[1:]:
  35.         for x in range(2): # das Alphabet um eine Stelle nach links schieben
  36.             alphabet[x].append(alphabet[x][0])
  37.             alphabet[x].pop(0)
  38.         temp = rotor_1[i]
  39.         decripted_message += rotor_2[temp]
  40.     print(decripted_message)
  41.  
  42.  
  43. def encript_enigma(alphabet, rotor_1, rotor_2):
  44.     print("-"*50)
  45.     print("Encrypting...")
  46.     message = input("Enter the message: ").upper()
  47.     encripted_message = ""
  48.     temp = rotor_2[message[0]]
  49.     encripted_message += rotor_1[temp]
  50.  
  51.     for i in message[1:]:
  52.         for x in range(2): # das Alphabet um eine Stelle nach links schieben
  53.             alphabet[x].append(alphabet[x][0])
  54.             alphabet[x].pop(0)
  55.         temp = rotor_2[i]
  56.         encripted_message += rotor_1[temp]
  57.     print(encripted_message)
  58.  
  59.  
  60. def main():
  61.     choice = None
  62.     while not choice == "x":
  63.         alphabet, rotor_1, rotor_2 = dafault()
  64.         alphabet = rotor_position(alphabet)
  65.         choice = input(
  66.             "Do you want to decript, encrypt or leave? (D/E/X) ").upper()
  67.         if choice == "D":
  68.             decript_enigma(alphabet, rotor_1, rotor_2)
  69.         elif choice == "E":
  70.             encript_enigma(alphabet, rotor_1, rotor_2)
  71.         else:
  72.             print("Wrong input!")
  73.  
  74.  
  75. if __name__ == "__main__":
  76.     main()
  77.  
Advertisement
Add Comment
Please, Sign In to add comment