We start by downloading the source files.
We are give a
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))
It seems like the source code is some kind of caesar cipher. As it turns out it is trithemius cipher, which is a variant of the caesar cipher. The trithemius cipher is a substitution cipher where each letter is shifted by a fixed number of positions. The shift is not constant, but depends on the position of the letter in the plaintext. The shift is calculated using a function that takes the position of the letter as input.
We can write a small script to decrypt the output:
from random import randint
output = "DJF_CTA_SWYH_NPDKK_MBZ_QPHTIGPMZY_KRZSQE?!_ZL_CN_PGLIMCU_YU_KJODME_RYGZXL"
def to_identity_map(a):
return ord(a) - 0x41
def from_identity_map(a):
return chr(a % 26 + 0x41)
def decrypt(c):
m = ''
for i in range(len(c)):
ch = c[i]
if not ch.isalpha():
m += ch
else:
chi = to_identity_map(ch)
m += from_identity_map(chi - i)
return m
print("HTB{" + decrypt(output) + "}")
Running this script gives us the flag.
HTB{DID_YOU_KNOW_ABOUT_THE_TRITHEMIUS_CIPHER?!_IT_IS_SIMILAR_TO_CAESAR_CIPHER}