Skip to content

Instantly share code, notes, and snippets.

@mikeecb
Last active August 18, 2020 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeecb/0d75f46521fe526a0138ae5265392505 to your computer and use it in GitHub Desktop.
Save mikeecb/0d75f46521fe526a0138ae5265392505 to your computer and use it in GitHub Desktop.
Cryptopals Challenge Set 1 Exercise 3
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
def break_single_key_xor(b1):
max_score = None
english_plaintext = None
key = None
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)
return key, english_plaintext
b1 = bytearray.fromhex("1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736")
print break_single_key_xor(b1)
# ('X', "Cooking MC's like a pound of bacon")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment