from sage.allimport * from Crypto.Util.number import * import random
defhensel_lifting(m, c, e, p, k): """ Require pow(m, e, p) == c % p Get m % (p ** k) """ assertpow(m, e, p) == int(c % p) mk = int(m % p) t = int(m * pow(c, -1, p) % p) pk = int(p) for _ inrange(1, k): pk *= p x = int((c - pow(mk, e, pk)) % pk) y = int((x * t * int(pow(e, -1, p))) % pk) mk = mk + y return mk
defgenkey(pbits: int, ebits: int, r: int, coprime: bool = True): r""" Generate a pair public key (n, e) and private key (p, q, d) where $n=p^rq, ed=1\pmod{\varphi}$ Param: pbits - p bits and q bits, where p and q is balanced ebits - e bits r - power of p Return: (n, e) """ p = getPrime(pbits) q = getPrime(pbits) phi = p ** r * (p - 1) * q n = p ** r * q e = random.getrandbits(ebits) while coprime and gcd(e, phi) != 1: e = random.getrandbits(ebits) return (n, e), (p, q)
defencrypt(m, e, n): returnpow(m, e, n)
defdecrypt(c, e, p, q, r): dp = pow(e, -1, p - 1) dq = pow(e, -1, q - 1) mp = pow(c, dp, p) mq = pow(c, dq, q) mpr = hensel_lifting(mp, c, e, p, r) m = crt([mpr, mq], [p ** r, q]) returnint(m)
pbits = 128 ebits = 64 r = 5 (n, e), (p, q) = genkey(128, 64, r) m = 1234567890123456789012345678901234567890 c = encrypt(m, e, n) _m = decrypt(c, e, p, q, r) print(f'{_m = }')
N=539779851369541956878655738599584730199799866957191805784596190682932284216781781433367450841202917758999300635019369629627621029957135109806205877317954671312041249493462048283611940752235036153024920172209763260723728345918562258401803973624430150143563078517485996070862532682695228590709019451174548520135142052216785774589096706631010293690859363524584240662502290912412366366114571976050857239915691266377257797199583543940504695517331512813468837128344612227973709974625418257243011036826241599265375741977853552204640800449679679351666009764297016524814036295707311913711955324055690490892097177271718850857268982130811714517356073266905474635370690445031512184247179039751734276906533177939993769044135143389748416635981226449566039039202521305851567296884751935162651063209779647359922622084851547605090230221057349511482738300221222563908357379545905837110168948295030747460300104202323692732549831403834387939156877086852393515817984772384147449841124275061609701453997579569931391166586163299940486204581696722731952467570857217406030804590055255431828403195798003509083922294733709507134156466158642941338493323430671502043066148246348074878064089651235355282144209668143249348243220714471988019011613749340243917652821 e=8179300978753084587812861894047395225516049110376948812109811319430275614612773726672345893359691900281432484382670047044697374818043512731533402576374645405477207239801498428774783768163880078495448747421425078521981578408638790336528372019271073712013371141939808017049399434858687299480461753638164719404612128939787055797762174745092074547412183349192156638711750872083313795551439465507724807626674514935170104573715458782366469587138508845980490673890245713729782917089910271980557159592807350504157192913530007199510144004848020221181558472160543018733124225266127379373751910439604459368078652499029070936707349862139053913745186413782066470461478961703013591655136140060879250067379283913798867648758171004535775565306842444545755351202796833177560656564652632975685912935281581268141803696686952259539945588609591385807620108279333498170028167338690235117003515264281843953984997958878272347778561933726792473981855755454522886321669676790813189668084373153897754540290867346751033567500922477317530445967753955221454744946208555394588111484610700789566547507402309549957740815535069057837915204852490930168843605732632328017129154852857227895362549146737618906180651623216848500491438142456250653458053922622240299736136335179639180898730269690699965799644757774472147210271111150769048976871249731156387939260749192370361488285775377622944817570292095201906142567403539151179209316853493906909989301225903409448461436855145 b=17623328397444755087284107444487160871617682792372566887446834913712379373851213638071138745775127796589871734472781755930251379295485892067473329763997583502625804363418069062645997342172778252731889437 r = 7 idx = (r*(r-1)) / ((r+1)*(r+1)) P.<x> = PolynomialRing(Zmod(N)) f = e * x - b roots = f.monic().small_roots(beta=idx) print(roots)
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() print("LLL done")
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 []
a = 68823589009570846705623113091430161477799561031575612135516351257937127579444 b = 76549169049080319489163980188997771079750670038002598167961495813084486794567 c = 99215492498952976642031024510129092308042391285395704888838178561670205468882 p = 12775129699668988473759438271274836254349225413222075887429387682336494103348583050672280757042383792640084197832605411237644937815012509935794275313643031
r = 7 N = 209798341155088334158217087474227805455138848036904381404809759100627849272231840321985747935471287990313456209656625928356468120896887536235496490078123448217785939608443507649096688546074968476040552137270080120417769906047001451239544719039212180059396791491281787790213953488743488306241516010351179070869410418232801398578982244984544906579574766534671056023774009163991804748763929626213884208260660722705479782932001102089367261720194650874553305179520889083170973755913964440175393646890791491057655226024046525748177999422035469428780228224800114202385209306803288475439775037067014297973202621118959024226798935588827359265962780792266516120013602384766460619793738405476219362508944225007365127768741191310079985425349292613888185378948854602285379329682053663283534930182589905986063348509703027498270111412063194971956202729807710253369312175636837558252924035002153389909587349043986253518050303628071319876207392440085675892353421232158925122721273720564784886530611286461575045181073744696415657043278123662980166364494583141297996445429477446442693717498789391918530672770193730629928408766563592081857706608049076318165712479742423149330311238462044666384622153280310696667586565906758451118241914402257039981388209
from Crypto.Util.number import bytes_to_long, getPrime from secret import msg from sympy import nextprime from gmpy2 import invert from hashlib import md5
flag = 'd3ctf{'+md5(msg).hexdigest()+'}' p = getPrime(256) q = getPrime(256) assert p > q n = p * q e = 0x10001 m = bytes_to_long(msg) c = pow(m, e, n)
from Crypto.Util.number import * from hashlib import md5 import gmpy2
r = 7
N = 1476751427633071977599571983301151063258376731102955975364111147037204614220376883752032253407881568290520059515340434632858734689439268479399482315506043425541162646523388437842149125178447800616137044219916586942207838674001004007237861470176454543718752182312318068466051713087927370670177514666860822341380494154077020472814706123209865769048722380888175401791873273850281384147394075054950169002165357490796510950852631287689747360436384163758289159710264469722036320819123313773301072777844457895388797742631541101152819089150281489897683508400098693808473542212963868834485233858128220055727804326451310080791
c = 2420624631315473673388732074340410215657378096737020976722603529598864338532404224879219059105950005655100728361198499550862405660043591919681568611707967
x = polygen(Zmod(N)) f = e1 * e2 * x - (e2 - e1) f = f.monic()