本文为看雪论坛优秀文章
看雪论坛作者ID:狗敦子
from sage.all import *
import json
from Crypto.Hash import SHA256
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
p = 12143520799543738643
def read_matrix(file_name):
'r').read().strip() data = open(file_name,
eval(row)) for row in data.splitlines()] rows = [list(
return Matrix(GF(p), rows)
'matrix.txt') D = read_matrix(
'public_key.txt') P = read_matrix(
digD ,A = D.diagonalization()
digP = A.inverse() * P * A
for i in range(12)] vD = [digD[i][i]
for i in range(12)] vP = [digP[i][i]
print(f"vD = {vD}")
vD = [37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2]
print(f"vP = {vP}")
vP = [6751925379844785295, 11256715989719283883, 4551561838026472495, 11383130904596697638, 8534299476177021992, 11184828239802784209, 7103104085280766875, 1622643043767580331, 11104789109564474465, 1502559189506368871, 522368022672629021, 1590703325067650792]
G = GF(p)
K = discrete_log(G(vP[11]), G(vD[11]))
print(f"K = {K}")
K = 7619698002081645976
# now we can decipher
key = SHA256.new(data=str(K).encode()).digest()[:2**8]
with open("encrypted_flag.txt", "r") as ff:
data_dict = json.load(ff)
iv = bytes.fromhex(data_dict["iv"])
ciphertext = bytes.fromhex(data_dict["ciphertext"])
cipher = AES.new(key, AES.MODE_CBC, iv)
flag = cipher.decrypt(ciphertext).decode()[:46]
print(flag)
CyberErudites{Di4g0n4l1zabl3_M4tric3s_d4_b3st}
from sage.all import Matrix, IntegerModRing
from message import N, e, ct
def poly(num):
return [(3*pow(num, 3, N)) % N, (3*pow(num, 6, N)) % N]
def v2polyv(v, num):
return (v - R_3 - pow(num, 9, N)) % N
def polyv2v(v, num):
return (v + R_3 + pow(num, 9, N)) % N
def gen(num):
V2 = Matrix(IntegerModRing(N), [poly(num)])
V1 = V2 * T_
v = (poly_C*V1[0][0] + poly_y*V1[0][1]) % N
table[polyv2v(v, num)] = chr(num)
table = {}
R_3 = ct[0]
prefix = b"CyberErudites{}"
T = Matrix(IntegerModRing(N), [poly(int(prefix[0])), poly(int(prefix[1]))])
T_ = T.inverse()
# print(T_)
poly_C = v2polyv(ct[1], ord('C'))
poly_y = v2polyv(ct[2], ord('y'))
for num in range(32, 126):
gen(num)
print("".join([table[v] for v in ct[1:]]))
# CyberErudites{Fr4nkl1n_W3_n33d_an0th3R_S3450N_A54P}
看雪ID:狗敦子
https://bbs.pediy.com/user-home-962418.htm
看雪2022KCTF秋季赛官网:https://ctf.pediy.com/game-team_list-18-29.htm
# 往期推荐
球分享
球点赞
球在看
点击“阅读原文”,了解更多!
原文始发于微信公众号(看雪学苑):GDG Algiers CTF两道矩阵题wp