当前位置:网站首页>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
边栏推荐
- Introduction to several common usage scenarios of message queue
- Flutter pull_ to_ refresh-1.6.0/lib/src/internals/slivers. dart:164:13: Error: Method not found: ‘descr
- 疫情之下,台积电一季度增长超预期,7nm占比35%!二季度或创新高
- 请求合并哪家强——接口请求合并的3种技巧,性能直接爆表
- The total investment is 60billion! Foxconn semiconductor high-end package test project officially settled in Qingdao
- 使用灰度滤镜
- The first activity of togaf10 standard reading club was successfully held, and the wonderful moments were reviewed!
- org.junit.runners.model.InvalidTestClassError: Invalid test class ‘com.zhj.esdemo.MysqlTests‘: 1.
- 苹果发布新款iPhone SE:搭载A13仿生处理器,售价3299元起
- 2022/7/26
猜你喜欢
随机推荐
Error:svn: E155010: ‘/Users/.../Desktop/wrokspace/xxx‘ is scheduled for addition, but is missing
四次挥手的Socket交互流程
Lua基础语法学习
Software test function test full set of common interview questions [function test] interview summary 4-2
Huawei Hongmeng 3 was officially released, and this security feature has solved a major pain point
加速IGBT国产化!比亚迪半导体将独立上市,市值或达300亿元!
尚硅谷尚品项目汇笔记(一)
76000 people shut down! Toshiba announced the closure of all factories in Japan
实现按照序号命名的txt文件由后往前递补重命名文件
Accelerate IGBT localization! BYD semiconductor will be listed independently, with a market value of 30billion yuan!
Yijia will release ODM orders in 2020 and make efforts in the middle and low-end market
File&递归14.1
Character stream learning 14.3
Using the optical fingerprint scheme under the huiding screen, Samsung Galaxy a71 5g is listed
基于原生js实现今日新闻网站
Is it really hard to understand? What level of cache is the recyclerview caching mechanism?
2022/7/26
smartRefresh嵌套多个RecycleView滑动冲突及布局显示不全
Explain the idempotence of distributed system in detail
Your list is too laggy? These four optimizations can make your list silky smooth









