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