Created
October 26, 2019 02:28
-
-
Save EvelynSubarrow/ed4c989ac3be8a5f5bf7ddb569acc44a to your computer and use it in GitHub Desktop.
SquareCTF challenge 3 ("Decode me") solution
This file contains 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
#!/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