Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- def nice_string(s):
- """ This is supposed to check if a string looks like English. All it
- does for the time being is return `False` if the string contains a
- space followed by punctuation, or punctuation followed by a letter.
- Returns `True` otherwise. """
- if re.search(' [\.\?,]',s):
- return False
- if re.search('[\.\?,]\w',s):
- return False
- return True
- def solve1(ciphertext):
- """ Checks permutations of a string with different offsets between one
- character and the next. When a character is reached, it is replaced
- with an asterisk to indicate that it should be skipped next time. """
- nchars = len(ciphertext)
- for delta in range(1,nchars):
- c = ciphertext
- out = ""
- p = 0
- for i in range(nchars):
- while c[p]=="*":
- p = (p+nchars-1) % nchars
- out = out + c[p]
- c = c[:p] + "*" + c[p+1:]
- p = (p+delta) % nchars
- if nice_string(out):
- print "(solve1) delta=%3d: %s" % (delta, out[:72])
- if len(c) != len(ciphertext):
- raise hell
- def solve2(ciphertext):
- """ Like `solve1()`, but with an offset that increments at each step. """
- nchars = len(ciphertext)
- for delta in range(1,nchars):
- c = ciphertext
- out = ""
- p = 0
- d = delta
- for i in range(nchars):
- while c[p]=="*":
- p = (p+1) % nchars
- out = out + c[p]
- c = c[:p] + "*" + c[p+1:]
- p = (p+d) % nchars
- d = d + 1
- if nice_string(out):
- print "(solve2) delta=%3d: %s" % (delta, out[:72])
- def solve3(ciphertext):
- """ Like `solve1()`, but with offsets based on the digits of pi. """
- pi500 = '314159265358979323846264338327950288419716939937510582097494' \
- '459230781640628620899862803482534211706798214808651328230664' \
- '709384460955058223172535940812848111745028410270193852110555' \
- '964462294895493038196442881097566593344612847564823378678316' \
- '527120190914564856692346034861045432664821339360726024914127' \
- '372458700660631558817488152092096282925409171536436789259036' \
- '001133053054882046652138414695194151160943305727036575959195' \
- '309218611738193261179310511854807446237996274956735188575272' \
- '48912279381830119491'
- nchars = len(ciphertext)
- c = ciphertext
- out = ""
- p = 0
- for i in range(nchars):
- while c[p]=="*":
- p = (p+nchars-1) % nchars
- out = out + c[p]
- c = c[:p] + "*" + c[p+1:]
- d = int(pi500[i]) or 10
- p = (p+d) % nchars
- if nice_string(out):
- print "(solve3)",out[:72]
- def tryall():
- """ Send every cyclical permutation to the `solve()` functions """
- cipher = "HA, TITEXGB DTESTRI AT UE A ONTCTSOMNDEOTA DN UEYCN HRUSN.I" \
- "ETH AEPT CR F TNGEYI?OI A RCA UODUTYE COHNTUI?EURYI HEPSA T R"
- """ No luck with this either:
- cipher = " ORIC.ELON TIHPE, E RET THELXLA. E OWR IU IGEYLRLIA -AOEW NV" \
- " GEU YLARNO AHOTAX SCATIATXI LTT SNITTL STI LL. FSSOYKWE RWOI" \
- " ENHETUAI ATLR EE SOF, NPSBARSCTEO R EO AIU YIECAULSNH: ITRS" \
- " MERHASDYTSCE OFTK RA LIWSTOC ORD VLWEIDS OLSNI!NHU EIA AIS " \
- "T DE TKDTE XIEOSAFWSMPIOT RVONR MOIRTH ONIO NET SFLPI AUDFND" \
- "MA ZZOI"
- """
- for i in range(len(cipher)):
- solve1(cipher)
- solve2(cipher)
- solve3(cipher)
- cipher = cipher[-1] + cipher[:-1] # Rotate string
- tryall()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement