SECCON Beginners 2021 | GFM

#secconbeginners2021

FLAG = b'<censored>'

SIZE = 8
p = random_prime(2^128)
MS = MatrixSpace(GF(p), SIZE)

key = MS.random_element()
while key.rank() != SIZE:
    key = MS.random_element()

M = copy(MS.zero())
for i in range(SIZE):
    for j in range(SIZE):
        n = i * SIZE + j
        if n < len(FLAG):
            M[i, j] = FLAG[n]
        else:
            M[i, j] = GF(p).random_element()

enc = key * M * key

print('p:', p)
print('key:', key)
print('enc:', enc)

MatrixSpaceの問題だが k * M * k kが与えられているので k^{-1} * enc * k^{-1} するだけ

import ast
with open("output.txt") as f:
  p = ast.literal_eval(f.readline().strip())
  key = ast.literal_eval(f.readline().strip())
  enc = ast.literal_eval(f.readline().strip())


SIZE = 8
MS = MatrixSpace(GF(p), SIZE)

key = MS(key)
enc = MS(enc)

M = key^(-1)  * enc * key^(-1)

flag = 0
for i in range(SIZE):
    for j in range(SIZE):
        n = i * SIZE + j
        if M[i,j] < 128:
          flag = flag*256 + int(M[i,j])

print(int(flag).to_bytes(100, "big"))