#T1 from Crypto.Util.number import * from random import * x=[(1<<95)|getrandbits(96) for i inrange(3)] a=[getrandbits(1024) for i inrange(3)] p=getPrime(1024) veca=vector(GF(p),a) vecx=vector(GF(p),x) y=veca*vecx print(y) print(veca)
#T2 from Crypto.Util.number import * from random import * p=getPrime(1024) x=[getPrime(160) for i inrange(3)] y=[getPrime(160) for i inrange(3)] a=[getrandbits(1024) for i inrange(3)] b=[getrandbits(1024) for i inrange(3)] z=[(a[i]*x[i]+b[i]*y[i])%p for i inrange(3)] print(a) print(b) print(z) print(p)
#T3 from Crypto.Util.number import * from random import * x=[getPrime(240) for i inrange(3)] a=[getrandbits(1024) for i inrange(3)] p=getPrime(1024) veca=vector(GF(p),a) vecx=vector(GF(p),x) y=veca*vecx print(y) print(veca)
#T4 from Crypto.Util.number import * from random import * p=getPrime(1024) x=[getPrime(450) for i inrange(3)] y=[getPrime(450) for i inrange(3)] a=[getrandbits(1024) for i inrange(3)] b=[getrandbits(1024) for i inrange(3)] z=[(a[i]*x[i]+b[i]*y[i])%p for i inrange(3)] print(a) print(b) print(z) print(p)
#T5 from Crypto.Util.number import * from random import * p=getPrime(1024) x=[getPrime(333) for i inrange(3)] y=[getPrime(633) for i inrange(3)] a=[getrandbits(1024) for i inrange(3)] b=[getrandbits(1024) for i inrange(3)] z=[(a[i]*x[i]+b[i]*y[i])%p for i inrange(3)] print(a) print(b) print(z) print(p)
# sagemath import random from Crypto.Util.number import *
flag = b''
k = 3 d = k/(2*(k+1)) ns = [] pqs = [] es = []
for i inrange(3): p = getPrime(512) q = getPrime(512) if p < q: tmp = p p = q q = tmp n = p*q ns.append(n) pqs.append((p,q))
n = min(ns) x = random.randint(0,int(n^(d/2))) x = next_prime(x)
for i inrange(3): p,q = pqs[i][0],pqs[i][1] bound1 = int((p-q)/(3*(p+q)) * x * n ^ 0.25) bound2 = int((p-q)/(3*(p+q)) * x^2 * n ^ 0.25) z = random.randint(bound1,bound2) f = (p-1)*(q-1) e = inverse(x^2,f) * z % f es.append(e)
n=58456238154727772714762362790039415372652580738847549549926175214592421074440425380491278175531057453959583518365006871715668115289674464868754600641087664868445977308497244134179400977293896807231964047365956545629327100737851868274388108150918741474301542596310528990700043925342513137054619092876834352167 y=15291667666307414493036951046107275741397940767822287922559621898054774057554431022160493431539991508171319851807097492122079767748903000613100351624105777 R.<x> = PolynomialRing(RealField(2048)) f = x * ((y) - x) - n res = f.roots() deflongdouble2int(x): x=str(x) x=x.split('.') x[1]=x[1].split('e') return Integer(x[0]+x[1][0][:int(x[1][1])]) for i in res: print(longdouble2int(i[0])) #7595466686419558151205913432118661460574378336265188601308332178368424478703277500529523587366925897212947299535668041397861697290034233117986300118079322 #7696200979887856341831037613988614280823562431557099321251289719686349578851153521630969844173065610958372552271429450724218070458868767495114051506026454
ph=7696200979887856341831037613988614280823562431557099321251289719686349578851153521630969844173065610958372552271429450724218070458868767495114051506026454 n=58456238154727772714762362790039415372652580738847549549926175214592421074440425380491278175531057453959583518365006871715668115289674464868754600641087664868445977308497244134179400977293896807231964047365956545629327100737851868274388108150918741474301542596310528990700043925342513137054619092876834352167 from multiprocessing import * from tqdm import * defsolve(p4): pbits = 512 kbits = pbits - p4.nbits() p4 = p4 << kbits PR.<x> = PolynomialRing(Zmod(n)) f = x + p4 roots = f.small_roots(X=2**kbits, beta=0.42,epsilon=0.012) if roots: p = p4 + int(roots[0]) q = n//p withopen('result','w') as f: f.write(f'p: {p}\n') f.write(f'q: {q}\n') returnTrue returnFalse ph=ph>>256 defsolve1(): for i in tqdm(range(2**11)): if(solve((ph<<12)|i)): return defsolve2(): for i in tqdm(range(2**11,2**12)): if(solve((ph<<12)|i)): return from multiprocessing import Process, current_process PROC=[] PROC.append(Process(target=solve1)) PROC.append(Process(target=solve2)) for i inrange(2): PROC[i].start() for i inrange(2): PROC[i].join()
from Crypto.Util.number import * phi=(p-1)*(q-1) n=p*q e=2820971 for i in cbrtcq: print(long_to_bytes(pow(i,inverse(e,q-1),q))) #flag{N3w_Attacks_4_key_equat1ons}
from subprocess import check_output from re import findall from sage.allimport *
defflatter(M): # flatter 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)))
defmatrix_overview(BB): # see the shape of matrix for ii inrange(BB.dimensions()[0]): a = ('%02d ' % ii) for jj inrange(BB.dimensions()[1]): a += '0'if BB[ii, jj] == 0else'X' if BB.dimensions()[0] < 60: a += ' ' print(a)
delta = f.degree() # 度delta N = f.parent().characteristic() # 模数N PR = PolynomialRing(ZZ, 'x') x = PR.gen()
Zm = f.base_ring() # Zmod(N) f = f.change_ring(ZZ) # ZZ下f(x) ifnot f.is_monic(): # 首一 f = f.monic() # f = f * f[delta].inverse_mod(N)
m = ceil(max(beta * beta / (delta * epsilon), 7 * beta / delta)) # m t = floor(delta * m * (1 / beta - 1)) # t print('m={}, t={}'.format(m, t))
f_ij = [] for i inrange(m): for j inrange(delta): f_ij.append(x ** j * N ** (m - i) * f ** i) # shift g_ij(x) for i inrange(t): f_ij.append(x ** i * f ** m) # shift h_i(x)
monomials = [] for g in f_ij: monomials += g.monomials() # 统计所有出现的单项 x^i monomials = sorted(set(monomials)) # 去重并排序
M = Matrix(ZZ, len(f_ij), len(monomials)) # 行数为多项式个数,列数为所有单项可能个数 for i inrange(M.nrows()): for j, monomial inenumerate(monomials): M[i, j] = f_ij[i].monomial_coefficient(monomial) * monomial.subs(x=X) # g_ij(xX)和h_i(xX) matrix_overview(M) # see assert M.nrows() == M.ncols() # 方阵 nrows()=ncols() B = flatter(M) # flater加速 print('end LLL') for j inrange(M.nrows()): # 得到f(xX),构建f(x),求根检验 Cx = sum(ZZ(B[j, i] // monomials[i](X)) * monomials[i] for i inrange(M.ncols())) # construct polynomial, R = Cx.roots() # get roots roots = [Zm(r[0]) for r in R ifabs(r[0]) <= X] # check x0<=X roots = [r for r in roots if gcd(N, ZZ(f(r))) >= ZZ(floor(N ** beta))] # check gcd(f(x_0),N)>N^beta if roots: return roots # 返回root
N = 135500646574582511239845764710311769260801998982429500680171919823431178899526463566215834234383331374445093363969218810906991784569340270510936759183504496584225937614940086329775325893307453919055830270986601152002191368431527285285313669979358099782497422114870417519470053198217401297960844455029559146309 h = 918578024558168836638919636090777586135497638818209533615420650282292168631485 PR = PolynomialRing(Zmod(N), 'x') x = PR.gen() f = (h << 253) + x roots = Small_Roots_Univariate(f, 2 ** 253, 0.499, 0.005) print(roots)
from hashlib import sha256 from Crypto.Util.number import inverse, bytes_to_long from random import randint from os import urandom #from secret import flag from datetime import * import signal
p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F a = 0 b = 7 gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798 gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8 order = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
zero = (0, 0) G = (gx, gy)
defadd(p1, p2): if p1 == zero: return p2 if p2 == zero: return p1 (p1x, p1y), (p2x, p2y) = p1, p2 if p1x == p2x and (p1y != p2y or p1y == 0): return zero if p1x == p2x: tmp = (3 * p1x * p1x + a) * inverse(2 * p1y, p) % p else: tmp = (p2y - p1y) * inverse(p2x - p1x, p) % p x = (tmp * tmp - p1x - p2x) % p y = (tmp * (p1x - x) - p1y) % p return (int(x), int(y))
defmul(n, p): r = zero tmp = p while0 < n: if n & 1 == 1: r = add(r, tmp) n, tmp = n >> 1, add(tmp, tmp) return r
defsign(msg, q, d, G): z = int(sha256(msg).hexdigest(), 16) whileTrue: k = randint(1, q - 1) P = mul(k, G) r = P[0] s = inverse(k, q) * (z + r * d) % q if r != 0and s != 0: return r, s, k.bit_length()
defverify(sig, msg, q, G, Q): r, s = sig print(r, s) if r == 0or s == 0: returnFalse z = int(sha256(msg).hexdigest(), 16) u1 = z * inverse(s, q) % q u2 = r * inverse(s, q) % q P = add(mul(u1, G), mul(u2, Q)) return P[0] == r
signal.alarm(240) menu = ''' 1.sign 2.verify 3.exit ''' uur=urandom(27) secret_key = bytes_to_long( uur+ b"qwbs8") withopen('dbg','w') as f: # only for debug check. f.write(uur.hex()) Q = mul(secret_key, G)
count = 678#1300 #1300 for debug and test, original changces is 678 for i inrange(count): print(menu) op = int(input(">").strip()) if op == 1: msg = input("msg: ").strip().encode() if msg == b"qwbs8_send_flag_to_me": print("another msg plz.") else: r, s, kbits = sign(msg, order, secret_key, G) print(r, s, kbits) elif op == 2: msg = input("msg: ").strip().encode() ur = int(input("r: ").strip()) us = int(input("s: ").strip()) sig = (ur, us) if verify(sig, msg, order, G, Q): if msg == b"qwbs8_send_flag_to_me": flag='flag{success-at-'+str(datetime.now()).replace(' ','-')+'}' print(flag) else: print(True) else: print("malformed signature.") else: exit(0)
from Crypto.Util.number import * from pwn import * from tqdm import * from hashlib import sha256 #context.log_level='debug' sh=process(['python3','taskdbg.py']) q=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 z = int(sha256(b'1').hexdigest(), 16) D=[] defcmp(xtuple): return xtuple[2] for i in tqdm(range(1200)): sh.recvuntil(b'>') sh.sendline(b'1') sh.recvuntil(b'msg: ') sh.sendline(b'1') rline=sh.recvline(keepends=False) r,s,lgk=[int(i) for i in rline.split()] if(lgk<=253): D.append((r,s,lgk)) D=sorted(D,key=cmp) dl=bytes_to_long(b"qwbs8") n=len(D) C,B=[],[] for i inrange(n): ri,si,lgk=D[i] C.append((z+dl*ri)*inverse(si,q)%q) B.append((ri*2**40)*inverse(si,q)%q) M=block_matrix(ZZ, [ [identity_matrix(2),matrix(ZZ,[B,C])], [zero_matrix(n,2),q*identity_matrix(n)], ]) scale=[216,0]+[(D[i][2]) for i inrange(n)] H=diagonal_matrix([1<<(253-i) for i in scale]) M3L=(M*H).LLL() count=0 for v in M3L: if(abs(v[1])==2**253and v[0]%2**37==0): print('{:054x}'.format(abs(v[0]>>37))) #00035200d2eb85df354d512e1ed7507fafa5f65421af758c6b69da #0001e3bb8bf2700f617a094f0a9bbeb9a4846a50e9fa6fe58ae15f #00052585d2d84afe1946e4c677b624fd13d46406c738b1dd456dbd #1565175e195b3e11fcab83369564a74051eaddd58da439d1d43b4f #156c8b3f909c72c31b7bd954c030a0e1c69d8c538ad3c375ff8eba #143967156dabd7d3acfc42a814a16017166cd69fd9f1e3d61c293d #1733855122e779bb0825373d45e906062963d8889d9291cea0d1a7 #17259640adc01e43c2b4e11150063b9703fe06734a2762f931a4bb #16059790aec15b5f13ad8b9bd0616741b9a46b9af96c75fac7f97f #033c737ebad6cb1e7e0c1f4edfe1f2d76f35b56026475a81df7ee0 #140f3ee633e6aaba4634aaa658010c524923b369035728c95fa649 #12d7a77743dd51f6368865420fb199ca13cb389972e2a6279214d7 #017c96aee9f5b7c4daf633f3e3d384a508545c73fec68f4bf02ce8 #... ... 反正没一个对的 #ans: d2dffadd7e9a6d4e839c17b2c5907a51c130cf352c9eb356c0a0e4
from Crypto.Util.number import * from pwn import * from tqdm import * from hashlib import sha256 #context.log_level='debug' sh=process(['python3','taskdbg.py']) q=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 z = int(sha256(b'1').hexdigest(), 16) D=[] defcmp(xtuple): return xtuple[2] for i in tqdm(range(1200)): sh.recvuntil(b'>') sh.sendline(b'1') sh.recvuntil(b'msg: ') sh.sendline(b'1') rline=sh.recvline(keepends=False) r,s,lgk=[int(i) for i in rline.split()] if(lgk<=253): D.append((r,s,lgk)) #D=sorted(D,key=cmp) dl=bytes_to_long(b"qwbs8") n=len(D) C,B=[],[] for i inrange(n): ri,si,lgk=D[i] C.append((-2**(D[i][2]-1) +(z+ri*dl+ri*2**255)*inverse(si,q))%q) B.append((ri*2**40)*inverse(si,q)%q) M=block_matrix(ZZ, [ [identity_matrix(2),matrix(ZZ,[B,C])], [zero_matrix(n,2),q*identity_matrix(n)], ]) # scale=[2**215,1]+[2**(D[i][2]-1) for i in range(n)] H=diagonal_matrix([1<<38,1<<253]+[1<<(253-D[i][2]) for i inrange(n)]) M3L=(M*H).LLL() count=0 for v in M3L: if(abs(v[1])==2**253and v[0]%2**(38)==0): print('{:054x}'.format(2**215+(v[0]>>38)),'{:054x}'.format(2**215-(v[0]>>38))) #76999ca36b634ef46930567a8115ac40f3cb3c9f89e6432fd9dcc8 8966635c949cb10b96cfa9857eea53bf0c34c3607619bcd0262338 #... #ans:8966635c949cb10b96cfa9857eea53bf0c34c3607619bcd0262338
from Crypto.Util.number import * from pwn import * from tqdm import * from hashlib import sha256 #context.log_level='debug' sh=process(['python3','taskdbg.py']) q=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 z = int(sha256(b'1').hexdigest(), 16) D=[] defcmp(xtuple): return xtuple[2] for i in tqdm(range(1200)): sh.recvuntil(b'>') sh.sendline(b'1') sh.recvuntil(b'msg: ') sh.sendline(b'1') rline=sh.recvline(keepends=False) r,s,lgk=[int(i) for i in rline.split()] if(lgk<=253): D.append((r,s,lgk)) #D=sorted(D,key=cmp) dl=bytes_to_long(b"qwbs8") n=len(D) C,B=[],[] for i inrange(n): ri,si,lgk=D[i] C.append((-2**(D[i][2]-1) +(z+ri*dl+ri*2**255)*inverse(si,q))%q) B.append((ri*2**40)*inverse(si,q)%q) M=block_matrix(ZZ, [ [identity_matrix(2),matrix(ZZ,[B,C])], [zero_matrix(n,2),q*identity_matrix(n)], ]) # scale=[2**215,1]+[2**(D[i][2]-1) for i in range(n)] H=diagonal_matrix([1<<38,1<<253]+[1<<(253-D[i][2]) for i inrange(n)]) M3L=(M*H).LLL() count=0 dh1,dh2,k0p,k0n=None,None,None,None for v in M3L: if(abs(v[1])==2**253and v[0]%2**(38)==0): vlow=v[0]>>38 dh1,dh2=2**215+vlow,2**215-vlow k0p=2**(D[0][2]-1)+(v[2]>>(253-D[0][2])) k0n=2**(D[0][2]-1)-(v[2]>>(253-D[0][2])) break print(hex(dh1),hex(dh2)) p=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F E=EllipticCurve(GF(p),[0,7]) gx=0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798 gy=0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8 G=E(gx,gy) d=None for k in [k0p,k0n]: for dh in [dh1,dh2]: testd=(dh<<40)|dl P=k*G r0=P[0]