Skip to content

Commit 789122f

Browse files
committed
Added codec: porta
1 parent fcf5d9d commit 789122f

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ This category also contains `ascii85`, `adobe`, `[x]btoa`, `zeromq` with the `ba
345345
- [X] `playfair`: aka Playfair Cipher
346346
- [X] `phillips`: aka Phillips Cipher (polyalphabetic block cipher with 8 key squares)
347347
- [X] `polybius`: aka Polybius Square Cipher
348+
- [X] `porta`: aka Porta Cipher (reciprocal variant of Vigenere Cipher with 13 alphabets)
348349
- [X] `railfence`: aka Rail Fence Cipher
349350
- [X] `rotN`: aka Caesar cipher (*N* belongs to [1,25])
350351
- [X] `scytaleN`: encrypts using the number of letters on the rod (*N* belongs to [1,[)

‎docs/pages/enc/crypto.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,23 @@ This implements the well-known Polybius Square cipher, using the square with the
311311

312312
-----
313313

314+
### Porta Cipher
315+
316+
This is a variant of the [Vigenere Cipher](#vigenere-cipher) where each pair of key letters (AB, CD, ..., YZ) selects one of 13 reciprocal alphabets swapping the halves A-M and N-Z. Hence, decoding is the same as encoding. There is no default key, meaning that `porta` as the encoding scheme throws a `LookupError` indicating that the _key must be a non-empty alphabetic string_.
317+
318+
**Codec** | **Conversions** | **Aliases** | **Comment**
319+
:---: | :---: | --- | ---
320+
`porta` | text <-> Porta ciphertext | `porta-abcdef`, `porta_cipher-MySuperSecret` | key only consists of characters, not digits
321+
322+
```python
323+
>>> codext.encode("DEFENDTHEEASTWALLOFTHECASTLE", "porta-fortification")
324+
'SYNNJSCVRNRLAHUTUKUCVRYRLANY'
325+
>>> codext.decode("SYNNJSCVRNRLAHUTUKUCVRYRLANY", "porta-fortification")
326+
'DEFENDTHEEASTWALLOFTHECASTLE'
327+
```
328+
329+
-----
330+
314331
### Rail Fence Cipher
315332

316333
This implements the Rail Fence encoding algorithm, using 3 rails and offset 0 as the default parameters. The encoding fence is built from the top ; the `up` flag can be used to build the fence from the bottom. Note that trying parameters that do not fit the input length will trigger a `ValueError` mentioning the bad value.

‎src/codext/crypto/vigenere.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
'dec(gronsfeld-cipher-31415)': {'DUXBHNBXEFZO': 'ATTACKATDAWN'},
3232
'enc-dec(gronsfeld-0|gronsfeld-12345|gronsfeld-9)': ['hello world', 'ATTACK AT DAWN', 'Test 1234!'],
3333
}
34+
__examples6__ = {
35+
'enc(porta)': None,
36+
'enc(porta-fortification)': {'DEFENDTHEEASTWALLOFTHECASTLE': 'SYNNJSCVRNRLAHUTUKUCVRYRLANY'},
37+
'dec(porta_cipher-fortification)': {'SYNNJSCVRNRLAHUTUKUCVRYRLANY': 'DEFENDTHEEASTWALLOFTHECASTLE'},
38+
'enc(porta-key)': {'Hello World': 'Ztxqm Kjcxv'},
39+
'enc-dec(porta-secret)': ['hello world', 'ATTACK AT DAWN', 'Test 1234!'],
40+
}
3441
__examples3__ = {
3542
'enc(trithemius-cipher)': {'this is a test': "tikv mx g ambd"},
3643
'enc(trithemius)': {'HELLO': "HFNOS", '12345!@#$': "12345!@#$"},
@@ -46,6 +53,7 @@
4653
__guess1__ = ["autoclave-key", "autoclave-password", "autoclave-secret"]
4754
__guess2__ = ["beaufort-key", "beaufort-password", "beaufort-secret"]
4855
__guess3__ = ["trithemius"]
56+
__guess6__ = ["porta-key", "porta-password", "porta-secret"]
4957
__guess5__ = ["gronsfeld-1234", "gronsfeld-12345", "gronsfeld-123456"]
5058
__guess4__ = ["vigenere-key", "vigenere-password", "vigenere-secret"]
5159

@@ -56,6 +64,13 @@
5664
[1, -1][d] * (ord(k[i % len(k)]) - ord('a'))) % 26]
5765

5866

67+
def pchar(c, k, i, d=False):
68+
""" Porta: the key letters pairs (AB, CD, ..., YZ) select one of 13 reciprocal alphabets swapping the halves A-M
69+
and N-Z ; hence, decoding is the same as encoding. """
70+
alph, s, x = LC if c in LC else UC, (ord(k[i % len(k)]) - ord('a')) // 2, ord(c.lower()) - ord('a')
71+
return alph[13 + (x + s) % 13 if x < 13 else (x - 13 - s) % 13]
72+
73+
5974
def __make(enc, char_func, key_stream=False, key_desc="alphabetic"):
6075
def _code(decode=False):
6176
def _wrapper(key):
@@ -100,6 +115,9 @@ def _wrapper(key=None):
100115
add("gronsfeld", *map(__gronsfeld, __make("gronsfeld", vchar, key_desc="numeric")),
101116
r"gronsfeld(?:[-_]cipher)?(?:[-_]([0-9]+))?$", examples=__examples5__, guess=__guess5__, penalty=.1)
102117

118+
add("porta", *__make("porta", pchar), r"porta(?:[-_]cipher)?(?:[-_]([a-zA-Z]+))?$",
119+
examples=__examples6__, guess=__guess6__, penalty=.1)
120+
103121
add("trithemius", enc(k := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), dec(k), r"trithemius(?:[-_]cipher)?$",
104122
examples=__examples3__, guess=__guess3__, penalty=.1)
105123

0 commit comments

Comments
 (0)