from Crypto.Util.number import * from gmpy2 import *
p = 67711062621608175960173275013534737889372437946924512522469843485353704013203 q = 91200252033239924238625443698357031288749612243099728355449192607988117291739 e = 2 c = 5251890478898826530186837207902117236305266861227697352434308106457554098811792713226801824100629792962861125855696719512180887415808454466978721678349614
from Crypto.Util.number import * from gmpy2 import *
n = 6969872410035233098344189258766624225446081814953480897731644163180991292913719910322241873463164232700368119465476508174863062276659958418657253738005689 e = 3331016607237504021038095412236348385663413736904453330557803644384818257225138777641344877202234881627514102078530507171735156112302207979925588113589669 c = 1754994938947260364311041300467524420957926989584983693004487724099773647229373820465164193428679197813476633649362998772470084452129370353136199193923837
classContinuedFraction(): def__init__(self, numerator, denumerator): self.numberlist = [] # number in continued fraction self.fractionlist = [] # the near fraction list self.GenerateNumberList(numerator, denumerator) self.GenerateFractionList()
''' n = 53763529836257082401813045869248978487210852880716446938539970599235060144454914000042178896730979463959004404421520555831136502171902051936080825853063287829 e = 65537 c = 50368170865606429432907125510556310647510431461588875539696416879298699197677994843344925466156992948241894107250131926237473102312181031875514294014181272618 '''
n = 53763529836257082401813045869248978487210852880716446938539970599235060144454914000042178896730979463959004404421520555831136502171902051936080825853063287829 e = 65537 c = 50368170865606429432907125510556310647510431461588875539696416879298699197677994843344925466156992948241894107250131926237473102312181031875514294014181272618
a = 2 m = 2 whileTrue: a = pow(a, m, n) p = GCD(a-1, n) if p != 1and p != n: break m += 1
q = n // p
phi = (p-1)*(q-1) d = inverse(e, phi) m = pow(c, d, n) print(long_to_bytes(m))
from Crypto.Util.number import * from gmpy2 import * from itertools import count
n = 63398538193562720708999492397588489035970399414238113344990243900620729661046648078623873637152448697806039260616826648343172207246183989202073562200879290937 e = 65537 c = 26971181342240802276810747395669930355754928952080329914687241779532014305320191048439959934699795162709365987652696472998140484810728817991804469778237933925
defmlucas(v, a, n): v1, v2 = v, (v ** 2 - 2) % n for bit inbin(a)[3:]: v1, v2 = ((v1 ** 2 - 2) % n, (v1 * v2 - v) % n) if bit == "0"else ( (v1 * v2 - v) % n, (v2 ** 2 - 2) % n) return v1
defprimegen(): yield2 yield3 yield5 yield7 yield11 yield13 ps = primegen() # yay recursion p = ps.__next__() and ps.__next__() q, sieve, n = p ** 2, {}, 13 whileTrue: if n notin sieve: if n < q: yield n else: next, step = q + 2 * p, 2 * p whilenextin sieve: next += step sieve[next] = step p = ps.__next__() q = p ** 2 else: step = sieve.pop(n) next = n + step whilenextin sieve: next += step sieve[next] = step n += 2
defilog(x, b): # greatest integer l such that b**l <= x. l = 0 while x >= b: x /= b l += 1 return l
defattack(n): for v in count(1): for p in primegen(): e = ilog(isqrt(n), p) if e == 0: break for _ inrange(e): v = mlucas(v, p, n) g = gcd(v - 2, n) if1 < g < n: returnint(g), int(n // g) # g|n if g == n: break
p, q = attack(n)
phi = (p-1)*(q-1) d = invert(e, phi) m = powmod(c, d, n) print(long_to_bytes(m))
[RSA2]P8
我们的目的也就是找出一个可以使得
s1×e1+s2×e2=1
那么我们就可以使得c1xc2=m mod n
我们也就可以据此求得m
这里要使用欧几里得扩展算法,具体代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13
from Crypto.Util.number import * from gmpy2 import *
# 10 n = 79201858340517902370077926747686673001645933420450220163567700296597652438275339093680329918615445030212417351430952656177171126427547284822789947152085534939195866096891005587613262293569611913019639653984932469691636338705418303482885987114085769045348074530172292982433373154900841135911548332400167290083 c = 70109332985937768446301118795636999352761371683181615470371772202170324747707233792154935611826981798791499937601162039878070094663516868746240133223110650205575807753345252087103328657073552992431511929172241702073381723302143955977662087561904058172777520360991685289300855900793806183473523998422682944404 dp = 3098334089252415941833934532457314870210700261428241562420857845879512952043729097866485406309479489101668423603305497982177150304625615059119312238777275 e = 65537 for i inrange(1,e): if (e * dp - 1) % i == 0: p = ((e * dp - 1) // i ) +1 print(p) if n % p == 0: q = n // p print(q) d = inverse(e,(p-1)*(q-1)) exit(long_to_bytes(pow(c,d,n)))
[RSA2]P11
这里的e较大,我们不能尝试直接遍历了,根据之前的欧拉降幂加上这个,我们可以推出来
aedp−a≡0modp
所以我们可以直接将这个式子和n取gcd即可得到p
我们利用这个方法写脚本
1 2 3 4 5 6 7 8 9 10
n = 108280026722298796068968170303156759745471686664814404724171434502249429011870583595808692893118419248225924869164875379709992190884930717654004006466664403479467573176438601715156464950045121937338569942817256182277141174728470067308962244296992229214749863655518517510026063088263849891990324547823192559069 e = 305691242207901867366357529364270390903 c = 26537258289122728220745496185201994733321402056894636636642710319261241111675937946139938310952968353253866895253865273981912174303818938005932883052177988834834575591342856235464380238486868448329727891268391728758132913642966389278296932186703733187105516710825918064228397602264185334108934765627411913661 dp = 2656631506624565349527023729530989647164022271235521672257622068579788839123502046687139927161669209201953909023994372208117081512139181611949631467292513
m = 10007 p = GCD(pow(m,e*dp,n)-m,n) q = n // p d = inverse(e,(p-1)*(q-1)) print(long_to_bytes(pow(c,d,n)))
from Crypto.Util.number import * from gmpy2 import * import hashlib
n = 113917408220469425995764932761465306974540330325378601642830241920567032775895088098706711486764203845425248022960733155994427766750033219106642310531864450654102562104771892268897793145789045570107312401570269581223945259704851104645493075550316424129401227653740942495625720165869565257394427181127734628103 d = 15762135247924329080208071933121250646888501386858311483546464344350547831176536290630826247188272280853810047335214127264865205744683174860903496832368687060941437002920094364116706593296591581117381565805322046922482804679245558495134876677733584718947309975077159564300049936769192724856722338627154192353 e = 65537
t = e*d - 1 s = 0
while t % 2 == 0: s += 1 t //= 2
found = False
for i inrange(1, s): c1 = powmod(2, powmod(2, i-1, n)*t, n) c2 = powmod(2, powmod(2, i, n)*t, n) if c1 != 1and c1 != (-1 % n) and c2 == 1: p = gcd(c1 - 1, n) q = n // p break