picoctf/crypto/new_caesar/solve.py

37 lines
1.0 KiB
Python
Executable File

#!/usr/bin/env python3
import string
LOWERCASE_OFFSET = ord("a")
ALPHABET = string.ascii_lowercase[:16]
flag = "mlnklfnknljflfjljnjijjmmjkmljnjhmhjgjnjjjmmkjjmijhmkjhjpmkmkmljkjijnjpmhmjjgjj"
def b16_decode(enc):
dec = ""
for i in range(0, len(enc), 2):
first = "{0:04b}".format(ALPHABET.index(enc[i]))
second = "{0:04b}".format(ALPHABET.index(enc[i + 1]))
dec += chr(int(first + second, 2))
return dec
def unshift(c, k):
t1 = ord(c) - LOWERCASE_OFFSET
t2 = ord(k) - LOWERCASE_OFFSET
return ALPHABET[(t1 - t2) % len(ALPHABET)]
def main():
for key in ALPHABET:
unenc = ""
for i, c in enumerate(flag):
unenc += unshift(c, key[i % len(key)])
unshifted = ''.join(unshift(c, key) for c in flag)
# NOTE: key is 'g', "et tu?" is a caesar reference.
# Remove the if statement to see all outpus for all keys
if key == 'g':
print(f'picoCTF{{{b16_decode(unenc)}}}')
if __name__ == "__main__":
main()