crypto/new_caesar

This commit is contained in:
Oystein Kristoffer Tveit 2024-09-06 20:07:32 +02:00
parent 66957e77d8
commit 82dcc622d9
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
3 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1 @@
mlnklfnknljflfjljnjijjmmjkmljnjhmhjgjnjjjmmkjjmijhmkjhjpmkmkmljkjijnjpmhmjjgjj

View File

@ -0,0 +1,28 @@
import string
LOWERCASE_OFFSET = ord("a")
ALPHABET = string.ascii_lowercase[:16]
def b16_encode(plain):
enc = ""
for c in plain:
binary = "{0:08b}".format(ord(c))
enc += ALPHABET[int(binary[:4], 2)]
enc += ALPHABET[int(binary[4:], 2)]
return enc
def shift(c, k):
t1 = ord(c) - LOWERCASE_OFFSET
t2 = ord(k) - LOWERCASE_OFFSET
return ALPHABET[(t1 + t2) % len(ALPHABET)]
flag = "redacted"
key = "redacted"
assert all([k in ALPHABET for k in key])
assert len(key) == 1
b16 = b16_encode(flag)
enc = ""
for i, c in enumerate(b16):
enc += shift(c, key[i % len(key)])
print(enc)

37
crypto/new_caesar/solve.py Executable file
View File

@ -0,0 +1,37 @@
#!/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()