Post

Cipher's Secret Message

Sharpen your cryptography skills by analyzing code to get the flag.

Cipher's Secret Message

Introduction

Let’s read the content Desktop View As you can see, we have to decrypt that to retrieve the flag

Desktop View We can see the whole code here, we have to modifiy the code to get the flag

1
2
3
4
5
6
7
8
9
10
11
from secret import FLAG

def enc(plaintext):
    return "".join(
        chr((ord(c) - (base := ord('A') if c.isupper() else ord('a')) + i) % 26 + base) 
        if c.isalpha() else c
        for i, c in enumerate(plaintext)
    )

with open("message.txt", "w") as f:
    f.write(enc(FLAG))
  1. enc() applies a Caesar cipher with incremental shift (+i)
  2. . + i adds the character’s index, creating a progressive shift per character
  3. chr(…) converts the resulting number back into a ciphered character.

Once we know what does the code do. we can procede to modify it Desktop View As you can see I just edit the variable and the hypertext to ciphertext. but when I tried to execute it, there is an error,

Desktop View

But the problem here is cause, I just forgot to put the variable decrypt next to def, we can modify it just adding decrypt Desktop View

1
2
3
4
5
6
7
8
9
10
message = "a_up4qr_kaiaf0_bujktaz_qm_su4ux_cpbq_ETZ_rhrudm"

def decrypt(cyphertext):
    return "".join(
        chr((ord(c) - (base := ord('A') if c.isupper() else ord('a')) + i) % 26 + base) 
        if c.isalpha() else c
        for i, c in enumerate(cyphertext)
    )

print("THM{" + decrypt(message) + "}")

I added the message variable manually with the encrypted string provided in the challenge.

I created a new function called decrypt() (instead of enc()) and changed the logic:

I replaced + i with - i to reverse the Caesar incremental shift.

This essentially undoes the encryption applied originally in enc().

Finally, I used print("THM{" + decrypt(message) + "}") to wrap the output in the expected flag format.

Desktop View AND HERE IS THE FLAG

YOU HAVE TO KNOW THAT IS THERE IS A GRAMATICAL ERROR IS CAUSE IM LEARNING ENGLISH, THANK YOU

This post is licensed under CC BY 4.0 by the author.

Trending Tags