n | def rotor (text, rotor_position): | n | def rotor(text, rotor_position): |
| converted_text = str() | | converted_text = '' |
| for character in text: | | for character in text: |
| if character == ' ': | | if character == ' ': |
| converted_text += character | | converted_text += character |
| else: | | else: |
| converted_text += rotor_position[character] | | converted_text += rotor_position[character] |
| return converted_text | | return converted_text |
| | | |
| | | |
n | def plugboard (text, plugboard_position): | n | def plugboard(text, plugboard_position): |
| new_list = [] | | new_list = [] |
| for each_list in plugboard_position: | | for each_list in plugboard_position: |
| new_list.append(list(each_list)) | | new_list.append(list(each_list)) |
| | | |
| new_dict = {} | | new_dict = {} |
| for each_list in new_list: | | for each_list in new_list: |
| new_dict[each_list[0]] = each_list[-1] | | new_dict[each_list[0]] = each_list[-1] |
| new_dict[each_list[-1]] = each_list[0] | | new_dict[each_list[-1]] = each_list[0] |
| | | |
n | converted_text = str() | n | converted_text = '' |
| for character in text: | | for character in text: |
t | if character in new_dict: | t | |
| converted_text += new_dict[character] | | converted_text += new_dict.get(character, character) |
| else: | | |
| converted_text += character | | |
| return converted_text | | return converted_text |
| | | |
| | | |
| def enigma_encrypt(plugboard_position, rotor_position): | | def enigma_encrypt(plugboard_position, rotor_position): |
| def decorator(func): | | def decorator(func): |
| def encrypt_text(text): | | def encrypt_text(text): |
| text = rotor(plugboard(text, plugboard_position), rotor_position) | | text = rotor(plugboard(text, plugboard_position), rotor_position) |
| return func(text) | | return func(text) |
| return encrypt_text | | return encrypt_text |
| return decorator | | return decorator |
| | | |
| | | |
| def enigma_decrypt(plugboard_position, rotor_position): | | def enigma_decrypt(plugboard_position, rotor_position): |
| def decorator(func): | | def decorator(func): |
| def decrypt_text(text): | | def decrypt_text(text): |
| new_rotor_position = {value: key for key, value in rotor_position.items()} | | new_rotor_position = {value: key for key, value in rotor_position.items()} |
| text = plugboard(rotor(text, new_rotor_position), plugboard_position) | | text = plugboard(rotor(text, new_rotor_position), plugboard_position) |
| return func(text) | | return func(text) |
| return decrypt_text | | return decrypt_text |
| return decorator | | return decorator |