-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc03.py
61 lines (44 loc) · 1.95 KB
/
c03.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from binascii import unhexlify
# https://en.wikipedia.org/wiki/Letter_frequency
english_frequency = {" ": 13, "a": 8.16, "b": 1.49, "c": 2.78, "d": 4.25, "e": 12.70, "f": 2.22, "g": 2.01, "h": 6.09,
"i": 6.96, "j": 0.15, "k": 0.77, "l": 4.02, "m": 2.40, "n": 6.74, "o": 7.50, "p": 1.92, "q": 0.09,
"r": 5.98, "s": 6.32, "t": 9.05, "u": 2.75, "v": 0.97, "w": 2.36, "x": 0.15, "y": 1.97, "z": 0.07}
def single_character_xor(s, k):
"""
XORs all characters in s against k
"""
return "".join([chr(c ^ k) for c in s])
def english_frequency_score(s):
"""
Floating point score of all characters in s, scored against english frequency score table
"""
return sum([english_frequency[c] if c in english_frequency else 0 for c in s])
def most_english_definition(s):
"""
Tries all 255 options with the single key XOR method, and scores with the english frequency table.
Returns a tuple of the highest score and the corresponding text.
"""
most_english = ""
highest_score = 0
for i in range(255):
result = single_character_xor(s, i)
score = english_frequency_score(result)
if score > highest_score:
highest_score = score
most_english = result
return highest_score, most_english
def main():
score, result = most_english_definition(
unhexlify(b"1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736")
)
assert result == "Cooking MC's like a pound of bacon"
if __name__ == '__main__':
main()
"""
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.
"""