当前位置:网站首页>BUUCTF-childRSA费马小定理
BUUCTF-childRSA费马小定理
2022-07-27 21:07:00 【[email protected]】
1.题目代码:
from random import choice
from Crypto.Util.number import isPrime, sieve_base as primes
from flag import flag
def getPrime(bits):
while True:
n = 2
while n.bit_length() < bits:
n *= choice(primes)
if isPrime(n + 1):
return n + 1
e = 0x10001
m = int.from_bytes(flag.encode(), 'big')
p, q = [getPrime(2048) for _ in range(2)]
n = p * q
c = pow(m, e, n)
# n = 32849718197337581823002243717057659218502519004386996660885100592872201948834155543125924395614928962750579667346279456710633774501407292473006312537723894221717638059058796679686953564471994009285384798450493756900459225040360430847240975678450171551048783818642467506711424027848778367427338647282428667393241157151675410661015044633282064056800913282016363415202171926089293431012379261585078566301060173689328363696699811123592090204578098276704877408688525618732848817623879899628629300385790344366046641825507767709276622692835393219811283244303899850483748651722336996164724553364097066493953127153066970594638491950199605713033004684970381605908909693802373826516622872100822213645899846325022476318425889580091613323747640467299866189070780620292627043349618839126919699862580579994887507733838561768581933029077488033326056066378869170169389819542928899483936705521710423905128732013121538495096959944889076705471928490092476616709838980562233255542325528398956185421193665359897664110835645928646616337700617883946369110702443135980068553511927115723157704586595844927607636003501038871748639417378062348085980873502535098755568810971926925447913858894180171498580131088992227637341857123607600275137768132347158657063692388249513
# c = 263080183567398538953822401099688941751667312837029270021652689987737083352163389970583141577171471310832965513133340425098062298533414884610870099552038542533138276082754605927856077390919925914310803426640819620305570427848640745333807010145853156632187831301623761760947730104781593624343317872793033027180987355746054698038018731099824732582074443423306331918490405535507088865933407707530643224108890481354250257159821966006507409870764865406740909231816642815151976797459078301076847772485322786453437162636860149410814179146227249063149602499451050113017312473246016208867829672173393403938536164500771051253919826899861783424172233922170852764654711027375947199323472424826703208010631918694713183135144079973263500651879041542295577063513550524460271599725467372134514229782110557781645787821564284666268940261030533604312816446455151554713018268447543388023528460952934217182498197282055385346522129848312836424720716694948518231235528273807377986098297062257443766670825340268744834824831274915334743065522100393862560621163457858706683315137257920533021882766825506726633539377810556218601016242422166716358243114127934959656288760363447317331427594953482489703136553814072414571187435323113946977632836818529085643872826052791082.复现
我们的目标就是求phi=(p-1)*(q-1)
这里primes为前10000个素数的列表
p,q的生成方式是一个突破口,分析函数getprime(),p,q就是在前10000个素数列表里面随机找几个素数相乘再加一得到的,所以这10000个素数的乘积x是p-1和q-1的倍数,x=k*(p-1),n=p*q,这里就要从n和x得到p。
费马小定理
若b为一个素数,则对于任意整数a,有a^(b-1) = 1 (mod b),即a^k*(b-1) = 1 (mod b)
这里为啥要用费马小定理,这也不敢问啊,>-<,可能是有一个减一吧。
自己推一遍:
x=k*(p-1),n=p*q
2^k*(p-1)=1 mod p
2^k*(p-1)-1=k*p
2^x-1=k*p
这里2^x-1是p的倍数,n也是p的倍数,他们的公因数就是p。
用gcd(2^x-1,n)=p,但是x太大了,直接计算公因数运行时间太久了。
我是这样想的p比n小,把2^x-1 mod n 再求公因数应该也可以吧。
下面看看证明:
2^x= 1 (mod p),即2^x = 1 + k1*p
而2^x% n = 2^x - k2n = 2^x - k2pq
两边同时% p,有2^x% n = 2^x (mod p)
所以同样有2^x % n = 1 (mod p)
import gmpy2
import libnum
from Crypto.Util.number import isPrime, sieve_base as primes
c = 26308018356739853895382240109968894175166731283702927002165268998773708335216338997058314157717147131083296551313334042509806229853341488461087009955203854253313827608275460592785607739091992591431080342664081962030557042784864074533380701014585315663218783130162376176094773010478159362434331787279303302718098735574605469803801873109982473258207444342330633191849040553550708886593340770753064322410889048135425025715982196600650740987076486540674090923181664281515197679745907830107684777248532278645343716263686014941081417914622724906314960249945105011301731247324601620886782967217339340393853616450077105125391982689986178342417223392217085276465471102737594719932347242482670320801063191869471318313514407997326350065187904154229557706351355052446027159972546737213451422978211055778164578782156428466626894026103053360431281644645515155471301826844754338802352846095293421718249819728205538534652212984831283642472071669494851823123552827380737798609829706225744376667082534026874483482483127491533474306552210039386256062116345785870668331513725792053302188276682550672663353937781055621860101624242216671635824311412793495965628876036344731733142759495348248970313655381407241457118743532311394697763283681852908564387282605279108
n=32849718197337581823002243717057659218502519004386996660885100592872201948834155543125924395614928962750579667346279456710633774501407292473006312537723894221717638059058796679686953564471994009285384798450493756900459225040360430847240975678450171551048783818642467506711424027848778367427338647282428667393241157151675410661015044633282064056800913282016363415202171926089293431012379261585078566301060173689328363696699811123592090204578098276704877408688525618732848817623879899628629300385790344366046641825507767709276622692835393219811283244303899850483748651722336996164724553364097066493953127153066970594638491950199605713033004684970381605908909693802373826516622872100822213645899846325022476318425889580091613323747640467299866189070780620292627043349618839126919699862580579994887507733838561768581933029077488033326056066378869170169389819542928899483936705521710423905128732013121538495096959944889076705471928490092476616709838980562233255542325528398956185421193665359897664110835645928646616337700617883946369110702443135980068553511927115723157704586595844927607636003501038871748639417378062348085980873502535098755568810971926925447913858894180171498580131088992227637341857123607600275137768132347158657063692388249513
e=0x10001
x=1
for i in primes:
x=i*x
p=gmpy2.gcd(pow(2,x,n)-1,n)
q=n//p
phi=(p-1)*(q-1)
d=gmpy2.invert(e,phi)
m=pow(c,d,n)
flag=libnum.n2s(int(m))
print(flag)
# b'NCTF{Th3r3_ar3_1ns3cure_RSA_m0duli_7hat_at_f1rst_gl4nce_appe4r_t0_be_s3cur3}'总结:当知道某一个数x是p-1,q-1的倍数时,就用gcd(2^x-1,n)=p,进一步gcd(pow(2,x,n)-1,n)=p。
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_61774705/article/details/124676106
边栏推荐
- JS提升:JS中的数组扁平化问题
- 新技术引领大中型企业营销新变革,用友BIP CRM重磅发布!
- The first activity of togaf10 standard reading club was successfully held, and the wonderful moments were reviewed!
- [C language] address book (dynamic version)
- CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021
- How to use xshell Free Edition
- urllib.error. URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: un
- C#委托用法--控制台项目,通过委托实现事件
- 基于原生js实现今日新闻网站
- 请求合并哪家强——接口请求合并的3种技巧,性能直接爆表
猜你喜欢

