n = 114566998957451783636756389276471274690612644037126335470456866443567982817002189902938330449132444558501556339080521014838959058380963759366933946623103869574657553262938223064086322963492884606713973124514306815995276393344755433548846003574038937940253826360659447735554684257197194046341849089254659225497 e = 35489734227210930185586918984451799765619374486784192218215354633053183935617953856556709715097294481614236703293033675674496036691242573294182072757562322996800390363453350727372642264982749305833933966045097125311467413670410802534093354414115267442785896373815076066721029449240889291057288090241124904705 c = 60503455347700500866544596012233537789678841391057706123172519773588895502922586197178148979273264437566411675346207472455036341903878112074983509557751805365618433536738111588239911292341288514123006967218545943520736254346030465088445419278775539026233686559207400401082452551955780877227801939191694370380
fra = (e/n).continued_fraction() for i inrange(len(fra)): k = fra.numerator(i) d = fra.denominator(i)
if k != 0and (e*d-1) % k == 0: try: phi = (e*d-1) // k p_plus_q = n + 1 - phi p_min_q = (p_plus_q^2 - 4*n)^(1/2) p = (p_plus_q + p_min_q) // 2 q = n // p assert p*q == n
fpm1 = [_[0] for _ inlist(factor(p-1))] fs = [] for f in fpm1: if q2^((p-1) // f) == 1: fs += [f] p2 = (p-1) // product(fs) r = r % p2 for i inrange(product(fs)): if q^r == qr: break r = r + p2 print('r = %s' % r) assert q^r == qr
from Crypto.Util.number import * from hashlib import sha256
defenc(pt, G, A, T, S, p): s = randint(0,p-1) D = G^s E = A*T*A F = D*E*D K = list(D*S*D) key = sum(K[0])+sum(K[1])+sum(K[2]) mask = int(sha256(str(key).encode()).hexdigest(),16) ct = pt ^^ mask return ct, F
defdec(ct, Q, F, p): K = Q*F*Q key = sum(K[0])+sum(K[1])+sum(K[2]) mask = int(sha256(str(key).encode()).hexdigest(),16) pt = ct ^^ mask return pt
p = 72887242108660141996862343556330151015969690949835567252527194788428065480383 Fp2.<i> = GF(p^2, modulus=x^2+1) M = MatrixSpace(Fp2, 3, 3)
fpm1 = [_[0] for _ inlist(factor(p-1))] fs = [] for f in fpm1: if g2^((p-1) // f) == 1: fs += [f] print(fs) p2 = (p-1) // product(fs) s = s % p2 for i inrange(product(fs)): if g^s == d: break s = s + p2 print('s = %s' % s)
D = G^s K = list(D*S*D) key = sum(K[0])+sum(K[1])+sum(K[2]) mask = int(sha256(str(key).encode()).hexdigest(),16) pt = ct ^^ mask flag = bytes.fromhex(hex(pt)[2:]) print(flag)
LENGTH = 512 RATIO = 0.02024 M = 2**LENGTH withopen('./../output.txt') as f: exec(f.read())
data = [] for i inrange(LENGTH): tmp = [] for j inrange(len(x)): xji = x[j][::-1][i] if xji != '*': tmp += [(int(xji), j)] data += [tmp] print(i, tmp) print()
A = [pow(a, i, M) for i inrange(1, len(x)+1)] B = [b * sum([pow(a, k, M) for k inrange(i)]) % M for i inrange(1, len(x)+1)]
defgao(s0, i): if i >= LENGTH-1: #print('GET: %s' % s0) return [s0]
ress = [] if data[i] == []: #print(i, bin(s0)) res = gao(s0, i+1) if res == []: #print(i, bin(2^i + s0)) ress += gao(2^i + s0, i+1) ress += res
res = set() for di in data[i]: Mi = 2^(i+1) sim1 = (A[di[1]] * s0 + B[di[1]]) % (2^i) s0i = (di[0]*2^i + sim1 - B[di[1]]) * pow(A[di[1]], -1, Mi) % Mi res.add(s0i) res = list(res) #print(i, res) iflen(res) == 1: ress += gao(res[0], i+1) return ress
seeds = gao(0, 0) print(seeds)
for seed in seeds: m = c ^^ seed flag = bytes.fromhex(hex(m)[2:]) print(flag)
from sage.allimport * from sage.parallel.multiprocessing_sage import parallel_iter from tqdm import tqdm
# https://tover.xyz/p/2024-wdb-RSA/#Part-4-%E5%A4%9A%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%B7%B2%E7%9F%A5p%E9%AB%98%E4%BD%8D%E5%88%86%E8%A7%A3 defgao(n, ph, pl=1, pbits=512): global tqdm hbits = Integer(ph).nbits() lbits = Integer(pl).nbits() PR = PolynomialRing(Zmod(n), 'x') x = PR.gen() f = ph * 2**(pbits-hbits) + x * 2**lbits + pl f = f.monic() # p:512 ph:262 #roots = f.small_roots(X=2**(pbits-hbits-lbits+1), beta=0.48, epsilon=0.0106) roots = f.small_roots(X=2**(pbits-hbits-lbits+1), beta=0.48, epsilon=0.0103) if roots: pm = Integer(roots[0]) p = ph * 2**(pbits-hbits) + pm * 2**lbits + pl if n % p == 0: q = n // p print() print('p = %d' % p) print('q = %d' % q) return p, q returnNone
defmgao(n, ph, T=16, gmax=262): hbits = Integer(ph).nbits() gbits = gmax - hbits ph = ph << gbits N = 2**gbits for i in tqdm(range(N//T + 1)): pars = [] for j inrange(T): pars += [((n, ph+(T*i+j), 1, 512), {})] res = list(parallel_iter(T, gao, pars)) for r in res: if r[1] != None: return r[1]
ph = 115314121469787984258489158421056136177545051135641551928888818017665807264468 n = 135133139540786818977969958456509467902948924003478556140490841984247464940261764739984274397650928404945721248284577232814352745333641188749824519153271662051302477973525156608141358709265683759057060630360909926255299541198485901065352661702656282587105799982740927802530997159098015074633017964344230291287 #mgao(n, ph)
p = 13352463043552409670211183534740157814546713901105410408023687926498813469217507846107364405269402732967687839808637375591530105677153038557366731161035343 q = 10120465347855011176216116854406657716210971137988515839650722657457130494685812171761795141588424375299868315964294503826721743852498238577704636503052409 P = (p - q) & ((1 << 130) - 1) c = 1836794759996264077871820946090708779709415760553736759453665641907562256633157424959089180650539327925671892742819931875681606982615287882656254828326465758462357812873839261469783652663796071814218493268788421243190729887313099383264588659922912876424206670310928514588754069909128149471326084547056385690037197908766053620702238356084124023146075698878494434053246157524775269473152458661801907641122308756667762880284617915774590075511686821816948174618196839335059944389423693187930672934293905608970421003536691336581450927887931599275461176935079227494931457562345640133982771901848553204154760760399724074615092290799119053032875792219794072963200108352944441876206386518960615891547166767499506114294860833404421893612197040731184031783165365621722947731966143226777081983415797778111715332055871302609049501876860012070502369090417942239749695034267695710324328867728296996779
c = (c-1) * Integer(P).inverse_mod(n**3) % (n**3) assert c % n == 0 c = c // n assert P % 2 == 0 A = (P * n) // 2
PR = PolynomialRing(ZZ, 'x') x = PR.gen() f = A * x**2 - (A-1) * x - c m = f.roots()[0][0] flag = bytes.fromhex(hex(m)[2:]) print(flag)
n = 36443283250594259606482132779262570582448178589602577809591307671554949253094255209079689901493052116793388954529442162972106210862341856282788030374324677114528044629385805693771773377070021111949953333360526159026822968061585876873187059674130307295006486032106471182393880915860569773206853864515489855553 h = 57792516722001523643789088224096258172899052039145876393373730235406451592173971020702024058282699663364267742428240581839287357212741266617791207580236457 c = 24482128269957355675512496312977308128712253968496848873519792376434347925427116612997489113223781321628516365811583310346553402215907938918891908853234881284620764982626375301219763593402089309909155204943747718536894186749932544428588048770663458669109073657836937287831725958017345747881678942488157429000
ppq = h pmq = (h^2 - 4*2*7*n)^(1/2) assert (ppq + pmq) % 14 == 0 q = (ppq + pmq) // 14 p = n // q assert p * q == n p = Integer(p) q = Integer(q) print(p) print(q)
from Crypto.Util.number import * phi = (p-1) * (q-1)
# https://tover.xyz/p/n-root-in-F/ load('nth.py') e = 673 - 1 checker = genHeaderChecker('DASCTF') print(e) res = nthRSA_n(c, e, [p], checker=checker)