Skip to content

Commit 5a68482

Browse files
Copilotdhondta
andcommitted
Add Polybius square codec
Co-authored-by: dhondta <9108102+dhondta@users.noreply.github.com> Agent-Logs-Url: https://github.com/dhondta/python-codext/sessions/d62d52ab-5a81-4502-a03c-e834bf9c65ff
1 parent 568b7d1 commit 5a68482

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/codext/crypto/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .bacon import *
55
from .barbie import *
66
from .citrix import *
7+
from .polybius import *
78
from .railfence import *
89
from .rot import *
910
from .scytale import *

src/codext/crypto/polybius.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- coding: UTF-8 -*-
2+
"""Polybius Square Codec - polybius-square content encoding.
3+
4+
The Polybius square is a method for fractionating plaintext characters so that
5+
they can be represented by a smaller set of symbols. Each letter is represented
6+
by its coordinates (row, column) in the 5×5 grid.
7+
8+
This codec:
9+
- en/decodes strings from str to str
10+
- en/decodes strings from bytes to bytes
11+
- decodes file content to str (read)
12+
- encodes file content from str to bytes (write)
13+
14+
Reference: https://en.wikipedia.org/wiki/Polybius_square
15+
"""
16+
from ..__common__ import *
17+
18+
19+
__examples__ = {
20+
'enc(polybius|polybius-square|polybius_square)': {
21+
'this is a test': "44232443 2443 11 44154344",
22+
'jack': "24111325",
23+
},
24+
'dec(polybius)': {
25+
'44232443 2443 11 44154344': "THIS IS A TEST",
26+
},
27+
}
28+
__guess__ = ["polybius-square"]
29+
30+
31+
# Standard 5×5 Polybius square (I and J share the same cell):
32+
# 1 2 3 4 5
33+
# 1 A B C D E
34+
# 2 F G H I K
35+
# 3 L M N O P
36+
# 4 Q R S T U
37+
# 5 V W X Y Z
38+
_ALPHABET = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
39+
ENCMAP = {_ALPHABET[i]: str(i // 5 + 1) + str(i % 5 + 1) for i in range(25)}
40+
ENCMAP['J'] = ENCMAP['I']
41+
ENCMAP[' '] = ' '
42+
43+
44+
add_map("polybius-square", ENCMAP, ignore_case="both",
45+
pattern=r"^(?:polybius(?:[-_]square)?)$", printables_rate=1.,
46+
expansion_factor=(1.85, .15))

0 commit comments

Comments
 (0)