defgen_seed(s): i, j, k = 0, len(s), 0 while i < j: k = k + ord(s[i]) i += 1 i = 0 while i < j: if (i % 2) != 0: k = k - (ord(s[i]) * (j - i + 1)) else: k = k + (ord(s[i]) * (j - i + 1)) k = k % 2147483647 i += 1
k = (k * j) % 2147483647 return k
defreseed(s): return s * 214013 + 2531011
defencrypt(s, msg): assert s <= 2**32 c, d = 0, s enc, l = b'', len(msg) while c < l: d = reseed(d) enc += (msg[c] ^ ((d >> 16) & 0xff)).to_bytes(1, 'big') c += 1 return enc
defencrypt(s, msg): assert s <= 2**32 c, d = 0, s enc, l = b'', len(msg) while c < l: d = reseed(d) enc += (msg[c] ^ ((d >> 16) & 0xff)).to_bytes(1, 'big') c += 1 return enc
C = [ 5960650533801939766973431801711817334521794480800845853788489396583576739362531091881299990317357532712965991685855356736023156123272639095501827949743772, 6521307334196962312588683933194431457121496634106944587943458360009084052009954473233805656430247044180398241991916007097053259167347016989949709567530079, 1974144590530162761749719653512492399674271448426179161347522113979158665904709425021321314572814344781742306475435350045259668002944094011342611452228289, 2613994669316609213059728351496129310385706729636898358367479603483933513667486946164472738443484347294444234222189837370548518512002145671578950835894451, 8127380985210701021743355783483366664759506587061015828343032669060653534242331741280215982865084745259496501567264419306697788067646135512747952351628613, 5610271406291656026350079703507496574797593266125358942992954619413518379131260031910808827754539354830563482514244310277292686031300804846114623378588204, 10543, 4, ] c = 80607532565510116966388633842290576008441185412513199071132245517888982730482694498575603226192340250444218146275844981580541820190393565327655055810841864715587561905777565790204415381897361016717820490400344469662479972681922265843907711283466105388820804099348169127917445858990935539611525002789966360469324052731259957798534960845391898385316664884009395500706952606508518095360995300436595374193777531503846662413864377535617876584843281151030183895735511854 O = [1391526622949983, 2848691279889518, 89200900157319, 31337]
e = 3 d = (C[6] * e - O[3]) // C[7]
A = Matrix([[C[0], -C[1], 0], [0, C[2], -C[3]], [-C[5], 0, C[4]]]) U = Matrix([[O[0]], [O[1]], [O[2]]]) p, q, r = [int(i[0]) for i in A.inverse() * U]
n = p * q * r * e * d phi = (p - 1) * (q - 1) * (r - 1) * (e - 1) * (d - 1) E = 0x10001 D = inverse(E, phi) m = pow(c, D, n) print(long_to_bytes(int(m)))
defmul_barak(m, P, E): if P == (0, 0): return P R = (0, 0) while m != 0: if m & 1: R = add_barak(R, P, E) m = m >> 1 if m != 0: P = add_barak(P, P, E) return R
defrand_barak(E): c, d, p = E whileTrue: y = randint(1, p - 1) K = Zmod(p) P.<x> = PolynomialRing(K) f = x**3 - d*x*y + c + y^3 R = f.roots() try: r = R[0][0] return (r, y) except: continue
p = 73997272456239171124655017039956026551127725934222347 d = 68212800478915688445169020404812347140341674954375635 c = 1 E = (c, d, p)
P = rand_barak(E)
FLAG = flag.lstrip(b'CCTF{').rstrip(b'}') m = bytes_to_long(FLAG) assert m < p Q = mul_barak(m, P, E) print(f'P = {P}') print(f'Q = {Q}')
p = 73997272456239171124655017039956026551127725934222347 d = 68212800478915688445169020404812347140341674954375635 c = 1 P = (71451574057642615329217496196104648829170714086074852, 69505051165402823276701818777271117086632959198597714) Q = (40867727924496334272422180051448163594354522440089644, 56052452825146620306694006054673427761687498088402245)
p = 73997272456239171124655017039956026551127725934222347 d = 68212800478915688445169020404812347140341674954375635 c = 1 P = (71451574057642615329217496196104648829170714086074852, 69505051165402823276701818777271117086632959198597714) Q = (40867727924496334272422180051448163594354522440089644, 56052452825146620306694006054673427761687498088402245)
R.<x,y,z> = Zmod(p)[] cubic = x^3 + y^3 + z^3 - d*x*y*z E = EllipticCurve_from_cubic(cubic,morphism=True) P = E(P) Q = E(Q) m = P.discrete_log(Q) order = P.order()
for i inrange(10000): try: flag = long_to_bytes(int(m)).decode() print(flag,i) break except: m += order
for nl in trange(2**7): n = nh + nl * 2 + 1 f = FactorDB(n) f.connect() fs = f.get_factor_list() iflen(fs) == 2: p, q = fs[0], fs[1] if ( len(bin(p)[2:]) == 128 andlen(bin(q)[2:]) == 128 and isPrime(p) and isPrime(q) ): phi = (p - 1) * (q - 1) break
for el in trange(2**7): e = eh + el * 2 + 1 ifnot isPrime(e): continue d = invert(e, phi) for cl inrange(2**8): try: c = ch + cl m = pow(c, d, n) print('CCTF{'+long_to_bytes(m).decode()+'}') except: pass
from Crypto.Util.number import * from secret import m, flag
defgenPrime(m, nbit): assert m >= 2 whileTrue: a = getRandomNBitInteger(nbit // m) r = getRandomNBitInteger(m ** 2 - m + 2) p = a ** m + r if isPrime(p): return (p, r)
defgenkey(m, nbit): p, r = genPrime(m, nbit // 2) q, s = genPrime(m, nbit // 2) n = p * q e = r * s return (e, n)
defencrypt(msg, pkey): e, n = pkey m = bytes_to_long(msg) c = pow(m, e, n) return c
from Crypto.Util.number import * from gmpy2 import * from tqdm import *
e, n = ( 150953688, 373824666550208932851344358703053061405262438259996622188837935528607451817812480600479188884096072016823491996056842120586016323642383543231913508464973502962030059403443181467594283936168384790367731793997013711256520780338341018619858240958105689126133812557401122953030695509876185592917323138313818881164334490044163316692588337720342646339764174333821950301279825316497898035760524780198230860089280791887363472060435443944632303774987556026740232641561706904946900169858650106994947597642013168757868017723456208796677559254390940651802333382820063200360490892131573853635471525711894510477078934343423255983, ) c = 275574424285842306309073814038154403551700455145115884031072340378743712325975683329051874910297915882286569143815715537085387363420246497061870251520240399514896001311724695996978111559476733709139080970977190150345474341853495386364275702356438666152069791355990718058189043717952080875207858163490627801836274404446661613239167700736337269924479349700031535265765885064606399858172168036794462235707003475360358004643720927563261787867952228496769300443415094124132722170498229611285689671203272698693505808912907778910378274197503048226322090611405601517624884408718689404556983397217070272851442351897456769883
for r inrange(1, 2**14): if e % r == 0: s = e // r if (a1**4 + r) * (a2**4 + s) == n: break
p = a1**4 + r q = a2**4 + s
phi = q - 1 e = e // 72 d = invert(e, phi) mb = pow(c,d,q) PR.<x> = Zmod(q)[] f = x ^ 72 - mb res = f.roots() for i in res: try: print(long_to_bytes(int(i[0])).decode()) except: pass
import random import time from tqdm import tqdm from Crypto.Util.number import * # About 3 seconds to run 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(d, a) 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
defonemod(p,r): t=random.randint(2,p) whilepow(t,(p-1)//r,p)==1: t=random.randint(2,p) returnpow(t,(p-1)//r,p) defsolution(p,root,e): whileTrue: g=onemod(p,e) may=[] for i in tqdm(range(e)): may.append(root*pow(g,i,p)%p) iflen(may) == len(set(may)): return may
defsolve_in_subset(ep,p): cp = int(pow(c,inverse(int(e//ep),p-1),p)) com_factors = [] while GCD(ep,p-1) !=1: com_factors.append(GCD(ep,p-1)) ep //= GCD(ep,p-1) com_factors.sort()
cps = [cp] for factor in com_factors: mps = [] for cp in cps: mp = AMM(cp, factor, p) mps += solution(p,mp,factor) cps = mps for each in cps: assertpow(each,e,p)==c%p return cps
p = 24854995563762799317055160315647073592768859410925406616067526817964296709994775588158311030813096922905657553370793515214591086698010302872311633588541111630338981703494212247996116660819640489213219705595382514374022123356637290058228183400682431815794876393612877273757515867990847040787313812864434536969 q = 15040222622096320078383580808680733765955114958694997949647342925417877088612792495485641348591026281373930569798925789027166056695954731923306109646611840570310396750856642056018981080439916663195842593441587057719678555907050674529272376248049062724657792390788687452049496308886252188791975094655675938807 e = 150953688 c = 275574424285842306309073814038154403551700455145115884031072340378743712325975683329051874910297915882286569143815715537085387363420246497061870251520240399514896001311724695996978111559476733709139080970977190150345474341853495386364275702356438666152069791355990718058189043717952080875207858163490627801836274404446661613239167700736337269924479349700031535265765885064606399858172168036794462235707003475360358004643720927563261787867952228496769300443415094124132722170498229611285689671203272698693505808912907778910378274197503048226322090611405601517624884408718689404556983397217070272851442351897456769883
start = time.time() print('Start CRT...') for mpp in m_p: for mqq in m_q: solution = CRT_list([int(mpp), int(mqq)], [p, q]) if solution < 2^800 : # Always the bit_length of flag is less than 800 print(long_to_bytes(solution))
end = time.time() print("Finished in {} seconds.".format(end - start))