from Pwn4Sage.pwn import * from tqdm import * from Crypto.Util.number import * import itertools
defsmall_roots(f, bounds, m=1, d=None): ifnot d: d = f.degree()
R = f.base_ring() N = R.cardinality()
f /= f.coefficients().pop(0) f = f.change_ring(ZZ)
G = Sequence([], f.parent()) for i inrange(m + 1): base = N ^ (m - i) * f ^ i for shifts in itertools.product(range(d), repeat=f.nvariables()): g = base * prod(map(power, f.variables(), shifts)) G.append(g)
factors = [monomial(*bounds) for monomial in monomials] for i, factor inenumerate(factors): B.rescale_col(i, factor)
B = B.dense_matrix().LLL()
B = B.change_ring(QQ) for i, factor inenumerate(factors): B.rescale_col(i, 1 / factor)
H = Sequence([], f.parent().change_ring(QQ)) for h infilter(None, B * monomials): H.append(h) I = H.ideal() if I.dimension() == -1: H.pop() elif I.dimension() == 0: roots = [] for root in I.variety(ring=ZZ): root = tuple(R(root[var]) for var in f.variables()) roots.append(root) return roots
return []
for i in trange(100): try: sh = remote("node4.anna.nssctf.cn",28593) p = int(sh.recvline().strip().decode().split("=")[-1]) sh.sendline(b"1") sh.recvline() r1 = int(sh.recvline().strip().decode().split("=")[-1]) d1 = int(sh.recvline().strip().decode().split("=")[-1]) sh.sendline(b"1") sh.recvline() r2 = int(sh.recvline().strip().decode().split("=")[-1]) d2 = int(sh.recvline().strip().decode().split("=")[-1])
from Pwn4Sage.pwn import * from tqdm import * from Crypto.Util.number import *
defgetData(): p = int(sh.recvline().strip().decode().split("=")[-1]) r = [] d = [] for i inrange(15): sh.sendline(b"1") sh.recvline() rr = int(sh.recvline().strip().decode().split("=")[-1]) dd = int(sh.recvline().strip().decode().split("=")[-1]) r.append(rr) d.append(dd) iflen(r) == 15andlen(d) == 15: return r,d,p
defgetE(): num = 15 N = 3*num-1 T = 2^(328*3) A = [r[i] - r[0] for i inrange(1,num)] B = [d[0]*(r[i] - r[0]) + 1for i inrange(1,num)] C = [d[i]*(r[i] - r[0]) - 1for i inrange(1,num)] D = [d[0]*d[i]*(r[i] - r[0]) + (d[i] - d[0]) for i inrange(1,num)] Ge = Matrix(ZZ,N,N) for i inrange(N): Ge[i,i] = 1 for i inrange(num-1): Ge[2*num+i,2*num+i] = p*T Ge[i,2*num+i] = A[i]*T Ge[num-1,2*num+i] = C[i]*T Ge[num+i,2*num+i] = B[i]*T Ge[2*num-1,2*num+i] = D[i]*T for i inrange(num): Ge[num-1+i,num-1+i] = 2^328 Ge[2*num-1,2*num-1] = 2^(328*2) for line in Ge.LLL(): if line[-1] == 0: e1 = int(abs(line[14]) // 2^328) ifint(e1).bit_length() == 328: print(f"e1 = {e1}") return e1
defgetData(): cipher = [] p = [] lines = open("output.txt", "rb").readlines() for i inrange(0, len(lines), 2): pp = eval(lines[i].strip(b"p = ").strip(b"\n")) p.append(pp) cc = eval(lines[i+1].strip(b"enc = ").strip(b"\n")) cipher.append(cc) return cipher,p cipher,p = getData() x = [] #提取所有是模p平方剩余的x
for i inrange(len(cipher)): num = 0 for j inrange(len(cipher[0])): if gmpy2.jacobi(cipher[i][j],p[i]) == 1: num += 1 t = num / len(cipher[0]) if t > 0.7: x.append(i)
m = '' for i inrange(len(cipher[i])): num = True for j in x: if gmpy2.jacobi(cipher[j][i],p[j]) != 1: num = False break if num: m += '1' else: m += '0'
flag = long_to_bytes(int(m[::-1],2)) print(flag) # flag{c8b10fb7-95f8-47a2-bfb3-b9489ad39711}
网鼎杯 2022 青龙组——Grasshopper
task.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from Crypto.Util.number import * from random import randrange
from grassfield import flag
p = getPrime(16)
k = [randrange(1,p) for i inrange(5)]
for i inrange(len(flag)): grasshopper = flag[i] for j inrange(5): k[j] = grasshopper = grasshopper * k[j] % p print('Grasshopper#'+str(i).zfill(2)+':'+hex(grasshopper)[2:].zfill(4))
from Crypto.Util.number import isPrime,bytes_to_long import random import os
defgetWYSIprime(): whileTrue: digits = [random.choice("727") for _ inrange(272)] prime = int("".join(digits)) if isPrime(prime): return prime # RSA encryption using the WYSI primes p = getWYSIprime() q = getWYSIprime() n = p * q e = 65537 flag = bytes_to_long(os.getenv("FLAG",b"osu{fake_flag_for_testing}")) ciphertext = pow(flag,e,n) print(f"n = {n}") print(f"e = {e}") print(f"ciphertext = {ciphertext}") """ n = 2160489795493918825870689458820648828073650907916827108594219132976202835249425984494778310568338106260399032800745421512005980632641226298431130513637640125399673697368934008374907832728004469350033174207285393191694692228748281256956917290437627249889472471749973975591415828107248775449619403563269856991145789325659736854030396401772371148983463743700921913930643887223704115714270634525795771407138067936125866995910432010323584269926871467482064993332990516534083898654487467161183876470821163254662352951613205371404232685831299594035879 e = 65537 ciphertext = 2087465275374927411696643073934443161977332564784688452208874207586196343901447373283939960111955963073429256266959192725814591103495590654238320816453299972810032321690243148092328690893438620034168359613530005646388116690482999620292746246472545500537029353066218068261278475470490922381998208396008297649151265515949490058859271855915806534872788601506545082508028917211992107642670108678400276555889198472686479168292281830557272701569298806067439923555717602352224216701010790924698838402522493324695403237985441044135894549709670322380450 """
from Crypto.Util.number import * import itertools import gmpy2
n = 2160489795493918825870689458820648828073650907916827108594219132976202835249425984494778310568338106260399032800745421512005980632641226298431130513637640125399673697368934008374907832728004469350033174207285393191694692228748281256956917290437627249889472471749973975591415828107248775449619403563269856991145789325659736854030396401772371148983463743700921913930643887223704115714270634525795771407138067936125866995910432010323584269926871467482064993332990516534083898654487467161183876470821163254662352951613205371404232685831299594035879 e = 65537 ciphertext = 2087465275374927411696643073934443161977332564784688452208874207586196343901447373283939960111955963073429256266959192725814591103495590654238320816453299972810032321690243148092328690893438620034168359613530005646388116690482999620292746246472545500537029353066218068261278475470490922381998208396008297649151265515949490058859271855915806534872788601506545082508028917211992107642670108678400276555889198472686479168292281830557272701569298806067439923555717602352224216701010790924698838402522493324695403237985441044135894549709670322380450
deffindp(p,q): l = len(p) if l == 272and n % int(p) == 0: q = n // int(p) print(f"p = {p}") print(f"q = {q}") else: table = ["2","7"] for i,j in itertools.product(table,repeat=2): pp = int(i + p) qq = int(j + q) if pp * qq % 10**l == n % 10**l: findp(i+p,j+q) # findp("7","7") p = 27777727727777722777277277777272772727772722777777277777277772227772772772227272727777772772772727227772777277727777777222777777277727772272272777772722727722777727277727727777777772772777772777277772222727227777777222727777772772272727777222777777277777727272772272272277 q = 77777772777772222722727222227777272777772777772277727772722777777777227277727272772727277277272772727227272772772222277777772727727772722772777272727777272722772777277777277777277777727777277277777277777272772772777777727772727277727777772772777772272772272222227777777227 d = gmpy2.invert(e,(p-1)*(q-1)) m = pow(ciphertext,d,n) print(long_to_bytes(m)) # osu{h4v3_y0u_3v3r_n0t1c3d_th4t_727_1s_pr1m3?}
from Crypto.Util.number import getPrime # https://pypi.org/project/pycryptodome/ from Crypto.Cipher import AES from Crypto.Util.Padding import pad from random import randrange from math import floor
deflcg(s, a, b, p): return (a * s + b) % p
p = getPrime(floor(72.7)) a = randrange(0, p) b = randrange(0, p) seed = randrange(0, p) print(f"{p = }") print(f"{a = }") print(f"{b = }")
defget_roll(): global seed seed = lcg(seed, a, b, p) return seed % 100 out = [] for _ inrange(floor(72.7)): out.append(get_roll()) print(f"{out = }")
flag = open("flag.txt", "rb").read() key = bytes([get_roll() for _ inrange(16)]) iv = bytes([get_roll() for _ inrange(16)]) cipher = AES.new(key, AES.MODE_CBC, iv) print(cipher.encrypt(pad(flag, 16)).hex())
w = 1007766898498955907869786015006414110177963571599690507305616866798367726133772947904112344473803352290475298324967917118497719002314168582330651485077694557008496465717884047202330407870172675541471666596768873677342429660840647109788577728613362048023960188394853332768656214046219781162891725391990618222498709603165658933818803574737
a = (w-1) // 2 b = (w+1) // 2 jl = a - 2024
s = jl.nth_root(2) print(f"s = {s}")
sf = divisors(s)
yz = []
for i inrange(len(sf)): t = sf[i] + 1 # 这个就是所有y,z的可能 if t > 2^200: break if t > 2^199and t < 2^200and is_prime(t): yz.append(t)
for i inrange(len(yz)): for j inrange(i+1, len(yz)): x = long_to_bytes(s // ((yz[i]-1)*(yz[j]-1)) + 1) if x.isascii(): print(x) # HSCCTF{math_is_good}
# # sage # from Crypto.Util.number import * from flag import flag,inc flag.startwith(b'flag{')
""" ===================== Very very very easy rsa!!! ===================== Rsa public key cryptosystem has been use for many years, in which the security of large prime number generation method has also been tested, the following is one of the typical,can you complete this simple chal- -lenge? ====================================================================== """
defmy_own_p(self): while1: x = getRandomNBitInteger(1020) Try = self.my_own_number*x**2 + 1 if self.Assert(Try): if isPrime(Try//4): return Try//4
defencrypt(self,p,m): q = getPrime(2048) n = p*q e = 65537 c = pow(m,e,n) return c,n
defAssert(self,x): if x%4 == 0: returnTrue else: returnFalse
if __name__ == '__main__': m = bytes_to_long(flag) # (inc^3+79*inc^2+65*x+13) % 1009 == 75 and inc is the least number of the solutions simple_rsa = Enc_RSA(inc) my_own_p = simple_rsa.my_own_p() c,n = simple_rsa.encrypt(my_own_p,m)
print('The number n is {}'.format(n)) print('The number c is {}'.format(c))
n = 239991661594255432253524767015995553515237552533155266926256011465282762559353011052450949268256733652728779258766468919544696925923029971358451218605372587362458449054025033701801900645898472588154917088906800582917494664328520686255219374705615093349028400487500700497262125962699907814762260694512419306655681221550404511187947035240862982211439747534964306636253099170338798685973287325340707860219110341961583076710007409917903130974964116916771142272349466429861637432682323525150403022509642843376610181395841148978693337599615406649799191853827010907802086292506660315192889149853860466713408351169716170349941283866327264263674186111821195117718331162019362457552641053820550856338437084718718166335155055467914441669571434953065100946319980517952599059131363048098350727744633605485707572274095622988175888575181926216657671979400539900036737074022439888766895137513405183084782345787084384440219108285918795208147597105329833220778335653967184986454715474901645149893734487589920817923864154513632372040497339658475506015077106021525958925682144048668278652340622951676339892584652575698271269136840455952742490194969320271826016057595443197409385545226358578330860265774991784277066549276529111906273907674552737416753863 c = 109418524185171249754036328878536082517954029920067574655738384592535836773335628458417141595917825776593787435093397689973609739515369740356902095262586192608015183792599638006673560604912289524063947742864265648290222544146026010834512839441651151683977393085931597793493789773796447230096442592944882642030190816279289547021784085222443501471419720621671097963713920969317609128950658009064968297525559501068818689604481823678151138300279455535521354912993468606798440152484970180922154575782801057685058825930140799668236228757680911175511728786376925291100386181042980092849556078128905911704118673386991731613513557730970187099491096161742557673136761987701380023132477428777616821281546394707593721174087541309042519043802756642438656462900040566266286487497200417682129927549586193679630211442035605657360570424712008827694002996251982742270642787724129846345798013831392882500535407597060962835644676708823777282733770509418807720840903933760203606859546641772415565269905905400875432944204469576245094963764264460475437038261117576528119870956293781467399036647466377281634948738629392548705519595154811629053243321519324745857848358773804108513698662614500352057759607896553293314291425723185332246973550692552395028420164
首先需要关注的是 # (inc^3+79*inc^2+65*x+13) % 1009 == 75 and inc is the least number of the solutions
defQinverse (Hx, a, N): r,s,t = xgcd(a.lift(), Hx, N) if (s,t) == (None, None): res = r, 0 else: rinv = r[0]^(-1) res = 1, s * rinv return res
defCMfactor(D, N): Hx = hilbert_class_polynomial(-D) res = FactorRes() ZN = Integers(N) R.<x> = PolynomialRing(ZN) Hx = R(Hx) Q.<j> = QuotientRing(R, R.ideal(Hx)) gcd, inverse = Qinverse(Hx, 1728 - j, N) if gcd == 1: a = Q(j * inverse) return CMfactor_core(N, a, Q, ZN, Hx, res)
defCMfactor_core(N, a, Q, ZN, Hx, res): for c in [1..10]: E = EllipticCurve(Q, [0, 0, 0, 3 * a * c ^ 2, 2 * a * c ^ 3]) for u in [1..10]: rand_elem = ZN.random_element() res.rand_elem = int(rand_elem) w = E.division_polynomial(N, Q(rand_elem), two_torsion_multiplicity=0) poly_gcd = xgcd(w.lift(), Hx, N)[0] r = gcd(ZZ(poly_gcd), N) res.c = c res.u = u if r > 1and r != N: return r, N//r
defmain(): sys.setrecursionlimit(50000) #####################################INPUTS#################################### d = 427 n = 239991661594255432253524767015995553515237552533155266926256011465282762559353011052450949268256733652728779258766468919544696925923029971358451218605372587362458449054025033701801900645898472588154917088906800582917494664328520686255219374705615093349028400487500700497262125962699907814762260694512419306655681221550404511187947035240862982211439747534964306636253099170338798685973287325340707860219110341961583076710007409917903130974964116916771142272349466429861637432682323525150403022509642843376610181395841148978693337599615406649799191853827010907802086292506660315192889149853860466713408351169716170349941283866327264263674186111821195117718331162019362457552641053820550856338437084718718166335155055467914441669571434953065100946319980517952599059131363048098350727744633605485707572274095622988175888575181926216657671979400539900036737074022439888766895137513405183084782345787084384440219108285918795208147597105329833220778335653967184986454715474901645149893734487589920817923864154513632372040497339658475506015077106021525958925682144048668278652340622951676339892584652575698271269136840455952742490194969320271826016057595443197409385545226358578330860265774991784277066549276529111906273907674552737416753863 c = 109418524185171249754036328878536082517954029920067574655738384592535836773335628458417141595917825776593787435093397689973609739515369740356902095262586192608015183792599638006673560604912289524063947742864265648290222544146026010834512839441651151683977393085931597793493789773796447230096442592944882642030190816279289547021784085222443501471419720621671097963713920969317609128950658009064968297525559501068818689604481823678151138300279455535521354912993468606798440152484970180922154575782801057685058825930140799668236228757680911175511728786376925291100386181042980092849556078128905911704118673386991731613513557730970187099491096161742557673136761987701380023132477428777616821281546394707593721174087541309042519043802756642438656462900040566266286487497200417682129927549586193679630211442035605657360570424712008827694002996251982742270642787724129846345798013831392882500535407597060962835644676708823777282733770509418807720840903933760203606859546641772415565269905905400875432944204469576245094963764264460475437038261117576528119870956293781467399036647466377281634948738629392548705519595154811629053243321519324745857848358773804108513698662614500352057759607896553293314291425723185332246973550692552395028420164 e = 65537 #####################################INPUTS####################################