330 words
2 minutes
TCTT25 Crypto: NewBase64

NewBase64 [200pts] - Cryptography Write-up#

โจทย์#

นี่มันคือการเข้ารหัส Base64 แบบใหม่ใช่หรือไม่

หมายเหตุ รูปแบบของ Flag ที่เป็นคำตอบของข้อนี้คือ flag{message_10digits}


ดาวน์โหลดไฟล์#

ไฟล์ดาวน์โหลด
base64.py📥 Download
enc.txt📥 Download
solved.py📥 Download
All Files📦 GitHub Folder

ไฟล์โจทย์#

base64.py#

def char_gen():
char = [chr(i) for i in range(ord('ก'), ord('ก') + 47)]
char += [chr(i) for i in range(ord('๐'), ord('๐') + 10)]
char += [chr(i) for i in range(ord('0'), ord('0') + 10)]
char = char[:64]
return char
def char_to_hex(msg):
hex_data = ''
for c in msg.encode():
hex_data += hex(c)[2:]
return hex_data
def base64_encode(msg):
char = char_gen()
msg = char_to_hex(msg)
msg_num = eval('0x' + msg)
base64 = ''
while msg_num > 0:
base64 = char[msg_num % 64] + base64
msg_num = msg_num // 64
base64 += '=='
return base64
msg = input('Message: ')
base64 = base64_encode(msg)
open('enc.txt', 'wb').write(base64.encode('utf-16'))

enc.txt#

ฒ๗4๐บฃขหผ๗ฉยฉจฒษปธญมปธญมพงยฦฉงบอนทฝมบว4๓ฉจฦ๐พณขวผงฆศฟ๗ฝ๑ญงฒภปฤขรธ๘ฒษญธฎภญธฎภพงม๔ธ๗๙๔พ๖4ฃฑจฎฦฎฤฒภฎฤฑ๑ญคก๗ฎคก๑ญจ๕มฉณฅยฉ๓ญฤ==

ข้อสังเกต#

  • ตัวอักษรที่ใช้มี 64 ตัว
  • กระบวนการเข้ารหัส:
    1. แปลงข้อความเป็น Hex
    2. แปลง Hex เป็น Integer
    3. แปลง Integer เป็น Custom Base64 ด้วย Alphabet ข้างต้น
    4. เติม ”==” ท้ายข้อความ และบันทึกเป็น UTF-16

แนวคิดการแก้โจทย์#

  1. ไฟล์ enc.txt เป็นแบบ UTF-16
  2. ตัด ”==” ทิ้ง
  3. แปลงจาก Custom Base64 กลับไปเป็น Integer
  4. แปลง Integer กลับไปเป็น Hex String
  5. แปลง Hex String กลับไปเป็น Bytes และ Decode
  6. ได้ข้อความที่มี flag อยู่ภายใน

อธิบาย Script#

  • make_alphabet() : สร้างชุดอักษร Custom Base64 64 ตัว
  • custom64_to_int(s) : แปลงสตริงเข้ารหัสเป็นจำนวนเต็มฐาน 10
  • decode_custom_base64(enc_text) :
    1. ตัด ”==”
    2. แปลงสตริงเป็นจำนวนเต็ม
    3. แปลงเป็นเลขฐาน 16
    4. ถ้าความยาว hex เป็นเลขคี่ และเติม 0 ด้านหน้า
    5. ใช้ bytes.fromhex() เพื่อแปลงกลับเป็นข้อความ UTF-8
  • อ่านไฟล์ enc.txt โดยตรง แล้วพิมพ์ผลลัพธ์ออกมา

solved.py#

def make_alphabet():
chars = [chr(i) for i in range(ord('ก'), ord('ก') + 47)] # ก... (47 ตัว)
chars += [chr(i) for i in range(ord('๐'), ord('๐') + 10)] # ๐-๙ (10 ตัว)
chars += [chr(i) for i in range(ord('0'), ord('0') + 10)] # 0-9 (10 ตัว)
return chars[:64]
ALPHABET = make_alphabet()
ALPH2VAL = {c: i for i, c in enumerate(ALPHABET)}
def custom64_to_int(s):
value = 0
for ch in s:
if ch not in ALPH2VAL:
raise ValueError(f"Alphabet NOT FOUND IN: {ch!r}")
value = value * 64 + ALPH2VAL[ch]
return value
def decode_custom_base64(enc_text):
if enc_text.endswith("=="):
enc_text = enc_text[:-2]
n = custom64_to_int(enc_text)
hex_str = format(n, "x")
if len(hex_str) % 2 == 1:
hex_str = "0" + hex_str
data = bytes.fromhex(hex_str)
return data.decode("utf-8", errors="replace")
if __name__ == "__main__":
import pathlib
enc_path = pathlib.Path("enc.txt")
enc_text = enc_path.read_text(encoding="utf-16").strip()
result = decode_custom_base64(enc_text)
print(result)

ผลลัพธ์#

เมื่อรัน Python Script และใช้ข้อมูลตามไฟล์ enc.txt จะได้ข้อความ:

Good job! this is the flag for you flag{g00d_j0b_th1s_1s_th3_n3w_B@se64_6400064000} !!!###

Run Result

ดังนั้น Flag คือ:

flag{g00d_j0b_th1s_1s_th3_n3w_B@se64_6400064000}

Credits#

Writeup by netw0rk7 | Original Repo

TCTT25 Crypto: NewBase64
https://blog.lukkid.dev/posts/tctt25-crypto-newbase64/
Author
LUKKID
Published at
2025-12-13
License
CC BY-NC-SA 4.0