Skip to content

Instantly share code, notes, and snippets.

@andrewbolster
Created November 11, 2013 19:17
Show Gist options
  • Save andrewbolster/7418702 to your computer and use it in GitHub Desktop.
Save andrewbolster/7418702 to your computer and use it in GitHub Desktop.
Solution to PyPool 11/11/13 Problem: You have intercepted a coded message. (Also available in plain text.) Your objective is to identify the source of the material. You can see from the character encoding of the HTML document that the encoded message is in Unicode. You have also received intelligence that the original message is in English and t…
import string
from urllib.request import urlopen
from collections import Counter
def getCipherText():
cipherreq = urlopen("http://pypool.daihuws.me.uk/crypto/encrypted.txt")
cipherstr = cipherreq.read().decode("utf-8")
cipherreq.close()
return cipherstr
freqdict = Counter(getCipherText().translate(string.punctuation).split(sep=" "))
# Assume that the most common word is "the"
the = freqdict.most_common()[0]
the_offsets = [ord(c)-ord(t) for c,t in zip(the[0],"the")]
if all(x==the_offsets[0] for x in the_offsets):
print("All Offsets equivalent for \"The\", going ahead with transposition\n\n")
the_offset = the_offsets[0]
clear_text=""
awkward_bastards = []
for c in getCipherText():
if c in string.punctuation or c in string.whitespace:
clear_line+=(c)
else:
try:
clear_line+=(chr(ord(c)-the_offset))
except ValueError:
clear_line+=(c)
awkward_bastards.append(c)
print(clear_line)
if awkward_bastards:
print("\n\nThese characters failed translation and were left as-is: %s"%str(awkward_bastards))
else:
print("This probably isn't a caeser cipher, give up: %s"%(str(the_offsets)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment