diff --git a/.idea/misc.xml b/.idea/misc.xml index 80bf3eb..5c9cfb9 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index 3e579c9..a0ce4a9 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/enigma.iml b/.idea/myenigma.iml similarity index 66% rename from .idea/enigma.iml rename to .idea/myenigma.iml index c6d5d10..2b0eb7f 100644 --- a/.idea/enigma.iml +++ b/.idea/myenigma.iml @@ -2,9 +2,10 @@ + - + \ No newline at end of file diff --git a/README.md b/README.md index 74b6b06..2a54c08 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,13 @@ -# enigma +# myenigma + +[![PyPI](https://img.shields.io/pypi/v/myenigma)](https://pypi.org/project/myenigma/) ![PyPI - Downloads](https://img.shields.io/pypi/dm/myenigma) + Python-based Enigma. ## Install ```bash -pip install enigma +pip install myenigma ``` ## Usage @@ -12,21 +15,21 @@ pip install enigma ### Import ```python -from enigma import Enigma +from myenigma import Enigma ``` The package also contains some sample plate: ```python -from enigma.sample_plate.rotor import rotor_I, rotor_II, rotor_III -from enigma.sample_plate.reflector import reflector_B +from myenigma.sample_plate.rotor import rotor_i, rotor_ii, rotor_iii +from myenigma.sample_plate.reflector import reflector_b ``` ### Defining Make sure the rotors are from right to left: ```python -e = Enigma([rotor_III(), rotor_II(), rotor_I()], reflector_B()) +e = Enigma([rotor_iii(), rotor_ii(), rotor_i()], reflector_b()) ``` ### Encryption @@ -59,13 +62,13 @@ assert e.input('XTGHAGDIVUPGBZVQSFMBSGLKVQHQWESYRTSRMOOFGRLE') == 'HELLOWORLDBYT You can freely customize your Enigma. For example, customize the circuits of rotors: ```python -from enigma.part.plate import Rotor, Reflector +from myenigma.part.plate import Rotor, Reflector rotor_I = Rotor('EKMFLGDQVZNTOWYHXUSPAIBRCJ', name='Rotor I', turnover='Q') rotor_II = Rotor('AJDKSIRUXBLHWTMCQGZNPYFVOE', name='Rotor II', turnover='E') rotor_III = Rotor('BDFHJLCPRTXVZNYEIWGAKMUSQO', name='Rotor III', turnover='V') reflector_B = Reflector('YRUHQSLDPXNGOKMIEBFZCWVJAT', name='Reflector B') -e = Enigma([rotor_III(), rotor_II(), rotor_I()], reflector_B()) +e_customize = Enigma([rotor_III, rotor_II, rotor_I], reflector_B) # same as e above ``` @@ -119,6 +122,13 @@ e.unplug('L') e.unplug('P') ``` +# Reference + +- [恩尼格玛密码机 - 维基百科,自由的百科全书](https://zh.wikipedia.org/wiki/%E6%81%A9%E5%B0%BC%E6%A0%BC%E7%8E%9B%E5%AF%86%E7%A0%81%E6%9C%BA) +- [Enigma Sim Manual](http://users.telenet.be/d.rijmenants/Enigma%20Sim%20Manual.pdf) +- [Enigma Simulator](http://users.telenet.be/d.rijmenants/en/enigmasim.htm) +- [Enigma Rotor and Umkehrwalze Wirings](http://www.ellsbury.com/ultraenigmawirings.htm) + ## License MIT License. \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index b8b027c..7db2f97 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,15 +1,15 @@ [metadata] -name = enigma +name = myenigma version = 1.0.0 author = DingJunyao author_email = dingjunyao0703@163.com description = Python-based Enigma. long_description = file: README.md long_description_content_type = text/markdown -url = https://github.com/DingJunyao/enigma +url = https://github.com/DingJunyao/myenigma project_urls = - Source = https://github.com/DingJunyao/enigma - Bug Tracker = https://github.com/DingJunyao/enigma/issues + Source = https://github.com/DingJunyao/myenigma + Bug Tracker = https://github.com/DingJunyao/myenigma/issues classifiers = Programming Language :: Python :: 3 License :: OSI Approved :: MIT License diff --git a/src/enigma/sample_plate/reflector.py b/src/enigma/sample_plate/reflector.py deleted file mode 100644 index 76b474c..0000000 --- a/src/enigma/sample_plate/reflector.py +++ /dev/null @@ -1,6 +0,0 @@ -from src.enigma.part.plate import Reflector - -reflector_B = lambda: Reflector('YRUHQSLDPXNGOKMIEBFZCWVJAT', name='Reflector B') -reflector_C = lambda: Reflector('FVPJIAOYEDRZXWGCTKUQSBNMHL', name='Reflector C') -reflector_B_thin = lambda: Reflector('ENKQAUYWJICOPBLMDXZVFTHRGS', name='Reflector B Thin') -reflector_C_thin = lambda: Reflector('RDOBJNTKVEHMLFCWZAXGYIPSUQ', name='Reflector C Thin') diff --git a/src/enigma/sample_plate/rotor.py b/src/enigma/sample_plate/rotor.py deleted file mode 100644 index d8889d6..0000000 --- a/src/enigma/sample_plate/rotor.py +++ /dev/null @@ -1,12 +0,0 @@ -from src.enigma.part.plate import Rotor - -rotor_I = lambda: Rotor('EKMFLGDQVZNTOWYHXUSPAIBRCJ', name='Rotor I', turnover='Q') -rotor_II = lambda: Rotor('AJDKSIRUXBLHWTMCQGZNPYFVOE', name='Rotor II', turnover='E') -rotor_III = lambda: Rotor('BDFHJLCPRTXVZNYEIWGAKMUSQO', name='Rotor III', turnover='V') -rotor_IV = lambda: Rotor('ESOVPZJAYQUIRHXLNFTGKDCMWB', name='Rotor IV', turnover='J') -rotor_V = lambda: Rotor('VZBRGITYUPSDNHLXAWMJQOFECK', name='Rotor V', turnover='Z') -rotor_VI = lambda: Rotor('JPGVOUMFYQBENHZRDKASXLICTW', name='Rotor VI', turnover=['Z', 'M']) -rotor_VII = lambda: Rotor('NZJHGRCXMYSWBOUFAIVLPEKQDT', name='Rotor VII', turnover=['Z', 'M']) -rotor_VIII = lambda: Rotor('FKQHTLXOCBJSPDZRAMEWNIUYGV', name='Rotor VIII', turnover=['Z', 'M']) -rotor_Beta = lambda: Rotor('LEYJVCNIXWPBQMDRTAKZGFUHOS', name='Rotor Beta') -rotor_Gamma = lambda: Rotor('FSOKANUERHMBTIYCWLQPZXVGJD', name='Rotor Gamma') diff --git a/src/enigma/__init__.py b/src/myenigma/__init__.py similarity index 100% rename from src/enigma/__init__.py rename to src/myenigma/__init__.py diff --git a/src/enigma/enigma.py b/src/myenigma/enigma.py similarity index 97% rename from src/enigma/enigma.py rename to src/myenigma/enigma.py index a79d2a2..2dbdd11 100644 --- a/src/enigma/enigma.py +++ b/src/myenigma/enigma.py @@ -1,7 +1,7 @@ from typing import Union -from src.enigma.part import Plugboard -from src.enigma.part.plate import Rotor, EntryPlate, Reflector +from .part import Plugboard +from .part.plate import Rotor, EntryPlate, Reflector class Enigma: diff --git a/src/enigma/part/__init__.py b/src/myenigma/part/__init__.py similarity index 100% rename from src/enigma/part/__init__.py rename to src/myenigma/part/__init__.py diff --git a/src/enigma/part/plate/__init__.py b/src/myenigma/part/plate/__init__.py similarity index 100% rename from src/enigma/part/plate/__init__.py rename to src/myenigma/part/plate/__init__.py diff --git a/src/enigma/part/plate/entry_plate.py b/src/myenigma/part/plate/entry_plate.py similarity index 100% rename from src/enigma/part/plate/entry_plate.py rename to src/myenigma/part/plate/entry_plate.py diff --git a/src/enigma/part/plate/plate.py b/src/myenigma/part/plate/plate.py similarity index 97% rename from src/enigma/part/plate/plate.py rename to src/myenigma/part/plate/plate.py index fdf7555..093bc34 100644 --- a/src/enigma/part/plate/plate.py +++ b/src/myenigma/part/plate/plate.py @@ -4,7 +4,7 @@ class Plate: - """Plate of an Engima, including Entry Plate, Rotors (Walzen in German) and Reflector (Umkehrwalze in German).""" + """Plate of an Enigma, including Entry Plate, Rotors (Walzen in German) and Reflector (Umkehrwalze in German).""" @staticmethod def _map_table_check(map_table: str, map_source: str = _ALPHABET): @@ -100,7 +100,7 @@ def encrypt(self, letter: str, letter_from: str = 'right') -> str: :param letter: A letter. :param letter_from: 'left' or 'right'. :return: A letter. - :raise AttributeError: Input invaild + :raise AttributeError: Input invalid """ if type(letter) != str or len(letter) != 1 or letter not in self.map_source: raise AttributeError(f'Input "{letter}" is invalid. A letter string in map_source is required.') @@ -132,7 +132,7 @@ def set_position(self, position: Union[int, str]): position_int = position if position_int is None: raise AttributeError( - f'position "{position}" is invaild. ' + f'position "{position}" is invalid. ' f'A letter string in map_source or an int between 0 and {len(self.map_source) - 1} is required.' ) self.position = position_int diff --git a/src/enigma/part/plate/reflector.py b/src/myenigma/part/plate/reflector.py similarity index 100% rename from src/enigma/part/plate/reflector.py rename to src/myenigma/part/plate/reflector.py diff --git a/src/enigma/part/plate/rotor.py b/src/myenigma/part/plate/rotor.py similarity index 95% rename from src/enigma/part/plate/rotor.py rename to src/myenigma/part/plate/rotor.py index 0b331b4..2402ed2 100644 --- a/src/enigma/part/plate/rotor.py +++ b/src/myenigma/part/plate/rotor.py @@ -11,7 +11,7 @@ def __turnover_attr_exception( ): """Raise AttributeError if the turnover value is invalid.""" raise AttributeError( - f'turnover "{turnover}" is invaild. ' + f'turnover "{turnover}" is invalid. ' f'A letter string in map_source or an int between 0 and {len(map_source) - 1} is required.' ) @@ -73,12 +73,12 @@ def forward(self) -> int: :return: Position of the plate. """ - orignal_position = self.position - if orignal_position == len(self.map_source) - 1: + original_position = self.position + if original_position == len(self.map_source) - 1: self.position = 0 else: self.position += 1 - if orignal_position in self.turnover: + if original_position in self.turnover: if self.left_plate: if self.left_plate.auto_rotatable: self.left_plate.forward() diff --git a/src/enigma/part/plugboard.py b/src/myenigma/part/plugboard.py similarity index 95% rename from src/enigma/part/plugboard.py rename to src/myenigma/part/plugboard.py index 8254c55..6ca6c05 100644 --- a/src/enigma/part/plugboard.py +++ b/src/myenigma/part/plugboard.py @@ -1,5 +1,5 @@ class Plugboard: - """Plugboard (Steckerbrett in German) of an Engima.""" + """Plugboard (Steckerbrett in German) of an Enigma.""" def __init__(self, parent=None): self.map_dict = {} self.parent = parent diff --git a/src/enigma/sample_plate/__init__.py b/src/myenigma/sample_plate/__init__.py similarity index 100% rename from src/enigma/sample_plate/__init__.py rename to src/myenigma/sample_plate/__init__.py diff --git a/src/myenigma/sample_plate/reflector.py b/src/myenigma/sample_plate/reflector.py new file mode 100644 index 0000000..753ef13 --- /dev/null +++ b/src/myenigma/sample_plate/reflector.py @@ -0,0 +1,17 @@ +from ..part.plate import Reflector + + +def reflector_b(): + return Reflector('YRUHQSLDPXNGOKMIEBFZCWVJAT', name='Reflector B') + + +def reflector_c(): + return Reflector('FVPJIAOYEDRZXWGCTKUQSBNMHL', name='Reflector C') + + +def reflector_b_thin(): + return Reflector('ENKQAUYWJICOPBLMDXZVFTHRGS', name='Reflector B Thin') + + +def reflector_c_thin(): + return Reflector('RDOBJNTKVEHMLFCWZAXGYIPSUQ', name='Reflector C Thin') diff --git a/src/myenigma/sample_plate/rotor.py b/src/myenigma/sample_plate/rotor.py new file mode 100644 index 0000000..35ca141 --- /dev/null +++ b/src/myenigma/sample_plate/rotor.py @@ -0,0 +1,41 @@ +from ..part.plate import Rotor + + +def rotor_i(): + return Rotor('EKMFLGDQVZNTOWYHXUSPAIBRCJ', name='Rotor I', turnover='Q') + + +def rotor_ii(): + return Rotor('AJDKSIRUXBLHWTMCQGZNPYFVOE', name='Rotor II', turnover='E') + + +def rotor_iii(): + return Rotor('BDFHJLCPRTXVZNYEIWGAKMUSQO', name='Rotor III', turnover='V') + + +def rotor_iv(): + return Rotor('ESOVPZJAYQUIRHXLNFTGKDCMWB', name='Rotor IV', turnover='J') + + +def rotor_v(): + return Rotor('VZBRGITYUPSDNHLXAWMJQOFECK', name='Rotor V', turnover='Z') + + +def rotor_vi(): + return Rotor('JPGVOUMFYQBENHZRDKASXLICTW', name='Rotor VI', turnover=['Z', 'M']) + + +def rotor_vii(): + return Rotor('NZJHGRCXMYSWBOUFAIVLPEKQDT', name='Rotor VII', turnover=['Z', 'M']) + + +def rotor_viii(): + return Rotor('FKQHTLXOCBJSPDZRAMEWNIUYGV', name='Rotor VIII', turnover=['Z', 'M']) + + +def rotor_beta(): + return Rotor('LEYJVCNIXWPBQMDRTAKZGFUHOS', name='Rotor Beta') + + +def rotor_gamma(): + return Rotor('FSOKANUERHMBTIYCWLQPZXVGJD', name='Rotor Gamma') diff --git a/tests/test_enigma.py b/tests/test_enigma.py index 5fe463b..2db8c7c 100644 --- a/tests/test_enigma.py +++ b/tests/test_enigma.py @@ -1,11 +1,11 @@ -from src.enigma import Enigma -from src.enigma.part.plate import Rotor, Reflector -from src.enigma.sample_plate.rotor import rotor_I, rotor_II, rotor_III -from src.enigma.sample_plate.reflector import reflector_B +from src.myenigma import Enigma +from src.myenigma.part.plate import Rotor, Reflector +from src.myenigma.sample_plate.rotor import rotor_i, rotor_ii, rotor_iii +from src.myenigma.sample_plate.reflector import reflector_b def test_100_enigma(): - e = Enigma([rotor_III(), rotor_II(), rotor_I()], reflector_B()) + e = Enigma([rotor_iii(), rotor_ii(), rotor_i()], reflector_b()) for input_letter, result in zip('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'BAQMFEXIHSWPDYTLCVJOZRKGNU'): e.set_position() assert e.input(input_letter) == result diff --git a/tests/test_enigma_plug.py b/tests/test_enigma_plug.py index 576c466..684d9c9 100644 --- a/tests/test_enigma_plug.py +++ b/tests/test_enigma_plug.py @@ -1,11 +1,11 @@ -from src.enigma import Enigma -from src.enigma.part.plate import Rotor, Reflector -from src.enigma.sample_plate.rotor import rotor_I, rotor_II, rotor_III -from src.enigma.sample_plate.reflector import reflector_B +from src.myenigma import Enigma +from src.myenigma.part.plate import Rotor, Reflector +from src.myenigma.sample_plate.rotor import rotor_i, rotor_ii, rotor_iii +from src.myenigma.sample_plate.reflector import reflector_b def test_200_enigma_plug(): - e = Enigma([rotor_III(), rotor_II(), rotor_I()], reflector_B()) + e = Enigma([rotor_iii(), rotor_ii(), rotor_i()], reflector_b()) e.plugboard.plug('L', 'M') e.plugboard.plug('O', 'P') for rotor in e.rotors: diff --git a/tests/test_plate.py b/tests/test_plate.py index 0942822..c6830e7 100644 --- a/tests/test_plate.py +++ b/tests/test_plate.py @@ -1,6 +1,6 @@ -from src.enigma.part.plate.plate import Plate +from src.myenigma.part.plate.plate import Plate def test_000_plate(): r = Plate('EKMFLGDQVZNTOWYHXUSPAIBRCJ') - r.encrypt('A', 'right') \ No newline at end of file + r.encrypt('A', 'right')