1 """A set of functions to break the ciphers give in ciphers.py.
9 import matplotlib
.pyplot
as plt
11 logger
= logging
.getLogger(__name__
)
12 logger
.addHandler(logging
.FileHandler('cipher.log'))
13 logger
.setLevel(logging
.WARNING
)
14 #logger.setLevel(logging.INFO)
15 #logger.setLevel(logging.DEBUG)
18 from language_models
import *
23 # c5a = open('2012/5a.ciphertext', 'r').read()
24 # timeit.timeit('keyword_break(c5a)', setup='gc.enable() ; from __main__ import c5a ; from cipher import keyword_break', number=1)
25 # timeit.repeat('keyword_break_mp(c5a, chunksize=500)', setup='gc.enable() ; from __main__ import c5a ; from cipher import keyword_break_mp', repeat=5, number=1)
28 def frequencies(text
):
29 """Count the number of occurrences of each character in text
31 >>> sorted(frequencies('abcdefabc').items())
32 [('a', 2), ('b', 2), ('c', 2), ('d', 1), ('e', 1), ('f', 1)]
33 >>> sorted(frequencies('the quick brown fox jumped over the lazy ' \
34 'dog').items()) # doctest: +NORMALIZE_WHITESPACE
35 [(' ', 8), ('a', 1), ('b', 1), ('c', 1), ('d', 2), ('e', 4), ('f', 1),
36 ('g', 1), ('h', 2), ('i', 1), ('j', 1), ('k', 1), ('l', 1), ('m', 1),
37 ('n', 1), ('o', 4), ('p', 1), ('q', 1), ('r', 2), ('t', 2), ('u', 2),
38 ('v', 1), ('w', 1), ('x', 1), ('y', 1), ('z', 1)]
39 >>> sorted(frequencies('The Quick BROWN fox jumped! over... the ' \
40 '(9lazy) DOG').items()) # doctest: +NORMALIZE_WHITESPACE
41 [(' ', 8), ('!', 1), ('(', 1), (')', 1), ('.', 3), ('9', 1), ('B', 1),
42 ('D', 1), ('G', 1), ('N', 1), ('O', 2), ('Q', 1), ('R', 1), ('T', 1),
43 ('W', 1), ('a', 1), ('c', 1), ('d', 1), ('e', 4), ('f', 1), ('h', 2),
44 ('i', 1), ('j', 1), ('k', 1), ('l', 1), ('m', 1), ('o', 2), ('p', 1),
45 ('r', 1), ('t', 1), ('u', 2), ('v', 1), ('x', 1), ('y', 1), ('z', 1)]
46 >>> sorted(frequencies(sanitise('The Quick BROWN fox jumped! over... '\
47 'the (9lazy) DOG')).items()) # doctest: +NORMALIZE_WHITESPACE
48 [('a', 1), ('b', 1), ('c', 1), ('d', 2), ('e', 4), ('f', 1), ('g', 1),
49 ('h', 2), ('i', 1), ('j', 1), ('k', 1), ('l', 1), ('m', 1), ('n', 1),
50 ('o', 4), ('p', 1), ('q', 1), ('r', 2), ('t', 2), ('u', 2), ('v', 1),
51 ('w', 1), ('x', 1), ('y', 1), ('z', 1)]
52 >>> frequencies('abcdefabcdef')['x']
55 return collections
.Counter(c
for c
in text
)
58 def caesar_break(message
, fitness
=Pletters
):
59 """Breaks a Caesar cipher using frequency analysis
61 >>> caesar_break('ibxcsyorsaqcheyklxivoexlevmrimwxsfiqevvmihrsasrxliwyrh' \
62 'ecjsppsamrkwleppfmergefifvmhixscsymjcsyqeoixlm') # doctest: +ELLIPSIS
63 (4, -130.849989015...)
64 >>> caesar_break('wxwmaxdgheetgwuxztgptedbgznitgwwhpguxyhkxbmhvvtlbhgtee' \
65 'raxlmhiixweblmxgxwmhmaxybkbgztgwztsxwbgmxgmert') # doctest: +ELLIPSIS
66 (19, -128.82410410...)
67 >>> caesar_break('yltbbqnqnzvguvaxurorgenafsbezqvagbnornfgsbevpnaabjurer' \
68 'svaquvzyvxrnznazlybequrvfohgriraabjtbaruraprur') # doctest: +ELLIPSIS
69 (13, -126.25403935...)
71 sanitised_message
= sanitise(message
)
73 best_fit
= float('-inf')
74 for shift
in range(26):
75 plaintext
= caesar_decipher(sanitised_message
, shift
)
76 fit
= fitness(plaintext
)
77 logger
.debug('Caesar break attempt using key {0} gives fit of {1} '
78 'and decrypt starting: {2}'.format(shift
, fit
,
83 logger
.info('Caesar break best fit: key {0} gives fit of {1} and '
84 'decrypt starting: {2}'.format(best_shift
, best_fit
,
85 caesar_decipher(sanitised_message
, best_shift
)[:50]))
86 return best_shift
, best_fit
89 def plot_frequency_histogram(freqs
, sort_key
=None):
90 x
= range(len(freqs
.keys()))
91 y
= [freqs
[l
] for l
in sorted(freqs
.keys(), key
=sort_key
)]
93 ax
= f
.add_axes([0.1, 0.1, 0.9, 0.9])
94 ax
.bar(x
, y
, align
='center')
96 ax
.set_xticklabels(sorted(freqs
.keys(), key
=sort_key
))
100 if __name__
== "__main__":