Skip to content

Commit 3c398f3

Browse files
committed
Added codec: chaocipher
1 parent 3623408 commit 3c398f3

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ This category also contains `ascii85`, `adobe`, `[x]btoa`, `zeromq` with the `ba
339339
- [X] `bazeries`: aka Bazeries Cipher (two Polybius grids and a transposition keyed by a number)
340340
- [X] `beaufort`: aka Beaufort Cipher (variant of Vigenere Cipher)
341341
- [X] `bifid`: aka Bifid Cipher (Delastelle's fractionation with a 5x5 Polybius square) ; `trifid` for its 3x3x3 cube variant
342+
- [X] `chaocipher`: aka Chaocipher (two alphabets permuted after each letter)
342343
- [X] `citrix`: aka Citrix CTX1 password encoding
343344
- [X] `columnar`: aka Columnar Transposition Cipher (keyword-ordered columns ; a second keyword gives the double transposition)
344345
- [X] `gronsfeld`: aka Gronsfeld Cipher (variant of Vigenere Cipher with a numeric key)

‎docs/pages/enc/crypto.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,25 @@ The codec name holds the optional keyword, then the optional period. The period
220220

221221
-----
222222

223+
### Chaocipher
224+
225+
The Chaocipher was invented by John F. Byrne in 1918 and only disclosed in 2010. It uses two alphabets, the left one for the ciphertext and the right one for the plaintext. Each letter is replaced by the letter at the same position in the other alphabet, then both alphabets are permuted, so that the substitution changes with every letter. The alphabets are given as keywords (without duplicates) followed by the remaining letters ; by default, those of the example of [Moshe Rubin's paper](http://www.chaocipher.com/ActualChaocipher/Chaocipher-Revealing-Algorithm-Rubin.pdf) are used (`HXUCZVAMDSLKPEFJRIGTWOBNYQ` and `PTLNBQDEOYSFAVZKGJRIHWXUMC`). Other characters are kept in place, as well as the case of the letters.
226+
227+
**Codec** | **Conversions** | **Aliases** | **Comment**
228+
:---: | :---: | --- | ---
229+
`chaocipher` | text <-> Chaocipher ciphertext | `chao-cipher`, `chaocipher-left-right` | optional left and right keywords (both or none)
230+
231+
```python
232+
>>> codext.encode("WELLDONEISBETTERTHANWELLSAID", "chaocipher")
233+
'OAHQHCNYNXTSZJRRHJBYHQKSOUJY'
234+
>>> codext.decode("OAHQHCNYNXTSZJRRHJBYHQKSOUJY", "chaocipher")
235+
'WELLDONEISBETTERTHANWELLSAID'
236+
>>> codext.encode("Well done, is better than well said!", "chaocipher")
237+
'Oahq hcny, nx tszjrr hjby hqks oujy!'
238+
```
239+
240+
-----
241+
223242
### Citrix CTX1
224243

225244
This implements the Citrix CTX1 password encoding algorithm.

‎src/codext/crypto/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .barbie import *
77
from .bazeries import *
88
from .bifid import *
9+
from .chaocipher import *
910
from .citrix import *
1011
from .columnar import *
1112
from .hill import *

‎src/codext/crypto/chaocipher.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# -*- coding: UTF-8 -*-
2+
"""Chaocipher Codec - chaocipher content encoding.
3+
4+
The Chaocipher was invented by John F. Byrne in 1918 and only disclosed in 2010. It uses two alphabets, the left one for
5+
the ciphertext and the right one for the plaintext. Each letter is replaced by the letter at the same position in the
6+
other alphabet, then both alphabets are permuted:
7+
- the left one is rotated so that the ciphertext letter comes first, then its second letter is moved to the 14th
8+
position (the nadir)
9+
- the right one is rotated so that the plaintext letter comes last, then its third letter is moved to the 14th
10+
position
11+
12+
The alphabets are given as keywords (without duplicates) followed by the remaining letters ; by default, those of the
13+
example of Moshe Rubin's paper are used. Other characters are kept in place, as well as the case of the letters.
14+
15+
This codec:
16+
- en/decodes strings from str to str
17+
- en/decodes strings from bytes to bytes
18+
- decodes file content to str (read)
19+
- encodes file content from str to bytes (write)
20+
21+
Reference: http://www.chaocipher.com/ActualChaocipher/Chaocipher-Revealing-Algorithm-Rubin.pdf
22+
"""
23+
from string import ascii_lowercase as LC, ascii_uppercase as UC
24+
25+
from ..__common__ import *
26+
27+
28+
__examples__ = {
29+
'enc(chaocipher|chao-cipher|chao_cipher-hxuczvamdslkpefjrigtwobnyq-ptlnbqdeoysfavzkgjrihwxumc)': {
30+
'WELLDONEISBETTERTHANWELLSAID': "OAHQHCNYNXTSZJRRHJBYHQKSOUJY"},
31+
'dec(chaocipher)': {'OAHQHCNYNXTSZJRRHJBYHQKSOUJY': "WELLDONEISBETTERTHANWELLSAID"},
32+
'enc(chaocipher)': {'Well done, is better than well said!':
33+
"Oahq hcny, nx tszjrr hjby hqks oujy!"},
34+
'enc(chaocipher-left-right)': {'WELLDONEISBETTERTHANWELLSAID': "WGJIAWXFBUWJDBXIRKQYXTNKOGFQ"},
35+
'enc-dec(chaocipher|chaocipher-left-right|chaocipher-byrne-chaos)': ["", "Hello, World!", "ATTACK AT DAWN",
36+
"@random"],
37+
}
38+
__guess__ = ["chaocipher"]
39+
40+
41+
LEFT, RIGHT = "HXUCZVAMDSLKPEFJRIGTWOBNYQ", "PTLNBQDEOYSFAVZKGJRIHWXUMC"
42+
43+
44+
def __alphabet(keyword):
45+
a = []
46+
for c in keyword.upper() + UC:
47+
if c not in a:
48+
a.append(c)
49+
return "".join(a)
50+
51+
52+
def __chaocipher(decode=False):
53+
def _wrapper(left=None, right=None):
54+
left, right = (__alphabet(left), __alphabet(right)) if left else (LEFT, RIGHT)
55+
def _code(text, errors="strict"):
56+
l, r, out = left, right, []
57+
for c in ensure_str(text):
58+
if c not in LC and c not in UC:
59+
out.append(c)
60+
continue
61+
# the ciphertext letter (left) and the plaintext letter (right) are at the same position
62+
i = (r if not decode else l).index(c.upper())
63+
o = (l if not decode else r)[i]
64+
out.append(o.lower() if c in LC else o)
65+
# left alphabet: ciphertext letter first, then the 2nd letter moved to the nadir
66+
l = l[i:] + l[:i]
67+
l = l[0] + l[2:14] + l[1] + l[14:]
68+
# right alphabet: plaintext letter last, then the 3rd letter moved to the nadir
69+
r = r[i+1:] + r[:i+1]
70+
r = r[:2] + r[3:14] + r[2] + r[14:]
71+
return (s := "".join(out)), len(s)
72+
return _code
73+
return _wrapper
74+
75+
76+
add("chaocipher", __chaocipher(), __chaocipher(True),
77+
r"^chao(?:[-_]?cipher)?(?:[-_]([a-zA-Z]+)[-_]([a-zA-Z]+))?$", penalty=.1)

0 commit comments

Comments
 (0)