-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.py
28 lines (21 loc) · 1.05 KB
/
3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""
! Single-byte XOR cipher
* The hex encoded string:
* 1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736
* ... has been XOR'd against a single character. Find the key, decrypt the message.
* You can do this by hand. But don't: write code to do it for you.
? How? Devise some method for "scoring" a piece of English plaintext. Character frequency is a good metric. Evaluate each output and choose the one with the best score.
* Achievement Unlocked
* You now have our permission to make "ETAOIN SHRDLU" jokes on Twitter.
* This solution uses a pretty primitive scoring mechanism
* Character frequencies taken from https://gist.github.com/pozhidaevak/0dca594d6f0de367f232909fe21cdb2f
"""
from utils import score, decode
def single_byte_xor_cipher(hex_input):
return max(range(256), key = lambda x: score(decode(hex_input, x)))
def main():
ciphertext = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"
key = single_byte_xor_cipher(ciphertext)
print(decode(ciphertext, key))
if __name__ == '__main__':
main()