# ProgrammingFire 🚀 Blog # picoCTF 2022: Cryptography: basic-mod-2

## Introduction

Challenge: basic-mod-2

Category: Cryptography

Description:

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}`)

## Solution

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)
``````

JavaScript:

``````const characterCodes = Array.from(Array(26)).map((e, i) => i + 65);
let characters = characterCodes.map((x) => 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]
})
console.log(`picoCTF{\${flag}}`)
``````

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.

## Conclusion

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

Flag: `picoCTF{1NV3R53LY_H4RD_DADAACAA}`