+ >>> 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', <KeywordWrapAlphabet.from_last: 2>, 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', <KeywordWrapAlphabet.from_last: 2>, 0), -52.834575011...),
+ (('elephant', <KeywordWrapAlphabet.from_largest: 3>, 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
+ be in a certain position). Returns a list of possible starting wheel
+ positions that could produce the crib.
+
+ >>> pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'h', 0)
+ ['a', 'f', 'q']
+ >>> pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'he', 0)
+ ['a']
+ >>> pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'll', 2)
+ ['a']
+ >>> pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'l', 2)
+ ['a']
+ >>> pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'l', 3)
+ ['a', 'j', 'n']
+ >>> pocket_enigma_break_by_crib('aaaaa', 1, 'l', 3)
+ []
+ """
+ pe = PocketEnigma(wheel=wheel_spec)
+ possible_positions = []
+ for p in string.ascii_lowercase:
+ pe.set_position(p)
+ plaintext = pe.decipher(message)
+ if plaintext[crib_position:crib_position+len(crib)] == crib:
+ possible_positions += [p]
+ return possible_positions