Skip to content

Instantly share code, notes, and snippets.

@Ge0rg3
Created April 8, 2019 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ge0rg3/593d318fef7f258a5ca94efe1c581206 to your computer and use it in GitHub Desktop.
Save Ge0rg3/593d318fef7f258a5ca94efe1c581206 to your computer and use it in GitHub Desktop.
Bruteforce 4 byte XOR encryption. Made for HMGCC's BLK_BOX challenge.
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