Tags: crypto 

Rating:

![image](https://user-images.githubusercontent.com/68913871/134805137-ad9c4da2-d324-4146-b572-a6602a9e5337.png)

> This is the contents of the .sage file which shows the encryption algorithm.

```python
def encrypt(msg, f):
return ''.join(chr(f.substitute(c)) for c in msg)

P.<x> = PolynomialRing(ZZ)
f = 13*x^2 + 3*x + 7

FLAG = open('./flag.txt', 'rb').read().strip()

enc = encrypt(FLAG, f)
print(enc)

```

> This is the contents of output.txt file which is the ciphertext that needs to be decrypted to give us the flag.

```
??玲???疗???䶹?蒵???蒵???疗??窇蒵?
```

> With the keywords `sage` and `PolynomialRing`, I stumbled across the [documentation](https://doc.sagemath.org/html/en/tutorial/tour_polynomial.html) as well as an [online interface](https://sagecell.sagemath.org/) where I can test some codes.

> We will adopt a bruteforce approach. Encrypt all printable ascii characters with the given encryption algorithm and store them in a list, then compare them with the ciphertext.

```python
import string
printable_list = string.printable

def encrypt(msg, f):
return ''.join(chr(f.substitute(c)) for c in msg)

P.<x> = PolynomialRing(ZZ)
f = 13*x^2 + 3*x + 7

enc = encrypt(bytes(printable_list, "ascii"), f)

code_store = list(enc)

ct = "??玲???疗???䶹?蒵???蒵???疗??窇蒵?"

for ch in ct:
if ch in code_store:
print(printable_list[code_store.index(ch)],end='')
else:
print("Non-printable character found!")
```

> Entering the above code in the [online interface](https://sagecell.sagemath.org/) will yield the flag.

`DUCTF{sh0uld'v3_us3d_r0t_13}`

Original writeup (https://github.com/Rookie441/CTF/blob/main/Storage/Writeups/DownUnderCTF2021_Writeup.md#substitution-cipher-i).