Challenge Description
Let's study encode techniques in CTF Crypto!
We are given the challenge page, the python source code and the produced output values.
Solving
When we read the source code, we can see that the flag is split into three parts. After that, each part is encoded in a different format.
flag = os.getenv("FLAG", ...).encode()
flag1 = flag[:20]
flag2 = flag[20:40]
flag3 = flag[40:]
print(f"long_value = {bytes_to_long(flag1)}")
print(f'hex_string = "{flag2.hex()}"')
print(f'base64_string = "{base64.b64encode(flag3).decode()}"')
The first part is converted from bytes to a big integer with bytes_to_long. The second part is encoded as a hex string. The third part is encoded with base64.
The output file gives us these three values:
long_value = 373502670300504551747111047082539140193958649718
hex_string = "346c5f6833785f6630726d61745f31735f636c33"
base64_string = "NG5fYjY0X3A0ZGQxbmdfaXNfY29vbH0="
So we just need to reverse each encoding.
For the integer value, we use the reverse function of bytes_to_long, which is long_to_bytes.
For the second part, we decode the hex string back to bytes and then to text.
For the last part, we use base64 decode and convert the result to ascii text.
import base64
from Crypto.Util.number import *
long_value = 373502670300504551747111047082539140193958649718
hex_string = "346c5f6833785f6630726d61745f31735f636c33"
base64_string = "NG5fYjY0X3A0ZGQxbmdfaXNfY29vbH0="
part1 = long_to_bytes(long_value).decode()
part2 = bytes.fromhex(hex_string).decode()
part3 = base64.b64decode(base64_string).decode()
print(part1 + part2 + part3)
After decoding every part and concatenating them, we get the full flag.
Flag
Alpaca{b1g_1nt3ger_v4l_h3x_f0rmat_1s_cl34n_b64_p4dd1ng_is_cool}