seed = H(secret) f = R( [bytes_to_long(secret)] + [bytes_to_long(H(seed)) for _ inrange(n - 1)] ) x = [getRandomRange(2, p - 1) for _ inrange(n)] y = [ZZ(f(xi)^2 + getPrime(256)) for xi in x]
import os from random import getrandbits from hashlib import sha256, md5 from Crypto.Util.number import * from Crypto.Cipher import AES from Crypto.Util.Padding import pad from secret import flag
classShamir: def__init__(self, pbits, noise_bit, n, m): self.pbits = pbits self.noise_bit = noise_bit self.n = n self.m = m self.p = getPrime(pbits) P.<x> = PolynomialRing(Zmod(self.p)) self.poly = P([bytes_to_long(sha256(os.urandom(32)).digest()) for i inrange(self.n)])
defsample(self): t = getrandbits(self.pbits) y = int(self.poly(t)) noise = getrandbits(noise_bit) return (t, y | noise)
defget_msg(self): res = [] for i inrange(self.m): res.append(self.sample()) return res
pbits = 400 noise_bit = 32 n = 100 m = 75
shamir = Shamir(pbits, noise_bit, n, m) coefficient = shamir.poly() key = "".join([str(i) for i inlist(coefficient)[1:]]) key = md5(key.encode()).digest() aes = AES.new(key = key, mode = AES.MODE_ECB) ct = aes.encrypt(pad(flag, 16))
withopen("data.txt", "w") as f: f.write(str(shamir.p)+'\n') f.write(str(shamir.get_msg())+'\n') f.write(str(bytes_to_long(ct))+'\n')
from hashlib import sha256, md5 from Crypto.Util.number import * from Crypto.Cipher import AES
defflatter(M): from subprocess import check_output from re import findall # 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)))
withopen('data.txt','r') as f: f = (f.read().encode().split(b'\n')[:-1])
X = [] B = [] for i in msg: X.append(i[0]) B.append(i[1])
L = matrix(ZZ,m+1+n,m+1+n)
K = 2**256 Kn = 2**(256-noise_bit)
for i inrange(m): for j inrange(n): L[j,i] = pow(X[i],j,p) L[n,i] = B[i] L[i+n+1,i] = p
L = Kn*L
for i inrange(n): L[i,i+m] = 1 L[n,-1] = K
LL = flatter(L)
for i in LL: ifabs(i[-1]) == K: res = [abs(j) for j in i[-n:-1]] key = ''.join([str(i) for i in res]) key = md5(key.encode()).digest() aes = AES.new(key=key, mode=AES.MODE_ECB) print(aes.decrypt(ct))
from hashlib import sha256, md5 from Crypto.Util.number import * from Crypto.Cipher import AES 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()) from re import findall return matrix(M.nrows(), M.ncols(), map(int, findall(b"-?\\d+", ret)))
pbits = 400 noise_bit = 32 n = 100 m = 75 p = msgs = ct =
########################################################### part1 construct A= [] b = [] for i inrange(m): temp = [msgs[i][0]^j % p for j inrange(n)] A.append(temp) b.append(msgs[i][1]) A = Matrix(ZZ,A) b = vector(ZZ,b)
########################################################### part2 LLL #primal_attack1 defprimal_attack1(A,b,m,n,p,esz): L = block_matrix( [ [matrix.identity(m)*p,matrix.zero(m, n+1)], [(matrix(A).T).stack(-vector(b)).change_ring(ZZ),matrix.identity(n+1)], ] ) print(L.dimensions()) Q = diagonal_matrix([2^256//esz]*m + [1]*n + [2^256]) L *= Q L = flatter(L) L /= Q for res in L: if(res[-1] == 1): s = vector(GF(p), res[-n-1:-1]) return s elif(res[-1] == -1): s = -vector(GF(p), res[-n-1:-1]) return s res = primal_attack1(A,b,m,n,p,2^32) print(res)
key = "".join([str(i) for i inlist(res)[1:]]) key = md5(key.encode()).digest() aes = AES.new(key = key, mode = AES.MODE_ECB) flag = aes.decrypt(long_to_bytes(ct)) print(flag)