Advertisement
Guest User

Untitled

a guest
Nov 6th, 2012
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. import random
  2. from ngram_score import ngram_score
  3. import re
  4.  
  5. fitness = ngram_score('quadgrams.txt') # load our quadgram model
  6.  
  7. # helper function, converts an integer 0-25 into a character
  8. def i2a(i): return 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[i%26]
  9.  
  10. # decipher a piece of text using the substitution cipher and a certain key    
  11. def sub_decipher(text,key):
  12.     invkey = [i2a(key.index(i)) for i in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ']
  13.  
  14.    
  15.     ret = ''      
  16.     for c in text:
  17.         if c.isalpha(): ret += invkey[ord(c.upper())-ord('A')]
  18.         else: ret += c
  19.     return ret
  20.  
  21. def break_simplesub(ctext,startkey=None):
  22.     ''' perform hill-climbing with a single start. This function may have to be called many times
  23.        to break a substitution cipher. '''
  24.     # make sure ciphertext has all spacing/punc removed and is uppercase
  25.     ctext = re.sub('[^A-Z]','',ctext.upper())
  26.     parentkey,parentscore = startkey or list('ABCDEFGHIJKLMNOPQRSTUVWXYZ'),-99e99
  27.     if not startkey: random.shuffle(parentkey)
  28.     parentscore = fitness.score(sub_decipher(ctext,parentkey))
  29.     count = 0
  30.     while count < 1000:
  31.         a = random.randint(0,25)
  32.         b = random.randint(0,25)
  33.         child = parentkey[:]
  34.         # swap two characters in the child
  35.         child[a],child[b] = child[b],child[a]
  36.         score = fitness.score(sub_decipher(ctext,child))
  37.         # if the child was better, replace the parent with it
  38.         if score > parentscore:
  39.             parentscore, parentkey = score, child[:]
  40.             count = 0 # reset the counter
  41.         count += 1
  42.     return parentscore, parentkey
  43.  
  44. ctext = 'ag nc nud uvfznavw bsvgadcvzauy zs xuf, nc erszc az av bahncr, znuz ax, pf xs bnuvwavw znc srdcr sg znc yczzcrx sg znc uyhnupcz, znuz vsz uesrd bsmyd pc qudc smz. Ig uvfsvc eaxncx zs dcbahncr zncxc, uvd wcz uz zncar qcuvavw, nc qmxz xmpxzazmzc znc gsmrzn yczzcr sg znc uyhnupcz, vuqcyf D, gsr A, uvd xs eazn znc szncrx'
  45.    
  46. print "Substitution Cipher solver, you may have to wait several iterations"
  47. print "for the correct result. Press ctrl+c to exit program."
  48.  
  49. # keep going until we are killed by the user
  50. i = 0
  51. maxscore = -99e99
  52. while 1:
  53.     i = i+1 # keep track of how many iterations we have done
  54.     score, key = break_simplesub(ctext,list('ABCDEFGHIJKLMNOPQRSTUVWXYZ'))
  55.     if score > maxscore:
  56.         maxscore,maxkey = score,key[:]
  57.         print '\nbest score so far:',maxscore,'on iteration',i
  58.         print '    best key: '+''.join(maxkey)
  59.         print '    plaintext: '+ sub_decipher(ctext,maxkey)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement