Skip to content

How The Columns Have Turned

import os


with open('super_secret_messages.txt', 'r') as f:
    SUPER_SECRET_MESSAGES = [msg.strip() for msg in f.readlines()]


def deriveKey(key):
    derived_key = []

    for i, char in enumerate(key):
        previous_letters = key[:i]
        new_number = 1
        for j, previous_char in enumerate(previous_letters):
            if previous_char > char:
                derived_key[j] += 1
            else:
                new_number += 1
        derived_key.append(new_number)
    return derived_key


def transpose(array):
    return [row for row in map(list, zip(*array))]


def flatten(array):
    return "".join([i for sub in array for i in sub])


def twistedColumnarEncrypt(pt, key):
    derived_key = deriveKey(key)

    width = len(key)

    blocks = [pt[i:i + width] for i in range(0, len(pt), width)]
    blocks = transpose(blocks)

    ct = [blocks[derived_key.index(i + 1)][::-1] for i in range(width)]
    ct = flatten(ct)
    return ct


class PRNG:
    def __init__(self, seed):
        self.p = 0x2ea250216d705
        self.a = self.p
        self.b = int.from_bytes(os.urandom(16), 'big')
        self.rn = seed

    def next(self):
        self.rn = ((self.a * self.rn) + self.b) % self.p
        return self.rn


def main():
    seed = int.from_bytes(os.urandom(16), 'big')
    rng = PRNG(seed)

    cts = ""

    for message in SUPER_SECRET_MESSAGES:
        key = str(rng.next())
        ct = twistedColumnarEncrypt(message, key)
        cts += ct + "\n"

    with open('encrypted_messages.txt', 'w') as f:
        f.write(cts)

    dialog = "Miyuki says:\n"
    dialog += "Klaus it's your time to sign!\n"
    dialog += "All we have is the last key of this wierd encryption scheme.\n"
    dialog += "Please do your magic, we need to gather more information if we want to defeat Draeger.\n"
    dialog += f"The key is: {str(key)}\n"

    with open('dialog.txt', 'w') as f:
        f.write(dialog)


if __name__ == '__main__':
    main()
Miyuki says:
Klaus it's your time to sign!
All we have is the last key of this wierd encryption scheme.
Please do your magic, we need to gather more information if we want to defeat Draeger.
The key is: 729513912306026
VEOAOTNRDCEEIFHIVHMVOETYDEDTESTHTCHLSRPDAIYAATOSTEGIIIOCIPYLTNOTLRTRNLEEUNBEOSFNANDHTUFTEETREEEEOEDHNRNYA
AAVPDESEETURAFFDUCEDAEECNEMOROCEANHPTTGROITCYSSSETTSKTTRLRIUAVSONOISECNJISAFAATAPATWORIRCETYUIPUEEHHAIHOG
NABPSVKELHRIALDVEHLORCNNOERUNGTAEEEHEHDORLIEEAOTITUTEAUEARTEFISESGTAYAGBTHCEOTWLSNTWECESHHBEIOYPNCOLICCAF
NIRYHFTOSSNPECMPNWSHFSNUTCAGOOAOTGOITRAEREPEEPWLHIPTAPEOOHPNSKTSAATETTPSIUIUOORSLEOAITEDFNDUHSNHENSESNADR
NUTFAUPNKROEOGNONSRSUWFAFDDSAAEDAIFAYNEGYSGIMMYTAANSUOEMROTRROUIIOODLOIRERVTAMNITAHIDNHRENIFTCTNLYLOTFINE
flag.py
def deriveKey(key):
    derived_key = []

    for i, char in enumerate(key):
        previous_letters = key[:i]
        new_number = 1
        for j, previous_char in enumerate(previous_letters):
            if previous_char > char:
                derived_key[j] += 1
            else:
                new_number += 1
        derived_key.append(new_number)
    return derived_key

