-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
92 lines (68 loc) · 3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from pycipher import Caesar
from breaking.caesar import CaesarBreak
from pycipher import Vigenere
from breaking.vigenere import KeylengthDetector
from breaking.vigenere import VigenereBreak
from pycipher import SimpleSubstitution
from breaking.substitution import SubstitutionBreak
from util.transforms import Masker
from score.ioc import IocScorer
from data.en import load_ngrams
from score.ngram import NgramScorer
def break_caesar_example(plaintext, masker):
print("#######################################")
print("######## Caesar cipher example ########")
print("#######################################")
key = 13
ciphertext = Caesar(key).encipher(plaintext)
print("\nCiphertext:\n---")
print(masker.extend(ciphertext))
print("---\n")
print("\nCracking...\n")
scorer = NgramScorer(load_ngrams(3))
breaker = CaesarBreak(scorer)
decryption, score, key = breaker.guess(ciphertext, 1)[0]
print("Caesar decryption (key={}, score={}):\n---\n{}---\n"
.format(key, score, masker.extend(decryption)))
def break_vigenere_example(plaintext, masker):
print("#########################################")
print("######## Vigenere cipher example ########")
print("#########################################")
key = "somekey"
ciphertext = Vigenere(key).encipher(plaintext)
print("\nCiphertext:\n---")
print(masker.extend(ciphertext))
print("---\n")
print("\nCracking...\n")
print("Inferring key length...")
s = IocScorer(alphabet_size=26)
KeylengthDetector(s).detect(ciphertext)
print("Cracking with key length 7... (11 was false positive)")
scorer = NgramScorer(load_ngrams(1))
breaker = VigenereBreak(7, scorer)
decryption, score, key = breaker.guess(ciphertext)[0]
print("Vigenere decryption (key={}, score={}):\n---\n{}---\n"
.format(key, score, masker.extend(decryption)))
def break_substitution_example(plaintext, masker):
print("#############################################")
print("######## Substitution cipher example ########")
print("#############################################")
key = ['L', 'C', 'N', 'D', 'T', 'H', 'E', 'W', 'Z', 'S', 'A', 'R', 'X',
'V', 'O', 'J', 'B', 'P', 'F', 'U', 'I', 'Q', 'M', 'K', 'G', 'Y']
ciphertext = SimpleSubstitution(key).encipher(plaintext)
print("\nCiphertext:\n---")
print(masker.extend(ciphertext))
print("---\n")
print("\nCracking...\n")
scorer = NgramScorer(load_ngrams(4))
breaker = SubstitutionBreak(scorer, seed=42)
breaker.optimise(ciphertext, n=3)
decryption, score, key = breaker.guess(ciphertext)[0]
print("Substitution decryption (key={}, score={}):\n---\n{}---\n"
.format(key, score, masker.extend(decryption)))
if __name__ == "__main__":
with open("examples/text.txt", "r") as f:
plaintext, masker = Masker.from_text(f.read())
break_caesar_example(plaintext, masker)
break_vigenere_example(plaintext, masker)
break_substitution_example(plaintext, masker)