Nouman Rahman


picoCTF 2022: Cryptography: basic-mod-2

picoCTF 2022: Cryptography: basic-mod-2

Nouman Rahman's photo
Nouman Rahman
·Oct 14, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article


Challenge: basic-mod-2

Category: Cryptography


A new modular challenge! Download the message here.

Take each number mod 41 and find the modular inverse for the result. Then map to the following character set: 1-26 are the alphabet, 27-36 are the decimal digits, and 37 is an underscore.

Wrap your decrypted message in the picoCTF flag format (i.e. picoCTF{decrypted_message})


This challenge is similar to the previous basic-mod-1 challenge that we completed earlier. In that challenge, we have been provided with a message that we have to decrypt by taking each number mod 37 and then mapping it to a specific character set. If you haven't completed that challenge be sure to complete that first to understand this one better.

Okay so our message looks like this:

104 85 69 354 344 50 149 65 187 420 77 127 385 318 133 72 206 236 206 83 342 206 370

The only change that we have to make to our previous script is that instead of taking each number mod 37. We have to take each number mod 41 and then we have to do modular inverse. If you don't know what modular inverse is, it is a mathematical concept that I don't know. So I searched for a solution and found out that we can do modular inverse in Python like this:

pow(number, -1, 41)

And in JavaScript, we have to write a custom function like this:

function modInverse(a, m) {
    for(let x = 1; x < m; x++)
        if (((a % m) * (x % m)) % m == 1)
            return x;

modInverse(number, 41)

So according to this, our finished solution will look like this: Python:

import string
characters = string.ascii_uppercase
characters += "0123456789_"

flag_enc = [104,85,69,354,344,50,149,65,187,420,77,127,385,318,133,72,206,236,206,83,342,206,370]
flag = ""
for char in flag_enc:
    mod = pow(char, -1, 41)
    flag += characters[mod - 1]

print('picoCTF{%s}' % flag)


const characterCodes = Array.from(Array(26)).map((e, i) => i + 65);
let characters = => String.fromCharCode(x));
characters = [...characters, ...Array.from(Array(10).keys()), "_"]

function modInverse(a, m) {
    for(let x = 1; x < m; x++)
        if (((a % m) * (x % m)) % m == 1)
            return x;

const flagEnc = [104,85,69,354,344,50,149,65,187,420,77,127,385,318,133,72,206,236,206,83,342,206,370]
let flag = ""
flagEnc.forEach(char => {
    const mod = modInverse(char, 41)
    flag += characters[mod - 1]

As you can see that besides that we are doing modular inverse we are subtracting 1 from that number because now our number doesn't have 0s.


In conclusion, we just did a few modifications to our code from the previous basic-mod-1 challenge.


Did you find this article valuable?

Support Nouman Rahman by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
Share this