Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from operator import itemgetter
- import urllib
- def getStats(string):
- letters = [chr(i) for i in xrange(0,256)]
- output = {}
- string = string.lower()
- string = string.replace(" ", "").replace(".", "").replace(",","")
- string = string.replace("\n","").replace("\r","")
- for letter in letters:
- count = string.count(letter)
- if count > 0 and ord(letter) >= ord('a') and ord(letter) <= ord('z'):
- output[letter] = float(count)/len(string) * 100
- return output
- def decipher(statsStr, cipherStr):
- statsStr = statsStr.lower()
- cipherStr = cipherStr.lower()
- stats = sorted(getStats(statsStr).items(), key=itemgetter(1))
- stats = convertList(stats)
- cipher = sorted(getStats(cipherStr).items(), key=itemgetter(1))
- cipher = convertList(cipher)
- cipherList = list(cipherStr)
- outputList = list(cipherStr)
- for i in xrange(0, len(stats)):
- outputList = listReplace(outputList, cipherList, cipher[i], stats[i])
- return "".join(outputList)
- def convertList(list):
- return [item[0] for item in list]
- def listReplace(output, input, replaced, replacing):
- for i in xrange(0, len(input)):
- if input[i] == replaced:
- output[i] = replacing
- return output
- def main():
- string = urllib.urlopen("http://home.agh.edu.pl/~kanczula/kryptografia/lab/lab02/text.txt").read()
- cipher = urllib.urlopen("http://home.agh.edu.pl/~kanczula/kryptografia/lab/lab02/szyfrogram.txt").read()
- print decipher(string, cipher)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment