首页 > 其他分享 >Crypto|[NCTF2019]babyRSA

Crypto|[NCTF2019]babyRSA

时间:2023-05-06 14:34:10浏览次数:42  
标签:1024 gmpy2 babyRSA pow Crypto NCTF2019 nextPrime ed import

task.py

from Crypto.Util.number import *
from flag import flag

def nextPrime(n):
    n += 2 if n & 1 else 1
    while not isPrime(n):
        n += 2
    return n

p = getPrime(1024)
q = nextPrime(p)
n = p * q
e = 0x10001
d = inverse(e, (p-1) * (q-1))
c = pow(bytes_to_long(flag.encode()), e, n)

# d = 19275778946037899718035455438175509175723911466127462154506916564101519923603308900331427601983476886255849200332374081996442976307058597390881168155862238533018621944733299208108185814179466844504468163200369996564265921022888670062554504758512453217434777820468049494313818291727050400752551716550403647148197148884408264686846693842118387217753516963449753809860354047619256787869400297858568139700396567519469825398575103885487624463424429913017729585620877168171603444111464692841379661112075123399343270610272287865200880398193573260848268633461983435015031227070217852728240847398084414687146397303110709214913
# c = 5382723168073828110696168558294206681757991149022777821127563301413483223874527233300721180839298617076705685041174247415826157096583055069337393987892262764211225227035880754417457056723909135525244957935906902665679777101130111392780237502928656225705262431431953003520093932924375902111280077255205118217436744112064069429678632923259898627997145803892753989255615273140300021040654505901442787810653626524305706316663169341797205752938755590056568986738227803487467274114398257187962140796551136220532809687606867385639367743705527511680719955380746377631156468689844150878381460560990755652899449340045313521804

解析

给出了e、d和c的值
p = getPrime(1024)、q = nextPrime(p),p和q是1024个bit位的素数,并且p和q相邻
m = pow(c,d,n),c、d已知所以需要求n
n=pq,所以求p和q即可
根据rsa的公式:e
d = 1 mod (p-1) ∗(q-1)
可得:ed - 1 = k * (p-1)(q-1)
根据已知的e和d求得ed - 1是2064位
p和q是1024个bit位,所以(p-1)
(q-1)是2048位,相隔16位
可得k在范围$2^{15}$ ~ $2^{16}$
得到k后对(p-1)*(q-1)进行开方,得到p,q则是p的下一个素数

解密脚本

import gmpy2
import sympy
import libnum
 
e = 0x10001
c = 5382723168073828110696168558294206681757991149022777821127563301413483223874527233300721180839298617076705685041174247415826157096583055069337393987892262764211225227035880754417457056723909135525244957935906902665679777101130111392780237502928656225705262431431953003520093932924375902111280077255205118217436744112064069429678632923259898627997145803892753989255615273140300021040654505901442787810653626524305706316663169341797205752938755590056568986738227803487467274114398257187962140796551136220532809687606867385639367743705527511680719955380746377631156468689844150878381460560990755652899449340045313521804
d = 19275778946037899718035455438175509175723911466127462154506916564101519923603308900331427601983476886255849200332374081996442976307058597390881168155862238533018621944733299208108185814179466844504468163200369996564265921022888670062554504758512453217434777820468049494313818291727050400752551716550403647148197148884408264686846693842118387217753516963449753809860354047619256787869400297858568139700396567519469825398575103885487624463424429913017729585620877168171603444111464692841379661112075123399343270610272287865200880398193573260848268633461983435015031227070217852728240847398084414687146397303110709214913
 
