f | def change_letter(letter, converting_list): | f | def change_letter(letter, converting_list): |
| for single_set in converting_list: | | for single_set in converting_list: |
| if letter in single_set: | | if letter in single_set: |
| return "".join(single_set.difference({letter})) | | return "".join(single_set.difference({letter})) |
n | | n | |
| return letter | | return letter |
| | | |
| | | |
| def plugboard(text_to_convert, converting_set): | | def plugboard(text_to_convert, converting_set): |
| converted_text = [change_letter(letter, converting_set) for letter in text_to_convert] | | converted_text = [change_letter(letter, converting_set) for letter in text_to_convert] |
n | | n | |
| return "".join(converted_text) | | return "".join(converted_text) |
| | | |
| | | |
| def rotor(text_to_convert, converting_dict): | | def rotor(text_to_convert, converting_dict): |
| converted_text = "" | | converted_text = "" |
| for symbol in text_to_convert: | | for symbol in text_to_convert: |
n | if symbol in converting_dict: | n | |
| converted_text += converting_dict[symbol] | | converted_text += converting_dict.get(symbol, ' ') |
| else: | | |
| converted_text += symbol | | |
| | | |
| return converted_text | | |
| | | |
| | | |
| def rotor_decrypt(text_to_convert, converting_dict): | | |
| converted_text = "" | | |
| reverse_dict = {v: k for k, v in converting_dict.items()} | | |
| for symbol in text_to_convert: | | |
| if symbol in reverse_dict: | | |
| converted_text += reverse_dict[symbol] | | |
| else: | | |
| converted_text += symbol | | |
| | | |
| 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 wrapped_func(text_to_encrypt): | | def wrapped_func(text_to_encrypt): |
| return func(rotor(plugboard(text_to_encrypt, plugboard_position), rotor_position)) | | return func(rotor(plugboard(text_to_encrypt, plugboard_position), rotor_position)) |
n | | n | |
| return wrapped_func | | return wrapped_func |
n | | n | |
| 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 wrapped_func(text_to_decrypt): | | def wrapped_func(text_to_decrypt): |
n | | n | reverse_rotor_position = {v: k for k, v in rotor_position.items()} |
| return func(plugboard(rotor_decrypt(text_to_decrypt, rotor_position), plugboard_position)) | | return func(plugboard(rotor(text_to_decrypt, reverse_rotor_position), plugboard_position)) |
| | | |
| return wrapped_func | | return wrapped_func |
t | | t | |
| return decorator | | return decorator |