print key, xor_mb(message, key)
### Challenge 4 - Detect single-character XOR
+ - Solution non-frequency analysis approach
+from collections import Counter
+from itertools import cycle
+def xor_mb(message, key):
+ return''.join(chr(ord(m_byte)^ord(k_byte)) for m_byte,k_byte in zip(message, cycle(key)))
+printset = set(string.printable)
+messages = open("4.txt", 'r').readlines()
+ message = line.rstrip().decode('hex')
+ for vv in cnt.most_common(3):
+ for char in "ETAOINSHRDLU".lower():
+ key = chr(ord(vv[0]) ^ ord(char))
+ temp = xor_mb(message, key)
+ if set(temp).issubset(printset) and temp[-1] == "\n":
- https://dbjergaard.github.io/posts/matasano_set_1.html
- [Pearson's chi-squared test](https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test)
- [Chi-square distribution introduction | Probability and Statistics | Khan Academy](https://www.youtube.com/watch?v=dXB3cUGnaxQ)