zoukankan      html  css  js  c++  java
  • UNCTF2020-crypto:简单的RSA

    题目:

    e= 18437613570247445737704630776150775735509244525633303532921813122997549954741828855898842356900537746647414676272022397989161180996467240795661928117273837666615415153571959258847829528131519423486261757569454011940318849589730152031528323576997801788206457548531802663834418381061551227544937412734776581781
    n= 147282573611984580384965727976839351356009465616053475428039851794553880833177877211323318130843267847303264730088424552657129314295117614222630326581943132950689147833674506592824134135054877394753008169629583742916853056999371985307138775298080986801742942833212727949277517691311315098722536282119888605701
    c= 140896698267670480175739817539898638657099087197096836734243016824204113452987617610944986742919793506024892638851339015015706164412994514598564989374037762836439262224649359411190187875207060663509777017529293145434535056275850555331099130633232844054767057175076598741233988533181035871238444008366306956934

    给了e、n、c求明文,使用前辈写的脚本:

    import gmpy2
    import time
    def continuedFra(x, y):
        cF = []
        while y:
            cF += [x / y]
            x, y = y, x % y
        return cF
    def Simplify(ctnf):
        numerator = 0
        denominator = 1
        for x in ctnf[::-1]:
            numerator, denominator = denominator, x * denominator + numerator
        return (numerator, denominator)
    def calculateFrac(x, y):
        cF = continuedFra(x, y)
        cF = map(Simplify, (cF[0:i] for i in xrange(1, len(cF))))
        return cF
    def solve_pq(a, b, c):
        par = gmpy2.isqrt(b * b - 4 * a * c)
        return (-b + par) / (2 * a), (-b - par) / (2 * a)
    def wienerAttack(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!'
    time.clock()
    e= 18437613570247445737704630776150775735509244525633303532921813122997549954741828855898842356900537746647414676272022397989161180996467240795661928117273837666615415153571959258847829528131519423486261757569454011940318849589730152031528323576997801788206457548531802663834418381061551227544937412734776581781
    n= 147282573611984580384965727976839351356009465616053475428039851794553880833177877211323318130843267847303264730088424552657129314295117614222630326581943132950689147833674506592824134135054877394753008169629583742916853056999371985307138775298080986801742942833212727949277517691311315098722536282119888605701
    c= 140896698267670480175739817539898638657099087197096836734243016824204113452987617610944986742919793506024892638851339015015706164412994514598564989374037762836439262224649359411190187875207060663509777017529293145434535056275850555331099130633232844054767057175076598741233988533181035871238444008366306956934
    p, q = wienerAttack(e, n)
    print '[+]Found!'
    print '  [-]p =',p
    print '  [-]q =',q
    print '  [-]n =',p*q
    d = gmpy2.invert(e,(p-1)*(q-1))
    print '  [-]d =', d
    print '  [-]m is:' + '{:x}'.format(pow(c,d,n)).decode('hex')
    print '
    [!]Timer:', round(time.clock(),2), 's'
    print '[!]All Done!'

  • 相关阅读:
    概率期望,数学,贪心策略——2020-camp-day1-A
    k染色——2020-camp-day3-C
    树形dp——2020-camp-day3-G
    欧拉回路/路径——2020-camp-day2-H
    dsu on tree——2020-camp-day2-E
    Nim博弈,异或性质——2020-camp-day2-C
    一些视频资料
    开发人员收藏的网站
    各行公认的好书
    资料库链接
  • 原文地址:https://www.cnblogs.com/luocodes/p/13986493.html
Copyright © 2011-2022 走看看