X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=szyfrow%2Fpolybius.py;h=d037ec9c0b212d03784fe64744f0c42eeb08c64a;hb=748b5cceaa346d3097c58229ea04ead0c7e3f48e;hp=cd78ee9f8a23445f9cc7355bac876f270b30a41f;hpb=27c8005f6dea0026887b80a01b5f93a8f1b3c2b2;p=szyfrow.git diff --git a/szyfrow/polybius.py b/szyfrow/polybius.py index cd78ee9..d037ec9 100644 --- a/szyfrow/polybius.py +++ b/szyfrow/polybius.py @@ -1,3 +1,7 @@ +"""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 szyfrow.support.utilities import * from szyfrow.support.language_models import * @@ -48,7 +52,7 @@ def polybius_reverse_grid(keyword, column_order, row_order, letters_to_merge=Non 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: @@ -60,7 +64,6 @@ def polybius_encipher(message, keyword, column_order, row_order, """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], \ @@ -101,13 +104,15 @@ def polybius_decipher(message, keyword, column_order, row_order, 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):