basic-mod1

Published: July 20, 2023

Description

A list of integers is provided along with instructions to take each number mod 37 and map it onto a custom alphabet (A-Z, digits, underscore). Implement the mapping to decode the hidden flag.

Grab the message file and convert the space-separated numbers into a Python list.

Apply modulo 37 to each entry, then treat 0-25 as A-Z, 26-35 as digits, and 36 as an underscore.

Concatenate the recovered characters and wrap them with `picoCTF{...}`.

wget https://artifacts.picoctf.net/c/128/message.txt
cat message.txt
cat message.txt | sed -e "s/^/[/" | sed 's/ *$//' | sed -e "s/$/]/" | sed -e "s/ /, /g"
python3 mod1.py

Solution

  1. Step 1Normalize the input
    Transform the message into a Python array (a quick sed oneliner wraps it in brackets and adds commas). This makes iterating and applying modulo arithmetic trivial.
    Learn more

    sed (stream editor) is a Unix utility that applies text transformations line-by-line without opening an interactive editor. The pipeline here chains four separate sed calls: the first prepends [ to the line, the third appends ], and the fourth replaces every space with , - turning raw space-separated numbers into a valid Python list literal in one shot.

    This pattern of "shell preprocessing then Python logic" is a common CTF workflow. Shell tools like sed, awk, and cut are excellent for quick structural transformations, while Python handles the numeric or cryptographic work. Keeping the two concerns separate makes each step easy to test independently.

    In practice, you could also just read the file directly with open() in Python and call .split() - but the sed trick is a useful pattern to know when you need to pipe data quickly between tools.

  2. Step 2Map each value
    For every number `n`, compute `n % 37` to keep it inside the alphabet range, then index into the character set `ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_`.
    Learn more

    Modular arithmetic reduces a number to the remainder after dividing by a modulus. Here the modulus is 37 because the custom alphabet has exactly 37 characters (26 letters + 10 digits + 1 underscore). Any integer, no matter how large, maps to a value in 0..36 after % 37.

    Custom alphabets like this are the foundation of many classical ciphers and encoding schemes. The design choice of 37 characters is deliberate: it is prime, which means every non-zero value has a multiplicative inverse modulo 37 - a property exploited in the follow-up challenge basic-mod2.

    In Python, the modulo operator % always returns a non-negative result when the divisor is positive, so you don't need to worry about negative indices. A single list comprehension - [alphabet[n % 37] for n in numbers]- is all that's needed.

  3. Step 3Assemble the flag
    Build a string starting with `picoCTF{...}` to produce the final answer.
    Learn more

    Once every integer is decoded into its character, joining the list with ''.join(chars) and wrapping it in the standard picoCTF{} format gives the submission-ready flag. This final assembly step is trivial but teaches an important habit: always verify the structure of your output matches what the challenge expects before submitting.

    The picoCTF{} wrapper is a flag format convention. Most CTF platforms use a consistent prefix (like flag{}, CTF{}, or competition-specific variants) so automated checkers can validate submissions. Recognizing these patterns helps you quickly confirm you decoded something correctly even before you know what the inner text means.

Flag

picoCTF{R0UND_N_R0UND_B6B...}

Simple modular arithmetic plus a custom alphabet turns the numeric sequence back into the plaintext flag.

Want more picoCTF 2022 writeups?

Useful tools for Cryptography

Related reading

What to try next