Berserker's Brain Teaser
Posted on Mon 20 March 2023 in ctf
This is a writeup of the Berserker's Brain Teaser challenge which was part of the Crypto category during vikeCTF.
The attached python script BerserkerBrainTeaserEncrypt.py
that was used to encrypt the flag had the following contents:
import random
alphabet = "abcdefghijklmnopqrstuvwxyz"
plaintext = input("text to encrypt:\n")
ciphertext = ""
key = [random.randint(1, len(alphabet)) for _ in range(len("vikeCTF"))]
print("key:", *key)
for i, c in enumerate(plaintext):
if not c.isalpha():
ciphertext += c
continue
offset = alphabet.find(c.lower())
rotation = key[i % len(key)]
result = alphabet[(offset + rotation) % len(alphabet)]
if c.islower():
ciphertext += result
else:
ciphertext += result.upper()
print("ciphertext:")
print(ciphertext)
Since we know the encrypted text as wenn as the flag prefix it was easy to first rebuild the encryption key:
alphabet = "abcdefghijklmnopqrstuvwxyz"
cipher = "zexqSNE{cVaLuM_xRxBuRs_vE_mTtAe_ToOiN_oEiK}"
cipher_prefix = "zexqSNE"
flag_prefix = "vikeCTF"
key = []
for i, c in enumerate(flag_prefix):
offset = alphabet.find(c.lower())
rotation = alphabet.find(cipher_prefix[i].lower()) - offset + 26
key.append(rotation)
print(f"key: {key}")
With that key we can decrypt the flag:
# Decrypt flag with previously generated key:
flag = ""
for i, c in enumerate(cipher):
if not c.isalpha():
flag += c
continue
offset = alphabet.find(c.lower())
rotation = key[i % len(key)]
result = alphabet[(offset - rotation) % len(alphabet)]
if c.islower():
flag += result
else:
flag += result.upper()
print(f"flag: {flag}")
This gave: vikeCTF{gIoVaN_bElLaSo_iS_sUpEr_DuPeR_cOoL}