X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=cipherbreak.py;h=f1ab58e50a4f293368ef9d5382aef9dabf590420;hb=8543ce0d45e4046c2c056e395ddf91d3c8a35586;hp=4f443c419e8d27cfb8746d8f52a262cf7d19bff2;hpb=102ee4fd38ce5dd5a39e317f1bdca06b82e4d690;p=cipher-tools.git diff --git a/cipherbreak.py b/cipherbreak.py index 4f443c4..f1ab58e 100644 --- a/cipherbreak.py +++ b/cipherbreak.py @@ -13,12 +13,23 @@ from multiprocessing import Pool import matplotlib.pyplot as plt -logging.basicConfig(filename="cipher.log", level=logging.INFO) -logger = logging.getLogger(__name__) -# logger.setLevel(logging.WARNING) +# logging.basicConfig(filename="cipher.log", level=logging.INFO) +# logger = logging.getLogger(__name__) + +logger = logging.getLogger('cipherbreak') +logger.setLevel(logging.WARNING) # logger.setLevel(logging.INFO) # logger.setLevel(logging.DEBUG) +# create the logging file handler +fh = logging.FileHandler("cipher.log") +formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') +fh.setFormatter(formatter) + +# add handler to logger object +logger.addHandler(fh) + + from cipher import * from language_models import * @@ -558,6 +569,43 @@ def hill_break_worker(message, matrix, fitness): fit, sanitise(plaintext)[:50])) return matrix, fit +def bifid_break_mp(message, wordlist=keywords, fitness=Pletters, max_period=10, + number_of_solutions=1, chunksize=500): + """Breaks a keyword substitution cipher using a dictionary and + frequency analysis + + >>> bifid_break_mp(bifid_encipher('this is a test message for the ' \ + 'keyword decipherment', 'elephant', wrap_alphabet=KeywordWrapAlphabet.from_last), \ + wordlist=['cat', 'elephant', 'kangaroo']) # doctest: +ELLIPSIS + (('elephant', , 0), -52.834575011...) + >>> bifid_break_mp(bifid_encipher('this is a test message for the ' \ + 'keyword decipherment', 'elephant', wrap_alphabet=KeywordWrapAlphabet.from_last), \ + wordlist=['cat', 'elephant', 'kangaroo'], \ + number_of_solutions=2) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE + [(('elephant', , 0), -52.834575011...), + (('elephant', , 0), -52.834575011...)] + """ + with Pool() as pool: + helper_args = [(message, word, wrap, period, fitness) + for word in wordlist + for wrap in KeywordWrapAlphabet + for period in range(max_period+1)] + # Gotcha: the helper function here needs to be defined at the top level + # (limitation of Pool.starmap) + breaks = pool.starmap(bifid_break_worker, helper_args, chunksize) + if number_of_solutions == 1: + return max(breaks, key=lambda k: k[1]) + else: + return sorted(breaks, key=lambda k: k[1], reverse=True)[:number_of_solutions] + +def bifid_break_worker(message, keyword, wrap_alphabet, period, fitness): + plaintext = bifid_decipher(message, keyword, wrap_alphabet, period=period) + fit = fitness(plaintext) + logger.debug('Keyword break attempt using key {0} (wrap={1}) gives fit of ' + '{2} and decrypt starting: {3}'.format(keyword, + wrap_alphabet, fit, sanitise(plaintext)[:50])) + return (keyword, wrap_alphabet, period), fit + def pocket_enigma_break_by_crib(message, wheel_spec, crib, crib_position): """Break a pocket enigma using a crib (some plaintext that's expected to