+"""Simple digraph substitution cipher, using the
+[Polybius square](https://en.wikipedia.org/wiki/Polybius_square). Enciphering
+and deciphering, and a couple of ways to break these ciphers.
+"""
import multiprocessing
-from support.utilities import *
-from support.language_models import *
-from cipher.keyword_cipher import KeywordWrapAlphabet, keyword_cipher_alphabet_of
-
-from logger import logger
+from szyfrow.support.utilities import *
+from szyfrow.support.language_models import *
+from szyfrow.keyword_cipher import KeywordWrapAlphabet, keyword_cipher_alphabet_of
def polybius_grid(keyword, column_order, row_order, letters_to_merge=None,
wrap_alphabet=KeywordWrapAlphabet.from_a):
def polybius_flatten(pair, column_first):
- """Convert a series of pairs into a single list of characters"""
+ """Convert a pair of characters into a single string."""
if column_first:
return str(pair[1]) + str(pair[0])
else:
"""Encipher a message with Polybius cipher, using a keyword to rearrange
the alphabet
-
>>> polybius_encipher('this is a test message for the ' \
'polybius decipherment', 'elephant', \
[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], \
column_index_type = type(column_order[0])
row_index_type = type(row_order[0])
if column_first:
- pairs = [(column_index_type(p[1]), row_index_type(p[0])) for p in chunks(message, 2)]
+ pairs = [(column_index_type(p[1]), row_index_type(p[0]))
+ for p in chunks(message, 2)]
else:
- pairs = [(row_index_type(p[0]), column_index_type(p[1])) for p in chunks(message, 2)]
+ pairs = [(row_index_type(p[0]), column_index_type(p[1]))
+ for p in chunks(message, 2)]
return cat(grid[p] for p in pairs if p in grid)
-def polybius_break_mp(message, column_labels, row_labels,
+def polybius_break(message, column_labels, row_labels,
letters_to_merge=None,
wordlist=keywords, fitness=Pletters,
number_of_solutions=1, chunksize=500):
fit = fitness(plaintext)
else:
fit = float('-inf')
- logger.debug('Polybius break attempt using key {0} (wrap={1}, merging {2}), '
- 'columns as {3}, rows as {4} (column_first={5}) '
- 'gives fit of {6} and decrypt starting: '
- '{7}'.format(keyword, wrap_alphabet, letters_to_merge,
- column_order, row_order, column_first,
- fit, sanitise(plaintext)[:50]))
return (keyword, wrap_alphabet, column_order, row_order, column_first), fit
if __name__ == "__main__":