a = e*d-1
for k in range(pow(2,15),pow(2,16)):
    if a % k == 0:
        p = sympy.prevprime(gmpy2.iroot(a//k,2)[0])
        q = sympy.nextprime(p)
        if (p-1)*(q-1)*k == a:
            break

#p = 143193611591752210918770476402384783351740028841763223236102885221839966637073188462808195974548579833368313904083095786906479416347681923731100260359652426441593107755892485944809419189348311956308456459523437459969713060653432909873986596042482699670451716296743727525586437248462432327423361080811225075839
#q = 143193611591752210918770476402384783351740028841763223236102885221839966637073188462808195974548579833368313904083095786906479416347681923731100260359652426441593107755892485944809419189348311956308456459523437459969713060653432909873986596042482699670451716296743727525586437248462432327423361080811225076497

n = p*q
m = gmpy2.powmod(c,d,n)
print(libnum.n2s(int(m)).decode())
NCTF{70u2_nn47h_14_v3ry_gOO0000000d}
<!-- more -->

标签:1024,gmpy2,babyRSA,pow,Crypto,NCTF2019,nextPrime,ed,import
From: https://www.cnblogs.com/scarecr0w7/p/17377217.html

相关文章

  • Crypto|[AFCTF2018]可怜的RSA
    public.key-----BEGINPUBLICKEY-----MIIBJDANBgkqhkiG9w0BAQEFAAOCAREAMIIBDAKCAQMlsYv184kJfRcjeGa7Uc/43pIkU3SevEA7CZXJfA44bUbBYcrf93xphg2uR5HCFM+Eh6qqnybpIKl3g0kGA4rvtcMIJ9/PP8npdpVE+U4Hzf4IcgOaOmJiEWZ4smH7LWudMlOekqFTs2dWKbqzlC59NeMPfu9avxxQ15fQzIjhvc......
  • Web|Buuctf [NCTF2019]SQLi
    直接给出了查询语句select*fromuserswhereusername=''andpasswd=''构造语句查询,发现有过滤fuzz一下,很多参数都被过滤robots协议下发现hint.txt文件hint.txt文件,有被过滤的参数,但是没有过滤"、|和\,并且提示只要密码与admin的密码相同就可以获得flag解题思路无......
  • cryptohack wp day(3)
    第二节模运算----第一题(GCD)在做这道题前,了解下欧几里得算法:欧几里得算法,也叫辗转相除法,用于求解两个非负整数a和b的最大公约数(GreatestCommonDivisor,GCD),即能够同时整除它们的最大正整数。算法的基本思想是,通过不断求解a和b的余数的最大公约数,最终可以得到a和b的最大公约......
  • cryptohack wp day (2)
    接着昨天的题目第五题看题目,一道简单的xor题,就是将“label中每个字符与13进行异或处理”,直接上代码:s="label"result=""foriins:result+=chr(ord(i)^13)print(result)或者按照题目所说,用pwntools库中的xor函数来进行异或操作,具体操作如下:frompwnimportxor......
  • cryptohack wp day(1)
    就从头开始吧第一题(ASCII)一道简单的ASCII码转换,直接用题目的提示代码解就行了ascii=[99,114,121,112,116,111,123,65,83,67,73,73,95,112,114,49,110,116,52,98,108,51,125]flag=""foriinascii:flag+=chr(i)print(flag)第二题(Hex)......
  • 安装pycrypto
    下载地址:https://www.dlitz.net/software/pycrypto/在BT5上安装:root@bt:~#easy_installpycrypto-2.6.tar.gzProcessingpycrypto-2.6.tar.gzWriting/tmp/easy_install-bRS9dY/pycrypto-2.6/setup.cfgRunningpycrypto-2.6/setup.py-qbdist_egg--dist-dir/tmp/easy_insta......
  • 前端使用CryptoJS加密解密
    1、安装crypto-js;npminstallcrypto-js--save-devyarnaddcrypto-js--dev2、新建unit.js写成公共方法;constCryptoJS=require('crypto-js');//16位十六进制数作为密钥(秘钥为随机生成,必须与后端保持一致!)constkey=CryptoJS.enc.Utf8.parse("xxxxxxxxxxxxxx");//......
  • DASCTF-Crypto-Sign1n
    sign1n显然能导出下面的式子:\(k\timesphi=e^3\times(WHATF-3)-1\).注意到\(phi=(p-1)\times(q-1)=n-(p+q)+1\),\(n=p\timesq\).考虑构造一元二次方程解出pq.同时考虑到\(k\times(p+q-1)<n\),代入得到:\(k\times(p+q-1)=(1-e^3\times(WHATF-3))\modn\).......
  • 【win10】No module named “Crypto”
    1、问题  下载视频解析的时候报错Nomodulenamed“Crypto”,已经pip安装  2、解决pipuninstallcryptopycryptodomepipinstallpycryptodome pycrypto和crypto是同一个库,crypto在python中又被称为pycrypto,它是一个第三方库,但是已经停止更新了,所以不建议大......
  • Proj. CHW Paper Reading: Characterizing Cryptocurrency Exchange Scams
    1.introBlockchaincommunity防范scamattack措施包含maliciousdomains的开源数据库,例如CryptoScamDB和EtherScanDB多半是使用crowd-sourcingbasedapproach搜集,例如受害者报告本文探究theextentthescamsexistintheecosystemwhoaretheattackerswhatare......