#!/usr/local/bin/python from secrets import randbits from Crypto.Util.number import getPrime from random import randrange
defsquare_root(a, p): if legendre_symbol(a, p) != 1: return0 elif a == 0: return0 elif p == 2: return0 elif p % 4 == 3: returnpow(a, (p + 1) // 4, p) s = p - 1 e = 0 while s % 2 == 0: s //= 2 e += 1 n = 2 while legendre_symbol(n, p) != -1: n += 1 x = pow(a, (s + 1) // 2, p) b = pow(a, s, p) g = pow(n, s, p) r = e whileTrue: t = b m = 0 for m inrange(r): if t == 1: break t = pow(t, 2, p) if m == 0: return x gs = pow(g, 2 ** (r - m - 1), p) g = (gs * gs) % p x = (x * gs) % p b = (b * g) % p r = m
deflegendre_symbol(a, p): ls = pow(a, (p - 1) // 2, p) return -1if ls == p - 1else ls
classEllipticCurve: def__init__(self, p, a, b): self.a = a self.b = b self.p = p ifnot self.check_curve(): raise Exception("Not an elliptic curve!") defcheck_curve(self): discrim = -16 * (4*pow(self.a, 3) + 27*pow(self.b, 2)) if discrim % self.p: return1 return0 deflift_x(self, px): y2 = (pow(px, 3) + self.a*px + self.b) % self.p py = square_root(y2, self.p) if py == 0: raise Exception("No point on elliptic curve.") return py
withopen("flag.txt", "rb") as f: flag = f.read() flag = int.from_bytes(flag, 'big')
print("Generating parameters...") whileTrue: p = getPrime(512) a, b = randbits(384), randbits(384) try: E = EllipticCurve(p, a, b) fy = E.lift_x(flag) print(f"p = {p}") print(f"flag y = {fy}") break except: continue checked = set() count = 0 while count < 2022: x = randrange(2, p) ifint(x) in checked or x < 2**384orabs(x - p) < 2**384: print(">:(") continue try: e = randbits(48) print(f"e = {e}") E = EllipticCurve(p, a^e, b^e) py = E.lift_x(x) checked.add(x) print(f"x = {x}") print(f"y = {py}") count += 1 except: print(":(") more = input("more> ") if more.strip() == "no": break print("bye!")
m=Matrix(QQ,6,6) m.set_column(0,[p,2^48*(x1-x2)%p,-x1,x2,1,(m2-m1)%p]) m[1,1]=2^-336 for i inrange(2,5): m[i,i]=2^-48 m[5,5]=1 res=m.LLL() #print(res) for row in res: if row[0]==0and row[-1]==1: au=Integer(row[1]*2^384) al=Integer(-row[2]*2^48)^^e1 a=au+al b=((m1-(a^^e1)*x1)%p)^^e1 R.<X>=PolynomialRing(Zmod(p)) f=X^3+a*X+b-flagy^2 roots=f.roots() for root in roots: print(long_to_bytes(int(root[0])))
from Crypto.Util.number import * from Crypto.Cipher import AES from hashlib import sha256 from tqdm import tqdm
classLFSR: def__init__(self, key, taps): self.key = key self.taps = taps self.state = key def_clock(self): ob = self.state[0] self.state = self.state[1:] + [sum([self.state[t] for t in self.taps]) ] return ob
lines=open('output.txt','r').readlines() out=list(map(int,bin(int(lines[0],16))[2:].zfill(118*128)))[::-1] out_parts=[ out[i:i+128] for i inrange(0,len(out),128)] ct=bytes.fromhex(lines[1]) iv = ct[:16] c = ct[16:]
R=PolynomialRing(GF(2),['x%d'%i for i inrange(128)]) key=list(R.gens()) l = LFSR(key, [1, 2, 7, 3, 12, 73])
I=[] for i inrange(118): bits = [l._clock() for _ inrange(128)] I.append(sum(bits)-sum(out_parts[i])%2)
for i in tqdm(range(2^11)): guess=list(map(int,bin(int(i))[2:].zfill(11))) guessI=[key[i]-guess[i] for i inrange(11)] res=Ideal(I+guessI).groebner_basis() iflen(res)!=1: k=int(''.join([ str(res[i].constant_coefficient()) for i inrange(len(res))]),2) aeskey = sha256(k.to_bytes(16, 'big')).digest()[:32] cipher=AES.new(aeskey,AES.MODE_CBC,iv) m=cipher.decrypt(c) ifb'corctf{'in m: print(m) exit(0) #corctf{m4yb3_w3_sh0uld_ju5t_cut_hum4n5_0ut_0f_th1s_c0mpl3t3ly_1f_th3y_d3c1d3_t0_f4k3_shuffl3_0r_s0m3th1ng}