Skip to content

Commit fcf5d9d

Browse files
committed
Added codec: gronsfeld
1 parent 55bba01 commit fcf5d9d

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ This category also contains `ascii85`, `adobe`, `[x]btoa`, `zeromq` with the `ba
341341
- [X] `bifid`: aka Bifid Cipher (Delastelle's fractionation with a 5x5 Polybius square) ; `trifid` for its 3x3x3 cube variant
342342
- [X] `citrix`: aka Citrix CTX1 password encoding
343343
- [X] `columnar`: aka Columnar Transposition Cipher (keyword-ordered columns ; a second keyword gives the double transposition)
344+
- [X] `gronsfeld`: aka Gronsfeld Cipher (variant of Vigenere Cipher with a numeric key)
344345
- [X] `playfair`: aka Playfair Cipher
345346
- [X] `phillips`: aka Phillips Cipher (polyalphabetic block cipher with 8 key squares)
346347
- [X] `polybius`: aka Polybius Square Cipher

‎docs/pages/enc/crypto.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,23 @@ The text is written in rows under a keyword, then read column by column, in the
256256

257257
-----
258258

259+
### Gronsfeld Cipher
260+
261+
This is a variant of the [Vigenere Cipher](#vigenere-cipher) whose key is made of digits, each digit giving the shift of a letter (0 to 9). There is no default key, meaning that `gronsfeld` as the encoding scheme throws a `LookupError` indicating that the _key must be a non-empty numeric string_.
262+
263+
**Codec** | **Conversions** | **Aliases** | **Comment**
264+
:---: | :---: | --- | ---
265+
`gronsfeld` | text <-> Gronsfeld ciphertext | `gronsfeld-31415`, `gronsfeld_cipher-0123` | key only consists of digits
266+
267+
```python
268+
>>> codext.encode("ATTACKATDAWN", "gronsfeld-31415")
269+
'DUXBHNBXEFZO'
270+
>>> codext.decode("DUXBHNBXEFZO", "gronsfeld-31415")
271+
'ATTACKATDAWN'
272+
```
273+
274+
-----
275+
259276
### Phillips Cipher
260277

261278
This implements Phillips cipher, a polyalphabetic code using 8 grids generated with one keyword.

‎src/codext/crypto/vigenere.py‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- decodes file content to str (read)
88
- encodes file content from str to bytes (write)
99
"""
10-
from string import ascii_lowercase as LC, ascii_uppercase as UC
10+
from string import ascii_lowercase as LC, ascii_uppercase as UC, digits as DG
1111

1212
from ..__common__ import *
1313

@@ -23,6 +23,14 @@
2323
'enc(beaufort_key)': {'Hello World': 'Danzq Cwnnh'},
2424
'enc-dec(beaufort-secret)': ['hello world', 'ATTACK AT DAWN', 'Test 1234!'],
2525
}
26+
__examples5__ = {
27+
'enc(gronsfeld)': None,
28+
'enc(gronsfeld-cipher-12a)': None,
29+
'enc(gronsfeld-31415)': {'ATTACKATDAWN': 'DUXBHNBXEFZO'},
30+
'enc(gronsfeld_0123)': {'Hello World': 'Hfnoo Xqule'},
31+
'dec(gronsfeld-cipher-31415)': {'DUXBHNBXEFZO': 'ATTACKATDAWN'},
32+
'enc-dec(gronsfeld-0|gronsfeld-12345|gronsfeld-9)': ['hello world', 'ATTACK AT DAWN', 'Test 1234!'],
33+
}
2634
__examples3__ = {
2735
'enc(trithemius-cipher)': {'this is a test': "tikv mx g ambd"},
2836
'enc(trithemius)': {'HELLO': "HFNOS", '12345!@#$': "12345!@#$"},
@@ -38,6 +46,7 @@
3846
__guess1__ = ["autoclave-key", "autoclave-password", "autoclave-secret"]
3947
__guess2__ = ["beaufort-key", "beaufort-password", "beaufort-secret"]
4048
__guess3__ = ["trithemius"]
49+
__guess5__ = ["gronsfeld-1234", "gronsfeld-12345", "gronsfeld-123456"]
4150
__guess4__ = ["vigenere-key", "vigenere-password", "vigenere-secret"]
4251

4352

@@ -47,12 +56,12 @@
4756
[1, -1][d] * (ord(k[i % len(k)]) - ord('a'))) % 26]
4857

4958

50-
def __make(enc, char_func, key_stream=False):
59+
def __make(enc, char_func, key_stream=False, key_desc="alphabetic"):
5160
def _code(decode=False):
5261
def _wrapper(key):
5362
def _subwrapper(text, errors="strict"):
5463
if not (k := key.lower()) or not k.isalpha():
55-
raise LookupError(f"Bad parameter for encoding '{enc}': key must be a non-empty alphabetic string")
64+
raise LookupError(f"Bad parameter for encoding '{enc}': key must be a non-empty {key_desc} string")
5665
if key_stream and not decode:
5766
k += "".join(c.lower() for c in ensure_str(text) if c in LC or c in UC)
5867
result, i = [], 0
@@ -79,6 +88,18 @@ def _subwrapper(text, errors="strict"):
7988
examples=__examples2__, guess=__guess2__, penalty=.1)
8089

8190
enc, dec = __make("trithemius", vchar)
91+
def __gronsfeld(f):
92+
""" Turn the digits of the key into the letters with the same shift (0 to 9 into a to j), a key made of digits being
93+
given as an integer by the codec name pattern. """
94+
def _wrapper(key=None):
95+
key = "" if key is None else str(key)
96+
return f(key.translate(str.maketrans(DG, LC[:10])) if key.isdigit() else "")
97+
return _wrapper
98+
99+
100+
add("gronsfeld", *map(__gronsfeld, __make("gronsfeld", vchar, key_desc="numeric")),
101+
r"gronsfeld(?:[-_]cipher)?(?:[-_]([0-9]+))?$", examples=__examples5__, guess=__guess5__, penalty=.1)
102+
82103
add("trithemius", enc(k := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), dec(k), r"trithemius(?:[-_]cipher)?$",
83104
examples=__examples3__, guess=__guess3__, penalty=.1)
84105

0 commit comments

Comments
 (0)