ddcb4230a4a6c26e1f386caf6964ced01770d1fc
[cipher-training.git] / cipherbreak.py
1 """A set of functions to break the ciphers give in ciphers.py.
2 """
3
4 import string
5 import collections
6 import norms
7 import logging
8 import math
9
10 import matplotlib.pyplot as plt
11
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)
17
18 from cipher import *
19 from language_models import *
20
21 # To time a run:
22 #
23 # import timeit
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)
27
28
29 def frequencies(text):
30 """Count the number of occurrences of each character in text
31
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']
54 0
55 """
56 return collections.Counter(c for c in text)
57
58
59 def caesar_break(message, fitness=Pletters):
60 """Breaks a Caesar cipher using frequency analysis
61
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...)
71 """
72 sanitised_message = sanitise(message)
73 best_shift = 0
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,
80 plaintext[:50]))
81 if fit > best_fit:
82 best_fit = fit
83 best_shift = shift
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
88
89
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)]
93 f = plt.figure()
94 ax = f.add_axes([0.1, 0.1, 0.9, 0.9])
95 ax.bar(x, y, align='center')
96 ax.set_xticks(x)
97 ax.set_xticklabels(sorted(freqs.keys(), key=sort_key))
98 f.show()
99
100
101 if __name__ == "__main__":
102 import doctest
103 doctest.testmod()