Last active
August 18, 2020 22:19
-
-
Save mikeecb/e26a56ba2200563b803e428a0ca4ea1b to your computer and use it in GitHub Desktop.
Cryptopals Challenge Set 1 Exercise 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def xor(b1, b2): | |
b = bytearray(len(b1)) | |
for i in range(len(b1)): | |
b[i] = b1[i] ^ b2[i] | |
return b | |
def score(s): | |
freq = {} | |
freq[' '] = 700000000 | |
freq['e'] = 390395169 | |
freq['t'] = 282039486 | |
freq['a'] = 248362256 | |
freq['o'] = 235661502 | |
freq['i'] = 214822972 | |
freq['n'] = 214319386 | |
freq['s'] = 196844692 | |
freq['h'] = 193607737 | |
freq['r'] = 184990759 | |
freq['d'] = 134044565 | |
freq['l'] = 125951672 | |
freq['u'] = 88219598 | |
freq['c'] = 79962026 | |
freq['m'] = 79502870 | |
freq['f'] = 72967175 | |
freq['w'] = 69069021 | |
freq['g'] = 61549736 | |
freq['y'] = 59010696 | |
freq['p'] = 55746578 | |
freq['b'] = 47673928 | |
freq['v'] = 30476191 | |
freq['k'] = 22969448 | |
freq['x'] = 5574077 | |
freq['j'] = 4507165 | |
freq['q'] = 3649838 | |
freq['z'] = 2456495 | |
score = 0 | |
for c in s.lower(): | |
if c in freq: | |
score += freq[c] | |
return score | |
max_score = None | |
english_plaintext = None | |
key = None | |
for line in open("4.txt", "r"): | |
line = line.rstrip() | |
b1 = bytearray.fromhex(line) | |
for i in range(256): | |
b2 = [i] * len(b1) | |
plaintext = bytes(xor(b1, b2)) | |
pscore = score(plaintext) | |
if pscore > max_score or not max_score: | |
max_score = pscore | |
english_plaintext = plaintext | |
key = chr(i) | |
print key, english_plaintext | |
# 5 Now that the party is jumping |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment