Advertisement
Guest User

A waste of processor cycles

a guest
Dec 11th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1. import re
  2.  
  3. def nice_string(s):
  4.    
  5.     """ This is supposed to check if a string looks like English. All it
  6.        does for the time being is return `False` if the string contains a
  7.        space followed by punctuation, or punctuation followed by a letter.
  8.        Returns `True` otherwise. """
  9.    
  10.     if re.search(' [\.\?,]',s):
  11.         return False
  12.     if re.search('[\.\?,]\w',s):
  13.         return False
  14.     return True
  15.  
  16.  
  17.  
  18. def solve1(ciphertext):
  19.    
  20.     """ Checks permutations of a string with different offsets between one
  21.        character and the next. When a character is reached, it is replaced
  22.        with an asterisk to indicate that it should be skipped next time. """
  23.    
  24.     nchars = len(ciphertext)
  25.     for delta in range(1,nchars):
  26.         c = ciphertext
  27.         out = ""
  28.         p = 0
  29.         for i in range(nchars):
  30.             while c[p]=="*":
  31.                 p = (p+nchars-1) % nchars
  32.             out = out + c[p]
  33.             c = c[:p] + "*" + c[p+1:]
  34.             p = (p+delta) % nchars
  35.         if nice_string(out):
  36.             print "(solve1) delta=%3d: %s" % (delta, out[:72])
  37.     if len(c) != len(ciphertext):
  38.         raise hell
  39.  
  40.  
  41.  
  42. def solve2(ciphertext):
  43.    
  44.     """ Like `solve1()`, but with an offset that increments at each step. """
  45.    
  46.     nchars = len(ciphertext)
  47.     for delta in range(1,nchars):
  48.         c = ciphertext
  49.         out = ""
  50.         p = 0
  51.         d = delta
  52.         for i in range(nchars):
  53.             while c[p]=="*":
  54.                 p = (p+1) % nchars
  55.             out = out + c[p]
  56.             c = c[:p] + "*" + c[p+1:]
  57.             p = (p+d) % nchars
  58.             d = d + 1
  59.         if nice_string(out):
  60.             print "(solve2) delta=%3d: %s" % (delta, out[:72])
  61.  
  62.  
  63.  
  64. def solve3(ciphertext):
  65.    
  66.     """ Like `solve1()`, but with offsets based on the digits of pi. """
  67.    
  68.     pi500 = '314159265358979323846264338327950288419716939937510582097494' \
  69.             '459230781640628620899862803482534211706798214808651328230664' \
  70.             '709384460955058223172535940812848111745028410270193852110555' \
  71.             '964462294895493038196442881097566593344612847564823378678316' \
  72.             '527120190914564856692346034861045432664821339360726024914127' \
  73.             '372458700660631558817488152092096282925409171536436789259036' \
  74.             '001133053054882046652138414695194151160943305727036575959195' \
  75.             '309218611738193261179310511854807446237996274956735188575272' \
  76.             '48912279381830119491'
  77.    
  78.     nchars = len(ciphertext)
  79.     c = ciphertext
  80.     out = ""
  81.     p = 0
  82.     for i in range(nchars):
  83.         while c[p]=="*":
  84.             p = (p+nchars-1) % nchars
  85.         out = out + c[p]
  86.         c = c[:p] + "*" + c[p+1:]
  87.         d = int(pi500[i]) or 10
  88.         p = (p+d) % nchars
  89.     if nice_string(out):
  90.         print "(solve3)",out[:72]
  91.  
  92.  
  93.  
  94. def tryall():
  95.    
  96.     """ Send every cyclical permutation to the `solve()` functions """
  97.    
  98.     cipher = "HA, TITEXGB   DTESTRI AT UE A ONTCTSOMNDEOTA DN UEYCN HRUSN.I" \
  99.              "ETH AEPT CR  F TNGEYI?OI A RCA UODUTYE COHNTUI?EURYI HEPSA T R"
  100.    
  101.     """ No luck with this either:
  102.    cipher = " ORIC.ELON TIHPE, E RET THELXLA.  E OWR IU IGEYLRLIA -AOEW NV" \
  103.             " GEU YLARNO AHOTAX SCATIATXI LTT SNITTL STI LL. FSSOYKWE RWOI" \
  104.             " ENHETUAI  ATLR EE SOF, NPSBARSCTEO R EO AIU YIECAULSNH: ITRS" \
  105.             "  MERHASDYTSCE OFTK RA LIWSTOC ORD VLWEIDS OLSNI!NHU EIA AIS " \
  106.             "T DE TKDTE XIEOSAFWSMPIOT  RVONR MOIRTH ONIO NET SFLPI AUDFND" \
  107.             "MA ZZOI"
  108.    """
  109.    
  110.     for i in range(len(cipher)):
  111.         solve1(cipher)
  112.         solve2(cipher)
  113.         solve3(cipher)
  114.         cipher = cipher[-1] + cipher[:-1]  # Rotate string
  115.  
  116.  
  117.  
  118. tryall()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement