f | def plugboard(word, plugboard_position): | f | def plugboard(word, plugboard_position): |
| transformed_word = [] | | transformed_word = [] |
| for letter in word: | | for letter in word: |
n | written = False | n | |
| for pair_of_letters in plugboard_position: | | for pair_of_letters in plugboard_position: |
| if letter in pair_of_letters: | | if letter in pair_of_letters: |
| transformed_word.append((pair_of_letters - {letter}).pop()) | | transformed_word.append((pair_of_letters - {letter}).pop()) |
n | written = True | n | |
| break | | break |
n | if not written: | n | else: |
| transformed_word.append(letter) | | transformed_word.append(letter) |
| | | |
| return "".join(transformed_word) | | return "".join(transformed_word) |
| | | |
| | | |
| def rotor(word, rotor_position): | | def rotor(word, rotor_position): |
| transformed_word = [] | | transformed_word = [] |
| for letter in word: | | for letter in word: |
t | value = rotor_position.get(letter, None) | t | value = rotor_position.get(letter, letter) |
| if value is not None: | | |
| transformed_word.append(value) | | transformed_word.append(value) |
| continue | | |
| else: | | |
| transformed_word.append(letter) | | |
| return "".join(transformed_word) | | return "".join(transformed_word) |
| | | |
| | | |
| def enigma_encrypt(plugboard_position, rotor_position): | | def enigma_encrypt(plugboard_position, rotor_position): |
| def decorator(func): | | def decorator(func): |
| def wrapper(word): | | def wrapper(word): |
| plugboarded_word = plugboard(word, plugboard_position) | | plugboarded_word = plugboard(word, plugboard_position) |
| encrypted_word = rotor(plugboarded_word, rotor_position) | | encrypted_word = rotor(plugboarded_word, rotor_position) |
| return func(encrypted_word) | | return func(encrypted_word) |
| | | |
| return wrapper | | return wrapper |
| | | |
| 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 wrapper(word): | | def wrapper(word): |
| swapped_rotor_position = {v: k for k, v in rotor_position.items()} | | swapped_rotor_position = {v: k for k, v in rotor_position.items()} |
| encrypted_word = rotor(word, swapped_rotor_position) | | encrypted_word = rotor(word, swapped_rotor_position) |
| plugboarded_word = plugboard(encrypted_word, plugboard_position) | | plugboarded_word = plugboard(encrypted_word, plugboard_position) |
| return func(plugboarded_word) | | return func(plugboarded_word) |
| | | |
| return wrapper | | return wrapper |
| | | |
| return decorator | | return decorator |
| | | |
| | | |
| def crypt_behaivior(additional_func): | | def crypt_behaivior(additional_func): |
| def decorator(logic_func): | | def decorator(logic_func): |
| def wrapper(*args): | | def wrapper(*args): |
| additional_func(logic_func(*args)) | | additional_func(logic_func(*args)) |
| | | |
| return wrapper | | return wrapper |
| | | |
| return decorator | | return decorator |
| | | |
| | | |
| def encrypt(word): | | def encrypt(word): |
| return word | | return word |
| | | |
| | | |
| def decrypt(word): | | def decrypt(word): |
| return word | | return word |
| | | |
| | | |
| plugboard_position = [{"a", "c"}, {"t", "z"}, {"k", "l"}, {"d", "y"}, {"s", "j"}] | | plugboard_position = [{"a", "c"}, {"t", "z"}, {"k", "l"}, {"d", "y"}, {"s", "j"}] |
| rotor_position = { | | rotor_position = { |
| "v": "d", | | "v": "d", |
| "d": "v", | | "d": "v", |
| "y": "u", | | "y": "u", |
| "n": "n", | | "n": "n", |
| "i": "w", | | "i": "w", |
| "z": "p", | | "z": "p", |
| "s": "e", | | "s": "e", |
| "x": "s", | | "x": "s", |
| "h": "f", | | "h": "f", |
| "b": "x", | | "b": "x", |
| "u": "c", | | "u": "c", |
| "p": "q", | | "p": "q", |
| "r": "g", | | "r": "g", |
| "q": "j", | | "q": "j", |
| "e": "t", | | "e": "t", |
| "l": "y", | | "l": "y", |
| "o": "z", | | "o": "z", |
| "g": "o", | | "g": "o", |
| "k": "b", | | "k": "b", |
| "t": "h", | | "t": "h", |
| "j": "m", | | "j": "m", |
| "a": "a", | | "a": "a", |
| "w": "i", | | "w": "i", |
| "f": "l", | | "f": "l", |
| "m": "r", | | "m": "r", |
| "c": "k", | | "c": "k", |
| } | | } |
| | | |
| | | |
| encrypt = enigma_encrypt(plugboard_position, rotor_position)(encrypt) | | encrypt = enigma_encrypt(plugboard_position, rotor_position)(encrypt) |
| decrypt = enigma_decrypt(plugboard_position, rotor_position)(decrypt) | | decrypt = enigma_decrypt(plugboard_position, rotor_position)(decrypt) |
| | | |
| | | |
| print(decrypt(encrypt("hitler is awesome"))) | | print(decrypt(encrypt("hitler is awesome"))) |