-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxxencode.py
More file actions
44 lines (41 loc) · 1.65 KB
/
xxencode.py
File metadata and controls
44 lines (41 loc) · 1.65 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
from pycipher import SimpleSubstitution as SimpleSub
import random
import re
from ngram_score import ngram_score
fitness = ngram_score('quadgrams.txt') # load our quadgram statistics
ctext = 'pmpafxaikkitprdsikcplifhwceigixkirradfeirdgkipgigudkcekiigpwrpucikceiginasikwduearrxiiqepcceindgmieinpwdfprduppcedoikiqiasafmfddfipfgmdafmfdteiki'
ctext = re.sub('[^A-Z]', '', ctext.upper())
maxkey = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
maxscore = -99e9
parentscore, parentkey = maxscore, maxkey[:]
print "Substitution Cipher solver, you may have to wait several iterations"
print "for the correct result. Press ctrl+c to exit program."
# keep going until we are killed by the user
i = 0
while 1:
i = i + 1
random.shuffle(parentkey)
deciphered = SimpleSub(parentkey).decipher(ctext)
parentscore = fitness.score(deciphered)
count = 0
while count < 1000:
a = random.randint(0, 25)
b = random.randint(0, 25)
child = parentkey[:]
# swap two characters in the child
child[a], child[b] = child[b], child[a]
deciphered = SimpleSub(child).decipher(ctext)
score = fitness.score(deciphered)
# if the child was better, replace the parent with it
if score > parentscore:
parentscore = score
parentkey = child[:]
count = 0
count = count + 1
# keep track of best score seen so far
if parentscore > maxscore:
maxscore, maxkey = parentscore, parentkey[:]
print '\nbest score so far:', maxscore, 'on iteration', i
ss = SimpleSub(maxkey)
print ' best key: ' + ''.join(maxkey)
print ' plaintext: ' + ss.decipher(ctext)