from secret import flag from Crypto.Util.number import* from gmpy2 import*
flag = b'D0g3xGC{****************}'
defgen_key(p, q): public_key = p*p*q e = public_key n = p*q phi_n = (p-1)*(q-1) private_key = inverse(e,phi_n) return public_key,private_key,e
p = getPrime(512) q = getPrime(512)
N,d,e = gen_key(p,q)
c = gmpy2.powmod(bytes_to_long(flag),e,N)
print(N) print(d) print(c)
''' n = 539403894871945779827202174061302970341082455928364137444962844359039924160163196863639732747261316352083923762760392277536591121706270680734175544093484423564223679628430671167864783270170316881238613070741410367403388936640139281272357761773388084534717028640788227350254140821128908338938211038299089224967666902522698905762169859839320277939509727532793553875254243396522340305880944219886874086251872580220405893975158782585205038779055706441633392356197489 d = 58169755386408729394668831947856757060407423126014928705447058468355548861569452522734305188388017764321018770435192767746145932739423507387500606563617116764196418533748380893094448060562081543927295828007016873588530479985728135015510171217414380395169021607415979109815455365309760152218352878885075237009 c = 82363935080688828403687816407414245190197520763274791336321809938555352729292372511750720874636733170318783864904860402219217916275532026726988967173244517058861515301795651235356589935260088896862597321759820481288634232602161279508285376396160040216717452399727353343286840178630019331762024227868572613111538565515895048015318352044475799556833174329418774012639769680007774968870455333386419199820213165698948819857171366903857477182306178673924861370469175 '''
N = 539403894871945779827202174061302970341082455928364137444962844359039924160163196863639732747261316352083923762760392277536591121706270680734175544093484423564223679628430671167864783270170316881238613070741410367403388936640139281272357761773388084534717028640788227350254140821128908338938211038299089224967666902522698905762169859839320277939509727532793553875254243396522340305880944219886874086251872580220405893975158782585205038779055706441633392356197489 d = 58169755386408729394668831947856757060407423126014928705447058468355548861569452522734305188388017764321018770435192767746145932739423507387500606563617116764196418533748380893094448060562081543927295828007016873588530479985728135015510171217414380395169021607415979109815455365309760152218352878885075237009 c = 82363935080688828403687816407414245190197520763274791336321809938555352729292372511750720874636733170318783864904860402219217916275532026726988967173244517058861515301795651235356589935260088896862597321759820481288634232602161279508285376396160040216717452399727353343286840178630019331762024227868572613111538565515895048015318352044475799556833174329418774012639769680007774968870455333386419199820213165698948819857171366903857477182306178673924861370469175 pq = GCD(pow(2, d*N, N)-2, N) flag = long_to_bytes(pow(c, d, pq)) print(flag)
EZ_sign
from Crypto.Util.number import * from gmpy2 import * from hashlib import* import random,os
flag = b'D0g3xGC{***************}' msg = b'e = ?'
defsign(pub, pri, k): (p,q,g,y) = pub x = pri r = int(powmod(g, k, p) % q) H = bytes_to_long(sha1(os.urandom(20)).digest()) s = int((H + r * x) * invert(k, q) % q) return (H,r,s)
k1 = getPrime(64) k2 = k1 ** 2 pri = bytes_to_long(msg) a = 149328490045436942604988875802116489621328828898285420947715311349436861817490291824444921097051302371708542907256342876547658101870212721747647670430302669064864905380294108258544172347364992433926644937979367545128905469215614628012983692577094048505556341118385280805187867314256525730071844236934151633203 b = 829396411171540475587755762866203184101195238207 g = 87036604306839610565326489540582721363203007549199721259441400754982765368067012246281187432501490614633302696667034188357108387643921907247964850741525797183732941221335215366182266284004953589251764575162228404140768536534167491117433689878845912406615227673100755350290475167413701005196853054828541680397 y = 97644672217092534422903769459190836176879315123054001151977789291649564201120414036287557280431608390741595834467632108397663276781265601024889217654490419259208919898180195586714790127650244788782155032615116944102113736041131315531765220891253274685646444667344472175149252120261958868249193192444916098238
pub = (a, b, g, y)
H1, r1, s1 = sign(pub, pri, k1)
H2, r2, s2 = sign(pub, pri, k2)
p = getPrime(128) q = getPrime(128) n = p * q c = powmod(bytes_to_long(flag), e, n)
# sage from Crypto.Util.number import * from sympy.solvers.diophantine.diophantine import cornacchia
p = 149328490045436942604988875802116489621328828898285420947715311349436861817490291824444921097051302371708542907256342876547658101870212721747647670430302669064864905380294108258544172347364992433926644937979367545128905469215614628012983692577094048505556341118385280805187867314256525730071844236934151633203 q = 829396411171540475587755762866203184101195238207 g = 87036604306839610565326489540582721363203007549199721259441400754982765368067012246281187432501490614633302696667034188357108387643921907247964850741525797183732941221335215366182266284004953589251764575162228404140768536534167491117433689878845912406615227673100755350290475167413701005196853054828541680397 y = 97644672217092534422903769459190836176879315123054001151977789291649564201120414036287557280431608390741595834467632108397663276781265601024889217654490419259208919898180195586714790127650244788782155032615116944102113736041131315531765220891253274685646444667344472175149252120261958868249193192444916098238
k = int(f.roots()[1][0]) x = (s1*k - H1) * inverse(r1, q) % q print(long_to_bytes(x).decode()) c = 18947793008364154366082991046877977562448549186943043756326365751169362247521 C = 179093209181929149953346613617854206675976823277412565868079070299728290913658 x1 = cornacchia(1, 1, C) for a, b in x1: assert a**2+b**2 == C if isPrime(a) and isPrime(b): p, q = a, b print(p,q) n = p*q phi = (p-1)*(q-1) d = inverse(e, phi) print(long_to_bytes(pow(c, d, n)).decode())
from Crypto.Util.number import getPrime, isPrime, bytes_to_long from secret import flag import base64
defemirp(x): y = 0 while x !=0: y = y*10 + x%10 x = x//10 return y
whileTrue: p = getPrime(512) q = emirp(p) if isPrime(q): break n = p*q e = 65537
flag = base64.b64encode(flag)
m = bytes_to_long(flag)
c = pow(m,e,n) print(f"{n = }") print(f"{c = }")
''' n = 404647938065363927581436797059920217726808592032894907516792959730610309231807721432452916075249512425255272010683662156287639951458857927130814934886426437345595825614662468173297926187946521587383884561536234303887166938763945988155320294755695229129209227291017751192918550531251138235455644646249817136993 c = 365683379886722889532600303686680978443674067781851827634350197114193449886360409198931986483197030101273917834823409997256928872225094802167525677723275059148476025160768252077264285289388640035034637732158021710365512158554924957332812612377993122491979204310133332259340515767896224408367368108253503373778 '''
import base64 from Crypto.Util.number import * n = 404647938065363927581436797059920217726808592032894907516792959730610309231807721432452916075249512425255272010683662156287639951458857927130814934886426437345595825614662468173297926187946521587383884561536234303887166938763945988155320294755695229129209227291017751192918550531251138235455644646249817136993 c = 365683379886722889532600303686680978443674067781851827634350197114193449886360409198931986483197030101273917834823409997256928872225094802167525677723275059148476025160768252077264285289388640035034637732158021710365512158554924957332812612377993122491979204310133332259340515767896224408367368108253503373778
deft(a, b, k): # sqrt(n) has 155 digits, so we need to figure out 77 digits on each side if k == 77: if a*b == n: print(a, b) return for i inrange(10): for j inrange(10): # we try to guess the last not-already-guessed digits of both primes a1 = a + i*(10**k) + j*(10**(154-k)) b1 = b + j*(10**k) + i*(10**(154-k)) if a1*b1 > n: # a1 and b1 are too large continue if (a1+(10**(154-k)))*(b1+(10**(154-k))) < n: # a1 and b1 are too small continue if ((a1*b1) % (10**(k+1))) != (n % (10**(k+1))): # The last digits of a1*b1 (which won't change later) doesn't match n continue # this a1 and b1 seem to be a possible match, try to guess remaining digits t(a1, b1, k+1)
# the primes have odd number of digits (155), so we try all possible middle digits (it simplifies the code) for i inrange(10): t(i*(10**77), i*(10**77), 0) p, q = 39316409865082827891559777929907275271727781922450971403181273772573121561800306699150395758615464222134092274991810028405823897933152302724628919678029201, 10292087691982642720325133979832850482001819947229043122246451685759305199660300816512137527737218130417905422918772717257270992977795519872828056890461393 print(p*q == n) e = 65537 d = inverse(e, (p-1)*(q-1)) flag = long_to_bytes(pow(c, d, n)).decode() print(base64.b64decode(flag).decode())
# sage from sage.allimport * from sage.parallel.multiprocessing_sage import parallel_iter # TODO import itertools from tqdm import tqdm from Crypto.Util.number import * import string
defnth_p(y, n, p, k=1): assert is_pseudoprime(p) print('[LOG] Solving pi = %s^%d' % (hex(p), k)) try: xs = Zmod(p**k)(y).nth_root(n, all=True) except: xs = GF(p**k)(y).nth_root(n, all=True) xs = list(set(xs)) xs = [Integer(x) for x in xs] return xs
defnthRSA_p(c, e, p, k=1): assert is_pseudoprime(p) P = Integer(pow(p, k)) phi = euler_phi(P)
rs = [] ei = e whileTrue: r = gcd(phi, ei) if r == 1: break rs += [r] ei //= r r = product(rs) dr = (e // r).inverse_mod(phi) cr = pow(c, dr, P) return nth_p(cr, r, p, k)
defnthRSA_n(c, e, ps, ks=None, checker=None, ret1=False): # ps: p, q, ... assertisinstance(ps, list) if ks == None: ks = [1] * len(ps) else: assertlen(ps) == len(ks) ms = [] for i inrange(len(ps)): mp = nthRSA_p(c, e, ps[i], ks[i]) ms += [mp] total = product([len(x) for x in ms]) print('[Log] Doing crt.\nComplexity = %d: %s' % (total, str([len(x) for x in ms])))
res = [] Ps = [ps[i]**ks[i] for i inrange(len(ps))] for msi in tqdm(itertools.product(*ms), total=total): m = crt(list(msi), Ps) if checker == None: res += [m] continue if checker(m): ifnot ret1: res += [m] continue return m return res
defgenHeaderChecker(hd): ifisinstance(hd, str): hd = hd.encode() assertisinstance(hd, bytes) defcheckHeader(m): try: flag = long_to_bytes(int(m)) if hd in flag: print(flag) returnTrue returnFalse except: returnFalse return checkHeader
defgenStrChecker(dict, n=65537): defcheckStr(m): try: flag = long_to_bytes(int(m)).decode() for fi in flag[:n]: ifnot fi indict: returnFalse print(flag) returnTrue except: returnFalse return checkStr
n = 211174039496861685759253930135194075344490160159278597570478160714793843648384778026214533259531963057737358092962077790023796805017455012885781079402008604439036453706912819711606916173828620000813663524065796636039272173716362247511054616756763830945978879273812551204996912252317081836281439680223663883250992957309172746671265758427396929152878633033380299036765665530677963287445843653357154379447802151146728382517702550201 c = 191928992610587693825282781627928404831411364407297375816921425636703444790996279718679090695773598752804431891678976685083991392082287393228730341768083530729456781668626228660243400914135691435374881498580469432290771039798758412160073826112909167507868640830965603769520664582121780979767127925146139051005022993085473836213944491149411881673257628267851773377966008999511673741955131386600993547529438576918914852633139878066 p = 31160882390461311665815471693453819123352546432384109928704874241292707178454748381602275005604671000436222741183159072136366212086549437801626015758789167455043851748560416003501637268653712148286072544482747238223 q = 6776895366785389188349778634427547683984792095011326393872759455291221057085426285502176493658280343252730331506803173791893339840460125807960788857396637337440004750209164671124188980183308151635629356496128717687 e = 1009*7 ps = [p, q] checker = genHeaderChecker('HECTF') res = nthRSA_n(c, e, ps, checker=checker) for r in res: flag = long_to_bytes(int(r)) print(flag)
不合格的魔药(unsolved)
from random import randrange from Crypto.Util.number import * from gmpy2 import * from fastecdsa.curve import Curve from Crypto.Cipher import AES from hashlib import * import secret
defA_function(key): whileTrue: p = getPrime(512) q = getPrime(512) if p % 4 == 3and q % 4 == 3: n = p * q break a = randrange(p) b = randrange(q) whileTrue: x1 = randrange(p) y1 = (x1**3 + a*x1) % p x2 = randrange(n) y2 = (x2**3 + a*x2 + b) % n ifpow(y1,(p-1)//2, p) == 1andpow(y1,(q-1)//2, q) == 1: yp = pow(y1,p//4+1,p) yq = pow(y1,q//4+1,q) _,s,t = gcdext(p,q) y1 = (s*p*yq + t*q*yp) % p ifpow(y2,(p-1)//2, p) == 1andpow(y2,(q-1)//2, q) == 1: yp = pow(y2,p//4+1,p) yq = pow(y2,q//4+1,q) _,s,t = gcdext(p,q) y2 = (s*p*yq + t*q*yp) % n break Ep = Curve(None, p, a, 0, None, x1, y1) En = Curve(None, n, a, b, None, x2, y2) print("p =", p) print("q =", q) print("a =", a) print("b =", b) hint1 = key * Ep.G hint = [x1 ,hint1] return En, hint
defA_encrypt(m, G, k): blocks = [m[16*i:16*(i+1)] for i inrange(len(m) // 16)] ciph = [] aes = AES.new(k, AES.MODE_ECB) for i inrange(len(blocks)): c = aes.encrypt(blocks[i]) if i%2==0: G = G + G ciph.append(G.x ^ bytes_to_long(c)) else: ciph.append(G.y ^ bytes_to_long(c)) return ciph
flag = secret.flag key = secret.key
m = flag[6:-1] assertb'HECTF{'+ m+ b'}' == flag A, hint = A_function(key) k = md5(long_to_bytes(key)).hexdigest().encode() c = A_encrypt(m, A.G,k) print("hint =", hint) print("c =", c)
''' p = 9604080254440553624043823039323876524034439909584709693304859297324410855942111467832096190746534800378359779991381701244554754870303658957438266614583487 q = 7117529167860499983120234872664469946810713755399747931099511148595647881645694071900284496403308583631053530870961375928947111857317803005696543076720079 a = 4681007517868949260473646867708411042804596292653498068045093108939357065240201843535644313612886376810286247810943227474659270191834401055704514648846995 b = 5604862515726338933576748414825616582947323501967288114322080747741801017833194347273532400730033226601964489467416955741018175785792514035352083708135431 hint = [5544706922427110224110125906620053049906095568886481576326706308027915868515721429471522223193053363494813044921519216114372968191072598748704528735817403, X: 0x2fa8e23f18ed4a9bd752a0c22b0750c17fbb66c76554e2089258fd979a5736b7766c974fb9788acf17fb065dc1daec6a8a6e98021de6c4ce3cde11dd54590e1d Y: 0xa3ce4bb1e25563b577a45cd06153d2dab584a70130c7ae71e65fe5e11b60493ccb845fbe4989dbd4a60d6a1ff12baa268b8833ed30f7c7e21c32268a139b5b6b (On curve <None>)] c = [36780810764729391947601691590378765170863850291763672158886689602006275675399596108959250284869355070618680265311484525337488013177333417742808496794250706127014303883956401715343247310936978778751394980638177344654524711571648231122027699452582302505466999915200896495338587961829985149664712686944510559820, 20958199004445348755624931477686903609410629089817702686793041731031202915294487428236505796231417377524290926704880107242252471250791747709149963693453815320856114055076830778689575609444155241642860745570792018879816650383543271943138193405548674967958109800776284787612370057476837642989670234913968669332, 19758181515666300263334531148587391869707566215385658759724970483060039216682585723722462835458856503531814316860237786892749700501436669071048571605926728917066797641628644730857333648930286503355701843365288276242984029888215453858844295912023305616753086127934173496355853797241944921600781294012353332277, 45576628433681427718167093217006549620067042472164439269014690121698560736312716407875326404496263261341269644373184438703912129559084380247641072914940830606649124606611794031719696797961847217643536070335745057048220615012019629278484208808353027070994021979997462190775853832457224157083880895894000484461] '''