-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19.py
More file actions
172 lines (158 loc) · 6.23 KB
/
19.py
File metadata and controls
172 lines (158 loc) · 6.23 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import aes
import convert
import ecboracle
import string
import xor
def generateCiphertexts():
plaintexts = ["SSBoYXZlIG1ldCB0aGVtIGF0IGNsb3NlIG9mIGRheQ==",
"Q29taW5nIHdpdGggdml2aWQgZmFjZXM=",
"RnJvbSBjb3VudGVyIG9yIGRlc2sgYW1vbmcgZ3JleQ==",
"RWlnaHRlZW50aC1jZW50dXJ5IGhvdXNlcy4=",
"SSBoYXZlIHBhc3NlZCB3aXRoIGEgbm9kIG9mIHRoZSBoZWFk",
"T3IgcG9saXRlIG1lYW5pbmdsZXNzIHdvcmRzLA==",
"T3IgaGF2ZSBsaW5nZXJlZCBhd2hpbGUgYW5kIHNhaWQ=",
"UG9saXRlIG1lYW5pbmdsZXNzIHdvcmRzLA==",
"QW5kIHRob3VnaHQgYmVmb3JlIEkgaGFkIGRvbmU=",
"T2YgYSBtb2NraW5nIHRhbGUgb3IgYSBnaWJl",
"VG8gcGxlYXNlIGEgY29tcGFuaW9u",
"QXJvdW5kIHRoZSBmaXJlIGF0IHRoZSBjbHViLA==",
"QmVpbmcgY2VydGFpbiB0aGF0IHRoZXkgYW5kIEk=",
"QnV0IG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nNmb3JtZWQgdXR0ZXJseTo=",
"QSB0ZXJyaWJsZSBiZWF1dHkgaXMgYm9ybi4="]
plaintexts = map(convert.b64ToByteString, plaintexts)
key = ecboracle.generateRandomKey()
return map(lambda x: aes.aesCTREncrypt(x, key, 0), plaintexts)
def findNgrams(ciphertexts, n, threshold = 1):
Ngrams = []
maxLength = getMaxLength(ciphertexts)
for i in range(maxLength-n+1):
ngramsAtIndex = {}
for j in range(len(ciphertexts)):
curNgram = ciphertexts[j][i:i+n]
if curNgram in ngramsAtIndex:
ngramsAtIndex[curNgram].append(j)
else:
ngramsAtIndex[curNgram] = [j]
for ngram in ngramsAtIndex:
if len(ngramsAtIndex[ngram]) > threshold and len(ngram) == n :
Ngrams.append((i, ngram, ngramsAtIndex[ngram]))
return sorted(Ngrams, key=lambda x: len(x[2]))
def findTrigrams(ciphertexts, threshold = 1):
return findNgrams(ciphertexts, 3, threshold)
def findFrequentLetters(ciphertexts, threshold = 1):
return findNgrams(ciphertexts, 1, threshold)
def getMaxLength(ciphertexts):
return max(map(len, ciphertexts))
def printGuesses(ciphertexts, keystream):
print " "+" ".join([string.rjust(repr( x ), 2) for x in range(len(keystream))])
print " k "+" ".join([string.rjust(repr( keystream[x])[1:-1].replace("\\x", ""), 2) for x in range(len(keystream))])
for i in range(len(ciphertexts)):
print string.rjust(str(i), 2)+" "+" ".join([string.rjust(repr( chr(ord(ciphertexts[i][x])^ord(keystream[x])) )[1:-1].replace("\\x", ""), 2) for x in range(len(ciphertexts[i]))])
def tryCrib(plaintextCrib, position, ciphertextsWithGuess, ciphertexts, keystream):
tempKeyStream = list(keystream)
guessKeyBytes = ciphertexts[ciphertextsWithGuess[0]][position:position+len(plaintextCrib)]
guessKeyBytes = list(xor.xorByteStrings(guessKeyBytes, plaintextCrib))
tempKeyStream[position:position+len(plaintextCrib)] = guessKeyBytes
printGuesses(ciphertexts, tempKeyStream)
keep = raw_input("Accept guess? (y/n) ")
if keep == "y":
keystream[position:position+len(plaintextCrib)] = guessKeyBytes
return True
return False
if __name__ == "__main__":
ciphertexts = generateCiphertexts()
maxLength = getMaxLength(ciphertexts)
keystream = [chr(0)]*maxLength
frequentLetters = findFrequentLetters(ciphertexts, 8)
for letter in frequentLetters:
printGuesses(ciphertexts, "".join(keystream))
print "Letter "+repr(letter[1])+" at position "+str(letter[0])+" in ciphertexts "+repr(letter[2])
guess = raw_input("Guess plaintext?")
while guess != "":
if tryCrib(guess, letter[0], letter[2], ciphertexts, keystream):
guess = ""
else:
guess = raw_input("Guess plaintext?")
trigrams = findTrigrams(ciphertexts, 2)
for trigram in trigrams:
printGuesses(ciphertexts, "".join(keystream))
print "Trigram "+repr(trigram[1])+" at position "+str(trigram[0])+" in ciphertexts "+repr(trigram[2])
guess = raw_input("Guess plaintext?")
while guess != "":
if tryCrib(guess, trigram[0], trigram[2], ciphertexts, keystream):
guess = ""
else:
guess = raw_input("Guess plaintext?")
guess = raw_input("Make a guess (position,ciphertext,guess) or quit (quit): ")
while guess != "quit":
splitGuess = guess.split(",")
tryCrib(splitGuess[2], int(splitGuess[0]), [int(splitGuess[1])], ciphertexts, keystream)
guess = raw_input("Make a guess (position,ciphertext,guess) or quit (quit): ")
'''
ciphertexts:
I have met them at close of day
Coming with vivid faces
From counter or desk among grey
Eighteenth-century houses.
I have passed with a nod of the head
Or polite meaningless words,
Or have lingered a while and said
Polite meaningless words,
And thought before I had done
Of a mocking tale or a gibe
To please a companion
Around the fire at the club,
Being certain that they and I
But lived where motley is worn:
All changed, changed utterly:
A terrible beauty is born.
That woman's days were spent
In ignorant good will,
Her nights in argument
Until her voice grew shrill.
What voice more sweet than hers
When young and beautiful,
She rode to harriers?
This man had kept a school
And rode our winged horse.
This other his helper and friend
Was coming into his force;
He might have won fame in the end,
So sensitive his nature seemed,
So daring and sweet his thought.
This other man I had dreamed
A drunken, vain-glorious lout.
He had done most bitter wrong
o some who are near my heart,
Yet I number him in the song;
He, too, has resigned his part
In the casual comedy;
He, too, has been changed in his time,
Transformed utterly:
A terrible beauty is born.
'''