🟩The Last Dance🕺

First you MUST understand how chacha20 works before solving this challenge, you can check thisarrow-up-right or any other source

Source code

from Crypto.Cipher import ChaCha20
from secret import FLAG
import os


def encryptMessage(message, key, nonce):
    cipher = ChaCha20.new(key=key, nonce=iv)
    ciphertext = cipher.encrypt(message)
    return ciphertext


def writeData(data):
    with open("out.txt", "w") as f:
        f.write(data)


if __name__ == "__main__":
    message = b"Our counter agencies have intercepted your messages and a lot "
    message += b"of your agent's identities have been exposed. In a matter of "
    message += b"days all of them will be captured"

    key, iv = os.urandom(32), os.urandom(12)

    encrypted_message = encryptMessage(message, key, iv)
    encrypted_flag = encryptMessage(FLAG, key, iv)

    data = iv.hex() + "\n" + encrypted_message.hex() + "\n" + encrypted_flag.hex()
    writeData(data)

output 👇


Understanding the code

this is a chacha20 encrypting which is a stream key encrypting cipher and as you see the code there is a msg and a flag encrypted using the same initial key and vector which means that both flag and msg are encrypted using the same key stream, chacha20 works by XORing the key with the text so if we XORed the encrypted msg and flag we will get the plain msg xored with the plain flag, by xoring the result with the plain msg we cancel its bytes and get our original flag :D

msg ⊕ key-stream = enc-msg

flag ⊕ key stream = enc-flag

enc-msg ⊕ enc-flag = flag ⊕ msg

or in other words its:

msg ⊕ key-stream ⊕ flag ⊕ key stream = flag ⊕ msg

then

flag ⊕ msg ⊕ msg = flag


solution code

this will give you the flag HTB{und3r57AnD1n9_... That's it

Happy hacking

Last updated