|
19 | 19 |
|
20 | 20 | Reference: https://www.dcode.fr/playfair-cipher |
21 | 21 | """ |
22 | | -from string import ascii_uppercase as UC, digits as DG |
23 | | - |
| 22 | +from ._square import * |
24 | 23 | from ..__common__ import * |
25 | 24 |
|
26 | 25 |
|
|
42 | 41 | } |
43 | 42 | __guess__ = ["playfair"] |
44 | 43 |
|
45 | | -_DEFAULT_ALPHABET = { |
46 | | - 5: UC.replace("J", ""), |
47 | | - 6: UC + DG, |
48 | | -} |
49 | | - |
50 | 44 |
|
51 | | -def __build_grid(key=None, grid_size=5, replace_char=('J', 'I')): |
52 | | - """Build the Playfair grid from an optional keyword. """ |
53 | | - seen, grid = set(), [] |
54 | | - for c in key.upper(): |
55 | | - if grid_size == 5 and c == replace_char[0]: |
56 | | - c = replace_char[1] |
57 | | - if c.isalpha() and c not in seen: |
58 | | - seen.add(c) |
59 | | - grid.append(c) |
60 | | - for c in _DEFAULT_ALPHABET[grid_size]: |
61 | | - if c not in seen: |
62 | | - seen.add(c) |
63 | | - grid.append(c) |
64 | | - return grid, {grid[i]: (i // grid_size, i % grid_size) for i in range(grid_size ** 2)} |
| 45 | +def __build_grid(key, alphabet, grid_size, replace_char): |
| 46 | + """Build the Playfair grid from a keyword. """ |
| 47 | + grid = keyed_square(key, alphabet, replace_char) |
| 48 | + return grid, {c: divmod(i, grid_size) for i, c in enumerate(grid)} |
65 | 49 |
|
66 | 50 |
|
67 | 51 | def __set_params(key, grid_size, fill_chars, replace_char): |
68 | 52 | if len(fc := str(fill_chars or "XQ").upper()) == 2 and fc[0] == fc[1]: |
69 | 53 | raise LookupError("Bad parameter for encoding 'playfair': filling char must be a single letter or a pair of " |
70 | 54 | "distinct letters") |
71 | | - if len(rc := str(replace_char or "JI").upper()) == 2 and rc[0] == rc[1]: |
72 | | - raise LookupError("Bad parameter for encoding 'playfair': replaced char must be a pair of distinct letters") |
73 | | - gs = int(grid_size or 5) |
74 | | - a = _DEFAULT_ALPHABET[gs] |
| 55 | + gs, rc, a = square_params("playfair", grid_size, replace_char) |
75 | 56 | if len(fc) == 1: |
76 | 57 | fc += a[(a.index(fc[0])+1) % len(a)] |
77 | | - return (a if key == "*" else a[::-1] if key == "^" else key).upper(), gs, tuple(fc), tuple(rc) |
| 58 | + return (a if key == "*" else a[::-1] if key == "^" else key).upper(), a, gs, tuple(fc), rc |
78 | 59 |
|
79 | 60 |
|
80 | 61 | def playfair_encode(key=None, grid_size=None, fill_chars=None, replace_char=None): |
81 | | - key, gsize, fchars, rchar = __set_params(key, grid_size, fill_chars, replace_char) |
82 | | - grid, pos = __build_grid(key, gsize, rchar) |
| 62 | + key, alphabet, gsize, fchars, rchar = __set_params(key, grid_size, fill_chars, replace_char) |
| 63 | + grid, pos = __build_grid(key, alphabet, gsize, rchar) |
83 | 64 | def encode(text, errors="strict"): |
84 | 65 | _filler = lambda c: fchars[1] if c == fchars[0] else fchars[0] |
85 | | - chars = [rchar[1] if gsize == 5 and c == rchar[0] else c for c in ensure_str(text).upper().replace(" ", "")] |
| 66 | + chars = [rchar[1] if rchar and c == rchar[0] else c for c in ensure_str(text).upper().replace(" ", "")] |
86 | 67 | result, i, _h = [], 0, handle_error("playfair", errors) |
87 | 68 | while i < len(chars): |
88 | 69 | a = chars[i] |
@@ -113,10 +94,10 @@ def encode(text, errors="strict"): |
113 | 94 |
|
114 | 95 |
|
115 | 96 | def playfair_decode(key=None, grid_size=None, fill_chars=None, replace_char=None): |
116 | | - key, gsize, _, rchar = __set_params(key, grid_size, fill_chars, replace_char) |
117 | | - grid, pos = __build_grid(key, gsize, rchar) |
| 97 | + key, alphabet, gsize, _, rchar = __set_params(key, grid_size, fill_chars, replace_char) |
| 98 | + grid, pos = __build_grid(key, alphabet, gsize, rchar) |
118 | 99 | def decode(text, errors="strict"): |
119 | | - chars = [rchar[1] if gsize == 5 and c == rchar[0] else c for c in ensure_str(text)] |
| 100 | + chars = [rchar[1] if rchar and c == rchar[0] else c for c in ensure_str(text)] |
120 | 101 | result, i, _h = [], 0, handle_error("playfair", errors, decode=True) |
121 | 102 | while i < len(chars): |
122 | 103 | try: |
|
0 commit comments