cts = []
cts.append("VEOAOTNRDCEEIFHIVHMVOETYDEDTESTHTCHLSRPDAIYAATOSTEGIIIOCIPYLTNOTLRTRNLEEUNBEOSFNANDHTUFTEETREEEEOEDHNRNYA")
cts.append("AAVPDESEETURAFFDUCEDAEECNEMOROCEANHPTTGROITCYSSSETTSKTTRLRIUAVSONOISECNJISAFAATAPATWORIRCETYUIPUEEHHAIHOG")
cts.append("NABPSVKELHRIALDVEHLORCNNOERUNGTAEEEHEHDORLIEEAOTITUTEAUEARTEFISESGTAYAGBTHCEOTWLSNTWECESHHBEIOYPNCOLICCAF")
cts.append("NIRYHFTOSSNPECMPNWSHFSNUTCAGOOAOTGOITRAEREPEEPWLHIPTAPEOOHPNSKTSAATETTPSIUIUOORSLEOAITEDFNDUHSNHENSESNADR")
cts.append("NUTFAUPNKROEOGNONSRSUWFAFDDSAAEDAIFAYNEGYSGIMMYTAANSUOEMROTRROUIIOODLOIRERVTAMNITAHIDNHRENIFTCTNLYLOTFINE")

blocks = [list(i[j:j+7])[::-1] for i in cts for j in range(0,len(i),7)]
# print(blocks)

key = '729513912306026'
derived_key = deriveKey(key)
print(derived_key)

rearange = [derived_key.index(i + 1) for i in range(len(key))]
print(rearange)

arr_new = []

for i in range(0,len(blocks),len(key)):
    for j in range(len(key)):
        arr_new.append(blocks[rearange.index(j)+i])

print(arr_new)

for i in range(7):
    for j in arr_new:
        print(j[i],end='')

# THELOCATIONOFTH THECARGOISSAFEW BECAREFULSKOLIW DONTFORGETTOCHA IFYOUMESSUPAGAI ECONVOYDANTEISD ENEEDTOMOVEFAST HENYOUARRIVEATT NGETHEDARKFUELO NILLSENDYOUTOTH ETERMINEDTOBEON CAUSETHERADARSA HEPALACEOFSCION FTHESPACESHIPWE EANDROIDGRAVEYA THETHIRDPLANETA REPICKINGUPSUSP SAYTHECODEPHRAS DONTWANTANYUNPL RDTOSUFFERFROMT FTERVINYRYOUCAN ICIOUSACTIVITYA ETOGETINHTBTHEL EASANTSURPRISES HECONSTANTTERMI USELIGHTSPEEDAF ROUNDTHETRAJECT CGISVULNERABLEW TOHAPPENTHISSER NATIONOFYOURKIN TERTHEDELIVERYS ORYOFTHEPLANETA ENEEDTOCHANGEIT IOUSMISSIONPOPO DAFINALWARNINGM
# solved with the help of a column turned miracle :)
# THELOCATIONOFTH THECARGOISSAFEW BECAREFULSKOLIW DONTFORGETTOCHA IFYOUMESSUPAGAI 
# ECONVOYDANTEISD ENEEDTOMOVEFAST HENYOUARRIVEATT NGETHEDARKFUELO NILLSENDYOUTOTH 
# ETERMINEDTOBEON CAUSETHERADARSA HEPALACEOFSCION FTHESPACESHIPWE EANDROIDGRAVEYA 
# THETHIRDPLANETA REPICKINGUPSUSP SAYTHECODEPHRAS DONTWANTANYUNPL RDTOSUFFERFROMT 
# FTERVINYRYOUCAN ICIOUSACTIVITYA ETOGETINHTBTHEL EASANTSURPRISES HECONSTANTTERMI 
# USELIGHTSPEEDAF ROUNDTHETRAJECT CGISVULNERABLEW TOHAPPENTHISSER NATIONOFYOURKIN 
# TERTHEDELIVERYS ORYOFTHEPLANETA ENEEDTOCHANGEIT IOUSMISSIONPOPO DAFINALWARNINGM

# HTB{THELCGISVULNERABLEWENEEDTOCHANGEIT}

flag HTB{THELCGISVULNERABLEWENEEDTOCHANGEIT}