Dynastic

Personal Rating: Easy

We have this encoder with a flag:

from secret import FLAG
from random import randint

def to_identity_map(a):
    return ord(a) - 0x41

def from_identity_map(a):
    return chr(a % 26 + 0x41)

def encrypt(m):
    c = ''
    for i in range(len(m)):
        ch = m[i]
        if not ch.isalpha():
            ech = ch
        else:
            chi = to_identity_map(ch)
            ech = from_identity_map(chi + i)
        c += ech
    return c

with open('output.txt', 'w') as f:
    f.write('Make sure you wrap the decrypted text with the HTB flag format :-]\n')
    f.write(encrypt(FLAG))

# DJF_CTA_SWYH_NPDKK_MBZ_QPHTIGPMZY_KRZSQE?!_ZL_CN_PGLIMCU_YU_KJODME_RYGZXL

I edited the source script so that I could better understand what it does:

from random import randint
FLAG = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

def encrypt(msg):
    c = ''
    for i in range(len(msg)):     # for each char in message
        char = msg[i]
        if not char.isalpha():    # only encrypt alphabetic chars
            encchar = char
        else:
            chi = ord(char) - 0x41             # chi = unicode int of char
            encchar = chr((chi+i) % 26 + 0x41) # ech = (chi+i) % 26 + 0x41
        c += encchar                           # add encrypted char ech to result c
    return c

print(encrypt(FLAG))

# DJF_CTA_SWYH_NPDKK_MBZ_QPHTIGPMZY_KRZSQE?!_ZL_CN_PGLIMCU_YU_KJODME_RYGZXL
# The following encryption result happens when I give 52*"A" as FLAG:

# AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
# ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ

Knowing that each char of the message becomes the according char in the alphabet + index, you can write a script so that each char of the cipher becomes the according char in the alphabet - index. For a message longer than the alphabet you can just set a second alphabet set that repeats the alphabet. This was my decrpytor:

cipher = "DJF_CTA_SWYH_NPDKK_MBZ_QPHTIGPMZY_KRZSQE?!_ZL_CN_PGLIMCU_YU_KJODME_RYGZXL"
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
alphabet2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"
final = ''

for i in range(len(cipher)):
    char = cipher[i]
    if not char.isalpha():
        final += char
    else:
        index = alphabet.find(char)
        resultchar = alphabet2[index-i]
        final += resultchar
print(final)

HTB{DID_YOU_KNOW_ABOUT_THE_TRITHEMIUS_CIPHER?!_IT_IS_SIMILAR_TO_CAESAR_CIPHER}

Last updated