MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / enigma

Function enigma

ciphers/enigma_machine2.py:167–290  ·  view source on GitHub ↗

The only difference with real-world enigma is that ``I`` allowed string input. All characters are converted to uppercase. (non-letter symbol are ignored) | How it works: | (for every letter in the message) - Input letter goes into the plugboard. If it is connected to ano

(
    text: str,
    rotor_position: RotorPositionT,
    rotor_selection: RotorSelectionT = (rotor1, rotor2, rotor3),
    plugb: str = "",
)

Source from the content-addressed store, hash-verified

165
166
167def enigma(
168 text: str,
169 rotor_position: RotorPositionT,
170 rotor_selection: RotorSelectionT = (rotor1, rotor2, rotor3),
171 plugb: str = "",
172) -> str:
173 """
174 The only difference with real-world enigma is that ``I`` allowed string input.
175 All characters are converted to uppercase. (non-letter symbol are ignored)
176
177 | How it works:
178 | (for every letter in the message)
179
180 - Input letter goes into the plugboard.
181 If it is connected to another one, switch it.
182
183 - Letter goes through ``3`` rotors.
184 Each rotor can be represented as ``2`` sets of symbol, where one is shuffled.
185 Each symbol from the first set has corresponding symbol in
186 the second set and vice versa.
187
188 example::
189
190 | ABCDEFGHIJKLMNOPQRSTUVWXYZ | e.g. F=D and D=F
191 | VKLEPDBGRNWTFCJOHQAMUZYIXS |
192
193 - Symbol then goes through reflector (static rotor).
194 There it is switched with paired symbol.
195 The reflector can be represented as ``2`` sets, each with half of the alphanet.
196 There are usually ``10`` pairs of letters.
197
198 Example::
199
200 | ABCDEFGHIJKLM | e.g. E is paired to X
201 | ZYXWVUTSRQPON | so when E goes in X goes out and vice versa
202
203 - Letter then goes through the rotors again
204
205 - If the letter is connected to plugboard, it is switched.
206
207 - Return the letter
208
209 >>> enigma('Hello World!', (1, 2, 1), plugb='pictures')
210 'KORYH JUHHI!'
211 >>> enigma('KORYH, juhhi!', (1, 2, 1), plugb='pictures')
212 'HELLO, WORLD!'
213 >>> enigma('hello world!', (1, 1, 1), plugb='pictures')
214 'FPNCZ QWOBU!'
215 >>> enigma('FPNCZ QWOBU', (1, 1, 1), plugb='pictures')
216 'HELLO WORLD'
217
218
219 :param text: input message
220 :param rotor_position: tuple with ``3`` values in range ``1``.. ``26``
221 :param rotor_selection: tuple with ``3`` rotors
222 :param plugb: string containing plugboard configuration (default ``''``)
223 :return: en/decrypted string
224 """

Callers 1

enigma_machine2.pyFile · 0.85

Calls 2

_validatorFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected