Skip to content

Instantly share code, notes, and snippets.

@EvelynSubarrow
Created October 26, 2019 02:28
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 EvelynSubarrow/ed4c989ac3be8a5f5bf7ddb569acc44a to your computer and use it in GitHub Desktop.
Save EvelynSubarrow/ed4c989ac3be8a5f5bf7ddb569acc44a to your computer and use it in GitHub Desktop.
SquareCTF challenge 3 ("Decode me") solution
#!/usr/bin/env python3
import base64, string, sys
B64 = string.ascii_uppercase + string.ascii_lowercase + string.digits + '+/='
B64 = [ord(c) for c in B64]
def decode(inp):
inp += b"\x00"
bag = []
buf = []
out = []
for x in inp:
if x==0:
for b in bag:
diff_count = buf.count(b)
ch = b+diff_count+1 - 100
while ch not in B64:
ch += 100
out.append(chr(ch))
bag, buf = [],[]
elif x in bag:
buf.append(x)
else:
bag.append(x)
return "".join(out)
if __name__ == '__main__':
with open(sys.argv[1], 'rb') as f:
print(decode(f.read()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment