from Crypto.Util.number import * from gmpy2 import * p = q = e = 2 c = n = p*q defrabin(c): mp = pow(c, (p + 1) // 4, p) mq = pow(c, (q + 1) // 4, q) yp = inverse(p,q) yq = inverse(q,p) a = (yp * p * mq + yq * q * mp) % n b = n - int(a) c = (yp * p * mq - yq * q * mp) % n d = n - int(c) aa = [a, b, c, d] for i in aa: print(i) print(long_to_bytes(i)) m = rabin()
from sage.allimport * from Crypto.Util.number import * from sympy.ntheory.modular import crt n = e = c = p = q = phi = (p-1)*(q-1) g = gcd(e, phi) e_prime = e // g d_prime = inverse_mod(e_prime, phi)
t = pow(c, d_prime, n) Rp = Zmod(p) tp = Rp(t) roots_p = tp.nth_root(g, all=True) # 返回所有根(列表)
Rq = Zmod(q) tq = Rq(t) roots_q = tq.nth_root(g, all=True) for rp in roots_p: for rq in roots_q: m = crt(int(rp), int(rq), p, q) ifpow(m, e, n) == c: print(long_to_bytes(m)) break
from Crypto.Util.number import * from sympy.ntheory.modular import crt import gmpy2 p= q= n= c= e= phi=(p-1)*(q-1) gcd=gmpy2.gcd(e,phi) d=inverse(e//gcd,phi)
R.<x> = PolynomialRing(Zmod(p)) f = x^gcd - c res1 = f.roots() R.<x> = PolynomialRing(Zmod(q)) f = x^gcd - c res2 = f.roots()
for i in res1: for j in res2: m = crt([p,q],[int(i),int(j)]) if m isnotNone: try: print(long_to_bytes(int(pow(m[0],d,n))).decode()) except Exception as e: continue
for i in res1: for j in res2: m = crt([p,q],[int(i),int(j)]) if m isnotNone: try: print(long_to_bytes(int(pow(m[0],d,n))).decode()) except Exception as e: continue
import gmpy2 from Crypto.Util.number import * from sympy.ntheory.modular import crt import time import random from tqdm import tqdm p= e= c=
defAMM(o, r, q): start = time.time() print('\n----------------------------------------------------------------------------------') print('Start to run Adleman-Manders-Miller Root Extraction Method') print('Try to find one {:#x}th root of {} modulo {}'.format(r, o, q)) g = GF(q) o = g(o) p = g(random.randint(1, q)) while p ^ ((q-1) // r) == 1: p = g(random.randint(1, q)) print('[+] Find p:{}'.format(p)) t = 0 s = q - 1 while s % r == 0: t += 1 s = s // r print('[+] Find s:{}, t:{}'.format(s, t)) k = 1 while (k * s + 1) % r != 0: k += 1 alp = (k * s + 1) // r print('[+] Find alp:{}'.format(alp)) a = p ^ (r**(t-1) * s) b = o ^ (r*alp - 1) c = p ^ s h = 1 for i inrange(1, t): d = b ^ (r^(t-1-i)) if d == 1: j = 0 else: print('[+] Calculating DLP...') j = - discrete_log(a, d) print('[+] Finish DLP...') b = b * (c^r)^j h = h * c^j c = c ^ r result = o^alp * h end = time.time() print("Finished in {} seconds.".format(end - start)) print('Find one solution: {}'.format(result)) return result deffindAllPRoot(p, e): print("Start to find all the Primitive {:#x}th root of 1 modulo {}.".format(e, p)) start = time.time() proot = set() whilelen(proot) < e: proot.add(pow(random.randint(2, p-1), (p-1)//e, p)) end = time.time() print("Finished in {} seconds.".format(end - start)) return proot
deffindAllSolutions(mp, proot, cp, p): print("Start to find all the {:#x}th root of {} modulo {}.".format(e, cp, p)) start = time.time() all_mp = set() for root in proot: mp2 = mp * root % p assert(pow(mp2, e, p) == cp) all_mp.add(mp2) end = time.time() print("Finished in {} seconds.".format(end - start)) return all_mp
for i in res1: for j in res2: for k in res3: for l in res4: for m in res5: m_list=[int(i[0]),int(j[0]),int(k[0]),int(l[0]),int(m[0])] a_list=[p,q,r,s,t] solve=CRT_list(m_list,a_list) flag=long_to_bytes(solve) ifb'ctf'in flag: print(flag)
2、e|p-1
1 2 3 4 5
e = 1801 c = pow(m,e,p)
c =821562155714228494350968286343241874202753771452745916900616612053610190986294297934462409534126095213198464996196364868528238538372119009517541428785632007137206972918081643841690069171088425923887930051635578719252415693144672179185417101210954906623326286804995637775062840407550493095027500638719998 p =19897846550210846565807788524492364050901480736489979129040638436463635149815428186161001280958415730930156556581274966745574164608778242980049611665461488306439665507971670397595035647317930606555771720849158745264269952668944940061576328219674721623208805067371087817766416300084129945316973502412996143
from Crypto.Util.number import * import gmpy2 import time import random from tqdm import tqdm
e = 1801 c = 821562155714228494350968286343241874202753771452745916900616612053610190986294297934462409534126095213198464996196364868528238538372119009517541428785632007137206972918081643841690069171088425923887930051635578719252415693144672179185417101210954906623326286804995637775062840407550493095027500638719998 p = 19897846550210846565807788524492364050901480736489979129040638436463635149815428186161001280958415730930156556581274966745574164608778242980049611665461488306439665507971670397595035647317930606555771720849158745264269952668944940061576328219674721623208805067371087817766416300084129945316973502412996143
defAMM(o, r, q): start = time.time() print('\n----------------------------------------------------------------------------------') print('Start to run Adleman-Manders-Miller Root Extraction Method') print('Try to find one {:#x}th root of {} modulo {}'.format(r, o, q)) g = GF(q) o = g(o) p = g(random.randint(1, q)) while p ^ ((q-1) // r) == 1: p = g(random.randint(1, q)) print('[+] Find p:{}'.format(p)) t = 0 s = q - 1 while s % r == 0: t += 1 s = s // r print('[+] Find s:{}, t:{}'.format(s, t)) k = 1 while (k * s + 1) % r != 0: k += 1 alp = (k * s + 1) // r print('[+] Find alp:{}'.format(alp)) a = p ^ (r**(t-1) * s) b = o ^ (r*alp - 1) c = p ^ s h = 1 for i inrange(1, t): d = b ^ (r^(t-1-i)) if d == 1: j = 0 else: print('[+] Calculating DLP...') j = - discrete_log(a, d) print('[+] Finish DLP...') b = b * (c^r)^j h = h * c^j c = c ^ r result = o^alp * h end = time.time() print("Finished in {} seconds.".format(end - start)) print('Find one solution: {}'.format(result)) return result deffindAllPRoot(p, e): print("Start to find all the Primitive {:#x}th root of 1 modulo {}.".format(e, p)) start = time.time() proot = set() whilelen(proot) < e: proot.add(pow(random.randint(2, p-1), (p-1)//e, p)) end = time.time() print("Finished in {} seconds.".format(end - start)) return proot
deffindAllSolutions(mp, proot, cp, p): print("Start to find all the {:#x}th root of {} modulo {}.".format(e, cp, p)) start = time.time() all_mp = set() for root in proot: mp2 = mp * root % p assert(pow(mp2, e, p) == cp) all_mp.add(mp2) end = time.time() print("Finished in {} seconds.".format(end - start)) return all_mp
for i in mps: flag = long_to_bytes(int(i)) ifb'flag'in flag: print(flag) # flag{Enj01_m1sc_A0d_cr0}
法二e阶子群
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from Crypto.Util.number import *
e = 1801 c = 821562155714228494350968286343241874202753771452745916900616612053610190986294297934462409534126095213198464996196364868528238538372119009517541428785632007137206972918081643841690069171088425923887930051635578719252415693144672179185417101210954906623326286804995637775062840407550493095027500638719998 p = 19897846550210846565807788524492364050901480736489979129040638436463635149815428186161001280958415730930156556581274966745574164608778242980049611665461488306439665507971670397595035647317930606555771720849158745264269952668944940061576328219674721623208805067371087817766416300084129945316973502412996143