Created
April 8, 2019 22:03
-
-
Save Ge0rg3/593d318fef7f258a5ca94efe1c581206 to your computer and use it in GitHub Desktop.
Bruteforce 4 byte XOR encryption. Made for HMGCC's BLK_BOX challenge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alphabet = [i for i in range(32, 127)]+[10] | |
getEnglish = lambda text: list(filter(lambda c: c in alphabet, text)) | |
isEnglish = lambda text: len(getEnglish(text)) == len(text) | |
with open('msg', 'rb') as f: | |
encrypted = f.read() | |
samples = [] | |
for i in range(4): | |
samples.append([val for index, val in enumerate(encrypted) if index % 4 == i]) | |
keysFound = [] | |
for a in range(256): | |
decryptAttempt = [val ^ a for val in samples[0]] | |
if isEnglish(decryptAttempt): | |
for b in range(256): | |
decryptAttempt = [val ^ b for val in samples[1]] | |
if (isEnglish(decryptAttempt)): | |
for c in range(256): | |
decryptAttempt = [val ^ c for val in samples[2]] | |
if (isEnglish(decryptAttempt)): | |
for d in range(256): | |
decryptAttempt = [val ^ d for val in samples[3]] | |
if (isEnglish(decryptAttempt)): | |
keysFound.append([a, b, c, d]) | |
for keyset in keysFound: | |
print(keyset) | |
print(''.join(chr(c ^ keyset[i%4]) for i, c in enumerate(encrypted))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment