Skip to content

Instantly share code, notes, and snippets.

@mikeecb
Last active August 18, 2020 22:19
Show Gist options
  • Save mikeecb/a4c988da9a2931e6b0c82af086dc01cb to your computer and use it in GitHub Desktop.
Save mikeecb/a4c988da9a2931e6b0c82af086dc01cb to your computer and use it in GitHub Desktop.
Key Size Simplified
b = bytearray("".join(list(open("6.txt", "r"))).decode("base64"))
normalized_distances = []
for KEYSIZE in range(2, 40):
b1 = b[:KEYSIZE]
b2 = b[KEYSIZE:KEYSIZE*2]
b3 = b[KEYSIZE*2:KEYSIZE*3]
b4 = b[KEYSIZE*3:KEYSIZE*4]
normalized_distance = float(
hamming_distance(b1, b2) +
hamming_distance(b2, b3) +
hamming_distance(b3, b4)
) / (KEYSIZE * 3)
normalized_distances.append(
(KEYSIZE, normalized_distance)
)
normalized_distances = sorted(normalized_distances, key=lambda (_, y): y)
# [(2, 2.0), (3, 2.6666666666666665), (29, 2.793103448275862), ...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment