Guest User

Untitled

a guest
Mar 22nd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #!/usr/bin/python
  2. def bestDecryption(ciphertext):
  3. bestKey = -1
  4. maxScore = -1
  5. bestPlaintext = ''
  6. for val in range(256):
  7. plaintext = xorDecrypt(ciphertext,val)
  8. score = englishScore(plaintext)
  9.  
  10. if score > maxScore:
  11. maxScore, bestKey, bestPlaintext = score, val, plaintext
  12.  
  13. return (bestKey, maxScore, bestPlaintext)
  14.  
  15. def xorDecrypt(ciphertext,val):
  16. cipherBlocks = [ciphertext[i:i+2] for i in range(0,len(ciphertext),2)]
  17. phrase = ''
  18. for b in cipherBlocks:
  19. phrase += chr(int(b,16) ^ val)
  20. return phrase
  21.  
  22. def englishScore(plaintext):
  23. score = 0
  24. for letter in plaintext:
  25. if letter in ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz':
  26. score += 1
  27. return float(score)/len(plaintext)
  28.  
  29. def main():
  30. ciphertext = '1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736'
  31. key, score, plaintext = bestDecryption(ciphertext)
  32.  
  33. print("Ciphertext =", ciphertext)
  34. print("Challenge 3 Answer:")
  35. print("Key =", key, "Score =", score)
  36. print("Plaintext =", plaintext)
  37.  
  38. if __name__ == "__main__":
  39. main()
Add Comment
Please, Sign In to add comment