|
| 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