#task from sage.allimport * from Crypto.Util.number import * from os import urandom from secret import flag
n = 16 bound = 2^15
A = [ZZ.random_element(-bound, bound) for _ inrange(n*n)] A = Matrix(ZZ, n, n, A) B = [ZZ.random_element(-bound, bound) for _ inrange(n*n)] B = Matrix(ZZ, n, n, B)
res = [] for i inrange(5): bound = 2^15 S = [ZZ.random_element(-bound, bound) for _ inrange(n*n)] S = Matrix(ZZ, n, n, S) tmp = [] for i inrange(0, 60): S = S* A + B bound = 2^(int(S[0, 0]).bit_length()) if i % 3 == 2: tmp.append(Matrix(ZZ,n,n,[ZZ.random_element(-bound, bound) for _ inrange(n*n)])) continue tmp.append(S) res.append(tmp) e = A.LLL().determinant()
p = getPrime(512) q = getPrime(512) n = p * q m = bytes_to_long(urandom(int(n).bit_length() // 8 - len(flag) - 1) + flag) c = pow(m, e, n) h1 = pow(p+q, e, n) h2 = pow(p-q, e, n)
f = open('双人成行.txt', 'w') f.writelines(str(res)+'\n') f.writelines(str(n)+'\n') f.writelines(str(c)+'\n') f.writelines(str(h1)+'\n') f.writelines(str(h2)+'\n') f.close()
读取数据:
替换了一下'双人成行.txt'里的逗号,换成\n,然后:
1 2 3 4 5 6 7 8 9 10
f = open('双人成行.txt', 'r') res = [] for a inrange(300): tmp = [] for aa inrange(16): line = f.readline().replace('[', '').replace(']', '') line = [int(i) for i in line.split()] tmp.append(line) res.append(tmp) f.close()
读代码发现第三组矩阵被替换成了随机值,所以选取第1,2,4,5组矩阵,运算得到A。
\(S_2=S_1*A+B\)
\(S_5=S_4*A+B\)
\(A = (S_4-S_1)^{-1}*(S_5-S_2)\)
1 2 3 4 5 6
s1 = Matrix(ZZ,res[0]) s2 = Matrix(ZZ,res[1]) s4 = Matrix(ZZ,res[3]) s5 = Matrix(ZZ,res[4]) a = ((s4-s1)^(-1))*(s5-s2) e = a.LLL().determinant()