Rating:

From the task.py we can infer that the input SECRET is a magic square with size N * N. The numbers range from 0 to n^2 - 1. It's known that the key is the sum of a single line or column or diagnal line.

There is the formula,

Key = 1/2 * N * (N^2 + 1) - N

And we have,

Key % N^2 = canary

--> 1/2 * N * (N^2 + 1) - N = K * N^2 + canary

--> N * (N^2 - 2 * K * N - 1) = 2 * canary

Thus we can factor canary and bruteforce N. Once N is obtained, it's trivial to get the flag.

solver.sage
```
flag = 'd9a103a6006bfba17074ef571011d8eededdf851b355bdc4795616744066433695b9e9201f6deff7577c03ba690d4d517bdaae'.decode('hex')
c = 5405053190768240950975482839552589374748349681382030872360550121041249100085609471

p = factor(2*c)
print(p)
perm = []

for item in p:
for i in range(item[1]):
perm.append(item[0])

import itertools

nn = -1
found = False
for k in range(1, len(perm) + 1):
if found:
break
for item in itertools.combinations(perm, k):
n = 1
for i in item:
n *= i
if (1/2*n*(n**2 + 1) - n) % (n**2) == c:
print(n)
nn = n
found = True
break

n = nn
key = 1/2*n*(n**2 + 1) - n
from Crypto.Util.number import *

ct = bytes_to_long(flag)
pt = int(key)^^int(ct)
print(['ct', ct])
print(['pt', pt])
print(['key', key])
print([long_to_bytes(pt)])

'''
2 * 13 * 523 * 3989 * 147419 * 1123483 * 59798254331 * 7868139393451 * 5637813537472069 * 453627807529130112554537
103971661434914474977947909929808181577819
/root/SageMath/local/lib/python2.7/site-packages/Crypto/Util/number.py:57: PowmInsecureWarning: Not using mpz_powm_sec. You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.
_warn("Not using mpz_powm_sec. You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.", PowmInsecureWarning)
['ct', 561972360388260343482822369021527458970356332082340547963450726508217043864533299999610296030451095550074144621116631276206L]
['pt', 599945086801911130806978817832163187667846140255274012288181028311543602861388968346504555978579832984534282L]
['key', 561972360388259748411992161175283140457835075730544044903466383696200377007240232937465352098303479813588180518349115533220]
['Aero{m4g1c_squ4r3_1s_just_4n_4nc13nt_puzzl3}\n']
'''

```