from Crypto.Util.number import * from re import findall from subprocess import check_output
defflatter(M): # compile https://github.com/keeganryan/flatter and put it in $PATH z = "[[" + "]\n[".join(" ".join(map(str, row)) for row in M) + "]]" ret = check_output(["flatter"], input=z.encode()) return matrix(M.nrows(), M.ncols(), map(int, findall(b"-?\\d+", ret)))
############################################################################################### data p = A = B =
############################################################################################### exp ########################################## part1 to mat n = 10 A, B = Matrix(Zmod(p), n, n, A), Matrix(Zmod(p), n, n, B)
########################################## part2 get solution of M1A = BM2 L = Matrix(Zmod(p), n^2, 2*n^2) for i inrange(n): for j inrange(n): for t inrange(n): L[n*i+j, n^2+n*i+t] -= A[t,j] L[n*i+j, j+n*t] += B[i,t] XY = Matrix(ZZ,L.right_kernel().basis())
########################################## part3 LLL L = Matrix(ZZ,2*n^2,2*n^2) for i inrange(n^2): for j inrange(2*n^2): L[i,j] = XY[i,j] L[n^2+i,n^2+i] = p #print(L.dimensions()) res = flatter(L)[0]
########################################## part4 get flag m1 = res[n^2:n^2+16] m2 = res[:16] flag = "" for i in m1: flag += chr(i) for i in m2: flag += chr(i) print(flag)
from Crypto.Util.number import * from re import findall from subprocess import check_output
defflatter(M): # compile https://github.com/keeganryan/flatter and put it in $PATH z = "[[" + "]\n[".join(" ".join(map(str, row)) for row in M) + "]]" ret = check_output(["flatter"], input=z.encode()) return matrix(M.nrows(), M.ncols(), map(int, findall(b"-?\\d+", ret)))
############################################################################################### data p = A = B =
############################################################################################### exp ########################################## part1 to mat n = 5 A = Matrix(Zmod(p), n, n, A)
########################################## part2 flatten FA = [] for i inrange(n): for j inrange(n): FA.append(A[j,i])
FFA = [] for i inrange(n^2): temp = [0]*n^2*i + FA + [0]*(n^4-n^2*i-n^2) FFA.append(temp)
########################################## part3 LLL L = block_matrix(ZZ, [ [1,Matrix(ZZ,FFA).T.stack(-vector(B))], [0,p] ] ) L[:,-n^2:] *= p #print(L.dimensions()) res = flatter(L)
########################################## part4 recover M1 for i in res: if(all(j == 0for j in i[-n^2:]) andabs(i[-n^2-1]) == 1): ans = i[:-n^2-1] M1 = Matrix(Zmod(p), n, n) for j inrange(n): temp = ans[:n^2]
for k inrange(n): mjk = gcd(ans[0+k], ans[n+k]) for r inrange(1,n-1): mjk = gcd(mjk, gcd(ans[r*n+k], ans[(r+1)*n+k])) M1[j,k] = mjk ans = ans[n^3:] print(M1) break
########################################## part5 recover M2 B = Matrix(Zmod(p), n, n, B) M2 = A^(-1)*M1^(-1)*B
########################################## part6 get flag defflatten(M): M_flatten = [] rows,cols = M.dimensions() for i inrange(rows): for j inrange(cols): M_flatten.append(M[i,j]) return M_flatten
for i in flatten(M1): print(chr(i), end="") print() for i in flatten(M2): print(chr(i), end="")
from Crypto.Util.number import * from Crypto.Cipher import AES from random import randint from hashlib import md5 from secret import flag
n = len(flag) p = getPrime(256) secret = [randint(1,p) for i inrange(n)]
defencrypt(n,p,msg): while(1): L = [] R = [] for i inrange(n): temp = random_vector(Zmod(p), n) L.append(temp) R.append(msg[i]*temp) L = Matrix(Zmod(p),L).T R = Matrix(Zmod(p),R).T if(L.rank() == n and R.rank() == n): return (R*L^(-1))^(bytes_to_long(b"Tequila"))
from Crypto.Util.number import * from Crypto.Cipher import AES from hashlib import md5
############################################################################################### data p = C = enc =
############################################################################################### exp ########################################## part1 to mat n = int(pow(len(C),1/2)) C = Matrix(GF(p), n, n, C)
########################################## part2 find eigenvalues and flag res = C.eigenvalues() e = bytes_to_long(b"Tequila") key = 0 for i in res: key += int(pow(i,inverse(e,p-1),p))
flag = AES.new(key=md5(str(key).encode()).digest(), nonce=b"Tiffany", mode=AES.MODE_CTR).decrypt(enc) print(flag)
n = 10 p = getPrime(256) M = Matrix(Zmod(p), n, n, pad(flag,n^2)) e = Matrix(Zmod(p), n, n, pad(b"",n^2)) W = random_matrix(Zmod(p), n, n) X = random_matrix(Zmod(p), n, n) Y = random_matrix(Zmod(p), n, n) Z = random_matrix(Zmod(p), n, n)
from Crypto.Util.number import * from re import findall from subprocess import check_output
defflatter(M): # compile https://github.com/keeganryan/flatter and put it in $PATH z = "[[" + "]\n[".join(" ".join(map(str, row)) for row in M) + "]]" ret = check_output(["flatter"], input=z.encode()) return matrix(M.nrows(), M.ncols(), map(int, findall(b"-?\\d+", ret)))
############################################################################################### data p = W = X = Y = Z = C =
############################################################################################### exp ########################################## part1 to mat n = 10 W, X, Y, Z, C = Matrix(Zmod(p),n,n,W), Matrix(Zmod(p),n,n,X), Matrix(Zmod(p),n,n,Y), Matrix(Zmod(p),n,n,Z), Matrix(Zmod(p),n,n,C)
########################################## part2 flatten defflatten(M): M_flatten = [] rows,cols = M.dimensions() for i inrange(rows): for j inrange(cols): M_flatten.append(M[i,j]) return M_flatten
E = W*X F = Y*Z EF = [] for i inrange(n): for j inrange(n): temp = [] for k inrange(n): for m inrange(n): temp.append(F[m,j]*E[i,k]) EF.append(temp)
EF = Matrix(Zmod(p), EF) CC = Matrix(Zmod(p), flatten(C)).T
########################################## part3 LWE L = block_matrix( [ [matrix.identity(n^2)*p, matrix.zero(n^2, n^2+1)], [(matrix(EF).T).stack(-vector(CC)).change_ring(ZZ), matrix.identity(n^2+1)], ] ) #print(L.dimensions()) res = flatter(L)[0] flag = "" for i in res: flag += chr(abs(i)) print(flag)
from Crypto.Util.number import * from random import randint from secret import flag
n = len(flag) p = getPrime(256)
defencrypt(n,p,msg): while(1): L = [msg] R = [randint(1,p)*msg] for i inrange(n-1): temp = random_vector(Zmod(p), n) L.append(temp) R.append(randint(1,p)*temp) L = Matrix(Zmod(p),L).T R = Matrix(Zmod(p),R).T if(L.rank() == n and R.rank() == n): return (R*L^(-1))^(bytes_to_long(b"Tequila"))
############################################################################################### data p = C =
############################################################################################### exp ########################################## part1 to mat n = int(pow(len(C),1/2)) C = Matrix(GF(p), n, n, C)
########################################## part2 find eigenvectors L = [] res = C.eigenvectors_right() for i in res: eigenvalue = i[0] eigenvector = i[1][0] L = block_matrix(ZZ,[ [Matrix(ZZ,eigenvector)], [p] ]) res = L.LLL()[1] if(abs(res[0]) < 128): for i in res: print(chr(abs(i)),end="") break
from Crypto.Util.number import * from random import randint from secret import flag
n = len(flag)-4 p = getPrime(256)
defencrypt(n,p,msg): while(1): L = [] R = [] for i inrange(n): temp = vector(Zmod(p), [randint(1,p) for _ inrange(n-5)] + list(msg[i:i+5])) L.append(temp) R.append(randint(1,p)*temp) L = Matrix(Zmod(p),L).T R = Matrix(Zmod(p),R).T if(L.rank() == n and R.rank() == n): return (R*L^(-1))^(randint(1,p))
############################################################################################### data
############################################################################################### exp ########################################## part1 to mat n = int(pow(len(C),1/2)) defto_mat(M): mat = [] for i inrange(n): temp = [] for j inrange(n): temp.append(M[n*i+j]) mat.append(temp) return Matrix(Zmod(p), mat) C = Matrix(GF(p),to_mat(C))
########################################## part2 find eigenvectors L = [] res = C.eigenvectors_right() flag_pad = [] for i in res: eigenvalue = i[0] eigenvector = i[1][0] L = block_matrix(ZZ,[ [Matrix(ZZ,eigenvector[-5:])], [p] ]) res = L.LLL()[1] temp = "" for i in res: temp += chr(abs(i)) flag_pad.append(temp)
prefix = "NSSC" flag = "NSSC" for i inrange(n+3): for j in flag_pad: if(j[:4] == prefix[-4:]): flag += j[-1] prefix = j break print(flag)
from Crypto.Util.number import * from random import randint from secret import flag
polyeval = lambda poly, x: sum(a*pow(x,i,n) for a,i in poly) % n
p = getPrime(256) q = getPrime(256) n = p*q e = 1337 m = bytes_to_long(flag)
nums = 10 g = [(randint(1,n), i) for i inrange(nums)] F = [] C = [] for i inrange(nums+1): f = [(randint(1,n), (randint(1,n))) for i inrange(nums)] F.append(f) C.append(polyeval(g,polyeval(f,m)))
############################################################################################### data n = c = F = C =
############################################################################################### exp nums = 10 e = 1337 PR.<x> = PolynomialRing(Zmod(n)) PRq.<a> = PR.quo(x^e-c) L = Matrix(PRq,nums+1,nums+1) for i in trange(nums+1): f = F[i] ci = C[i] temp = 0 for j inrange(nums): temp += f[j][0] * a^f[j][1] for j inrange(nums): L[i,j] = temp^j L[i,-1] = ci
m = -gcd(L.det().lift(), x^e-c)[0] print(long_to_bytes(int(m)))
############################################################################################### data p = C =
############################################################################################### exp n = 10 L = Matrix(Zmod(p), 0, n^2) R = Matrix(Zmod(p), 0, n) for i inrange(n): Ai = Matrix(Zmod(p), n, n, C[i][0]) Ci = Matrix(Zmod(p), n, n, C[i][1]) temp = Ai R = R.stack(Ci) for j inrange(1,n): temp = temp.augment(Ai^(j+1)) L = L.stack(temp)
F = L.solve_right(R) M = F[:n] for i inrange(n): for j inrange(n): print(chr(M[i,j]), end="")
from Crypto.Util.number import * from re import findall from subprocess import check_output
defflatter(M): # compile https://github.com/keeganryan/flatter and put it in $PATH z = "[[" + "]\n[".join(" ".join(map(str, row)) for row in M) + "]]" ret = check_output(["flatter"], input=z.encode()) return matrix(M.nrows(), M.ncols(), map(int, findall(b"-?\\d+", ret)))
defflatten(M): M_flatten = [] rows,cols = M.dimensions() for i inrange(rows): for j inrange(cols): M_flatten.append(M[i,j]) return M_flatten
############################################################################################### data p = C =
############################################################################################### exp n = 7 t = 1 defmatrix_lagrange_LLL(n, t, PT, p): ##### part1 flatten Ci to R R = [] for i inrange(t): C = flatten(Matrix(Zmod(p), n, n, PT[i][1])) for j in C: R.append(j) R = vector(Zmod(p), R) ##### part2 get L L = Matrix(Zmod(p), t*n^2, n^3) for r inrange(t): A = Matrix(Zmod(p), n, n, PT[r][0]) for j inrange(n): for k inrange(n):
for i inrange(n): Ai = A^(i+1) for x inrange(n): L[n^2*r+n*j+k,n^2*i+n*k+x] = Ai[j,x]
##### part3 LLL M = block_matrix(ZZ,[ [1,Matrix(ZZ,L).T.stack(Matrix(ZZ,R))], [0,p] ]) M[:,-t*n^2:] *= p #print(M.dimensions()) res = flatter(M)[0][:n^2] for i inrange(n): for j inrange(n): print(chr(res[n*j+i]), end="")
from Crypto.Util.number import * from tqdm import * from re import findall from subprocess import check_output
defflatter(M): # compile https://github.com/keeganryan/flatter and put it in $PATH z = "[[" + "]\n[".join(" ".join(map(str, row)) for row in M) + "]]" ret = check_output(["flatter"], input=z.encode()) return matrix(M.nrows(), M.ncols(), map(int, findall(b"-?\\d+", ret)))
############################################################################################### data p = C =
############################################################################################### exp ############################################## part1 construct matrix equation n = 15 L = Matrix(Zmod(p), 0, n^2) R = Matrix(Zmod(p), 0, n) for i inrange(1): Ai = Matrix(Zmod(p), n, n, C[i][0]) Ci = Matrix(Zmod(p), n, n, C[i][1]) temp = Ai R = R.stack(Ci) for j inrange(1,n): temp = temp.augment(Ai^(j+1)) L = L.stack(temp)
############################################## part2 LLL F1 = [] for i in trange(n): L1 = Matrix(ZZ,L) R1 = R[:,i:i+1] M = block_matrix(ZZ,[ [1,L1.T.stack(Matrix(R1).T)], [0,p] ]) M[:,-n:] *= p res = flatter(M)[0][:n] F1.append(list(map(abs,res)))
############################################## part3 recover F1 for i inrange(n): for j inrange(n): print(chr(F1[j][i]), end="")
#NSSCTF{d1m3nSi0n_1s_n0T_A_pr0B13m_FOr_th1S_T@sK}
Matrix9
题目:
1 2 3 4 5 6 7 8 9 10 11 12
from Crypto.Util.number import * from Crypto.Cipher import AES from hashlib import md5 from secret import flag
n = 10 A = random_matrix(GF(2), n, n) PR.<x> = PolynomialRing(GF(2)) B = PR.random_element(degree=n)(A)
from Crypto.Util.number import * from itertools import * from Crypto.Cipher import AES from hashlib import md5
############################################################################################### data B = enc =
############################################################################################### exp ########################################## part1 to mat n = 10 B = Matrix(GF(2), n, n, B)
########################################## part2 get solution of BA = AB L = Matrix(GF(2), n^2, n^2) for i inrange(n): for j inrange(n): for t inrange(n): L[n*i+j, n*i+t] += B[t,j] L[n*i+j, j+n*t] -= B[i,t] Ker = L.right_kernel().basis()
########################################## part3 bruteforce for A defrecover(A_flatten, n, p): A = Matrix(Zmod(p), n, n) for i inrange(n): for j inrange(n): A[i,j] = A_flatten[n*i+j] return A
for i in product([0,1], repeat=len(Ker)): A = Matrix(GF(2), n, n) for j inrange(len(Ker)): A += recover(i[j]*Ker[j], n, 2) flag = AES.new(key=md5(str(A).encode()).digest(), nonce=b"Tiffany", mode=AES.MODE_CTR).decrypt(enc) if(b"NSSCTF"in flag): print(flag) break
#NSSCTF{U31nG_C0mmut@t1vE_and_Bru7eF0rCe!}
Matrix9_v2
题目:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from Crypto.Util.number import * from os import urandom from secret import flag
############################################################################################### data p = B =
############################################################################################### exp ########################################## part1 to mat n = 10 B = Matrix(Zmod(p), n, n, B)
########################################## part2 get solution of BX = XB L = Matrix(Zmod(p), n^2, n^2) for i inrange(n): for j inrange(n): for t inrange(n): L[n*i+j, n*i+t] += B[t,j] L[n*i+j, j+n*t] -= B[i,t] T = Matrix(Zmod(p), L.right_kernel().basis())
########################################## part3 LLL T = block_matrix(ZZ,[ [T], [p] ]) res = T.LLL()[n+1] for i in res: if(abs(i) < 128andabs(i) > 32): print(chr(abs(i)), end="") else: print("*", end="")