Is it really hard to understand? What level of cache is the recyclerview caching mechanism?
Software test function test full set of common interview questions [function test] interview summary 4-2

2022 International Conference on civil, building and Environmental Engineering (iccaee 2022)
![[JS reverse hundred examples] a public resource trading network, reverse analysis of announcement URL parameters](/img/05/7029eb1fe36d7ddab2640f07247c81.png)
[JS reverse hundred examples] a public resource trading network, reverse analysis of announcement URL parameters

远程调试 idea配置remote debug、在远程服务器的程序中,添加JVM启动参数-Xdebug

给网站套上Cloudflare(以腾讯云为例)

Can Siemens PLC collect analog data of multiple slave stations in real time and wirelessly?

Record the errors about formatc in R language

Redis hash underlying data structure

TCP的粘包拆包问题+解决方案
随机推荐
日产1500万只!比亚迪口罩拿下美国加州10亿美元订单
29.学习Highcharts 使用百分比的堆叠柱形图
解决5G使用痛点,魅族17 mSmart 5G快省稳技术发布
Arm32进行远程调试
Current situation and future of Nb IOT industry: cross the threshold of 100million shipments and rush to 5g connection!
五子棋人机对战实现
Date的使用
C # delegate usage -- console project, which implements events through delegation
org.junit.runners.model. InvalidTestClassError: Invalid test class ‘com.zhj.esdemo. MysqlTests‘: 1.
(十二)51单片机----用DS18B20浅测一下工(江)西的室外温度
2019年全球十大半导体厂商:英特尔重回第一,苹果逆势大涨
股价暴涨180.46%!国产大硅片龙头沪硅产业上市:近4年净利累计不足6000万
并发和并行有什么区别?
29. Learn the stacked column chart of highcharts using percentage
76000 people shut down! Toshiba announced the closure of all factories in Japan
NB-IoT产业的现状与未来:跨过1亿出货门槛,奔向5G大连接!
请求合并哪家强——接口请求合并的3种技巧,性能直接爆表
The print version of imeta | international standard ISSN is officially confirmed, and the application for dual ISSN is completed
2022 International Conference on civil, building and Environmental Engineering (iccaee 2022)
[CVA valuation training camp] how to quickly read the annual reports of listed companies - Lesson 4