defcontinuedFra(x, y): cF = [] while y: cF += [x / y] x, y = y, x % y return cF
defSimplify(ctnf): numerator = 0 denominator = 1 for x in ctnf[::-1]: numerator, denominator = denominator, x * denominator + numerator return (numerator, denominator)
defcalculateFrac(x, y): cF = continuedFra(x, y) cF = map(Simplify, (cF[0:i] for i in xrange(1, len(cF)))) return cF
defsolve_pq(a, b, c): par = gmpy2.isqrt(b * b - 4 * a * c) return (-b + par) / (2 * a), (-b - par) / (2 * a)
defwienerAttack(e, n): for (d, k) in calculateFrac(e, n): if k == 0: continue if (e * d - 1) % k != 0: continue phi = (e * d - 1) / k p, q = solve_pq(1, n - phi + 1, n) if p * q == n: return abs(int(p)), abs(int(q)) print'not find!'
c = 27420976989291704853290942746692975639118638546067791801590979188047771542047787039580073164232184357645332167377536880357294473233587818671582569240436649745729337106688606707116526797597394519249817893131017053447035933993749956812524229683939314908415241390539611886521068601651355848422704542765488436854 n = 69988650330983375636872769052203818680340664553703450665990964469273736786617696120792164947740009300429031158862390894445991532951186399745338277739956748329285117725077102271964774512969566292237059962743570575241572400229054824465463170558697192026957786636086030806458003635105353720825305471780313159973 e = 4306944577640965469025251042732166573657840611335368059812648728269236926628151554925535212692608107002613526156095373259390632045899793249201966433087347582154469798178033266929734684449587674782655126590266569766563017074437592833170157173467749170261972368285470573484819815433505903223073580421828377345
p, q = wienerAttack(e, n) d = gmpy2.invert(e, (p-1) * (q-1)) m = pow(c, d, n) print long_to_bytes(m)
from Crypto.Util.number import * from random import randint flag = int('SCTF{*******************}'.encode('hex'), 16) d = getPrime(randint(380, 385))
for _ in range(3): p = getPrime(512) q = getPrime(512) n = p * q fn = (p - 1) * (q - 1) e = inverse(d, fn) c = pow(flag, e, n) print'e'+str(_)+str('=')+hex(e) print'n'+str(_)+str('=')+hex(n) print'c'+str(_)+str('=')+hex(c)
可以计算出,在 N 最小的情况下 Nrδ 的位数为 408 位,d 最大只有 385 位,因此是可以算出的。