Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DingJunyao committed Dec 6, 2021
1 parent 0bd56b3 commit 25d50d3
Show file tree
Hide file tree
Showing 22 changed files with 106 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/enigma.iml → .idea/myenigma.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
# 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

### 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
Expand Down Expand Up @@ -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
```

Expand Down Expand Up @@ -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.
8 changes: 4 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[metadata]
name = enigma
name = myenigma
version = 1.0.0
author = DingJunyao
author_email = [email protected]
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
Expand Down
6 changes: 0 additions & 6 deletions src/enigma/sample_plate/reflector.py

This file was deleted.

12 changes: 0 additions & 12 deletions src/enigma/sample_plate/rotor.py

This file was deleted.

File renamed without changes.
4 changes: 2 additions & 2 deletions src/enigma/enigma.py → src/myenigma/enigma.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.')
Expand Down Expand Up @@ -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
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
)

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions src/myenigma/sample_plate/reflector.py
Original file line number Diff line number Diff line change
@@ -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')
41 changes: 41 additions & 0 deletions src/myenigma/sample_plate/rotor.py
Original file line number Diff line number Diff line change
@@ -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')
10 changes: 5 additions & 5 deletions tests/test_enigma.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 5 additions & 5 deletions tests/test_enigma_plug.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_plate.py
Original file line number Diff line number Diff line change
@@ -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')
r.encrypt('A', 'right')

0 comments on commit 25d50d3

Please sign in to comment.