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 = {"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]
- [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]}
- # Grundeinstellungen der Rotoren
- # könnte ich zwar auch in einer Liste mit 2 Dictionaries packen, aber das ist schöner
- 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],
- "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], }
- return alphabet, rotor_1, rotor_2
- 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: ").upper()) - 65)
- for _ in range(position):
- alphabet[i].append(alphabet[i][0])
- alphabet[i].pop(0)
- return alphabet
- def decript_enigma(alphabet, rotor_1, rotor_2):
- print("-"*50)
- print("Decripting...")
- message = input("Enter the message: ").upper()
- decripted_message = ""
- temp = rotor_1[message[0]]
- decripted_message += rotor_2[temp]
- for i in message[1:]:
- for x in range(2): # das Alphabet um eine Stelle nach links schieben
- alphabet[x].append(alphabet[x][0])
- alphabet[x].pop(0)
- temp = rotor_1[i]
- decripted_message += rotor_2[temp]
- print(decripted_message)
- def encript_enigma(alphabet, rotor_1, rotor_2):
- print("-"*50)
- print("Encrypting...")
- message = input("Enter the message: ").upper()
- encripted_message = ""
- temp = rotor_2[message[0]]
- encripted_message += rotor_1[temp]
- for i in message[1:]:
- for x in range(2): # das Alphabet um eine Stelle nach links schieben
- alphabet[x].append(alphabet[x][0])
- alphabet[x].pop(0)
- temp = rotor_2[i]
- encripted_message += rotor_1[temp]
- print(encripted_message)
- def main():
- choice = None
- while not choice == "x":
- alphabet, rotor_1, rotor_2 = dafault()
- alphabet = rotor_position(alphabet)
- choice = input(
- "Do you want to decript, encrypt or leave? (D/E/X) ").upper()
- if choice == "D":
- decript_enigma(alphabet, rotor_1, rotor_2)
- elif choice == "E":
- encript_enigma(alphabet, rotor_1, rotor_2)
- else:
- print("Wrong input!")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment