f | | f | |
| def plugboard(text, plugboard_position): | | def plugboard(text, plugboard_position): |
| encypted_text = "" | | encypted_text = "" |
| for letter in text: | | for letter in text: |
| new_letter = letter | | new_letter = letter |
| for pair in plugboard_position: | | for pair in plugboard_position: |
| if letter in pair: | | if letter in pair: |
| new_letter = "".join(pair - {letter}) | | new_letter = "".join(pair - {letter}) |
| encypted_text += new_letter | | encypted_text += new_letter |
| return encypted_text | | return encypted_text |
| | | |
| | | |
| def rotor(text, rotor_position): | | def rotor(text, rotor_position): |
| encrypted_text = "" | | encrypted_text = "" |
| for letter in text: | | for letter in text: |
t | if letter != ' ': | t | |
| encrypted_text += rotor_position[letter] | | encrypted_text += rotor_position.get(letter, ' ') |
| else: | | |
| encrypted_text += ' ' | | |
| return encrypted_text | | return encrypted_text |
| | | |
| | | |
| def enigma_encrypt(plugboard_position, rotor_position): | | def enigma_encrypt(plugboard_position, rotor_position): |
| def decorator_encrypt(func): | | def decorator_encrypt(func): |
| def encrypt(text): | | def encrypt(text): |
| return func(rotor(plugboard(text, plugboard_position), rotor_position)) | | return func(rotor(plugboard(text, plugboard_position), rotor_position)) |
| return encrypt | | return encrypt |
| return decorator_encrypt | | return decorator_encrypt |
| | | |
| def enigma_decrypt(plugboard_position, rotor_position): | | def enigma_decrypt(plugboard_position, rotor_position): |
| reverse_rotor_position = {} | | reverse_rotor_position = {} |
| for key_letter in rotor_position: | | for key_letter in rotor_position: |
| reverse_rotor_position[rotor_position[key_letter]] = key_letter | | reverse_rotor_position[rotor_position[key_letter]] = key_letter |
| def decorator_decrypt(func): | | def decorator_decrypt(func): |
| def decrypt(text): | | def decrypt(text): |
| return func(plugboard(rotor(text, reverse_rotor_position), plugboard_position)) | | return func(plugboard(rotor(text, reverse_rotor_position), plugboard_position)) |
| return decrypt | | return decrypt |
| return decorator_decrypt | | return decorator_decrypt |
| | | |