当前位置:网站首页>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数组复制速度测试220320
- 进制转换方法
- What is the difference between concurrency and parallelism?
- TFRecord的Shuffle、划分和读取
- 如果我们是那晚负责修复 B 站崩了的开发人员
- Which one is better to request to merge -- three skills of interface request merging, and the performance directly explodes the table
- Redis hash underlying data structure
- 一加将在2020年释放ODM订单,发力中低端市场
- How to use xshell Free Edition
- C # delegate usage -- console project, which implements events through delegation
猜你喜欢

一位软件投资者的独白:我为什么不追逐快速增长的公司

The print version of imeta | international standard ISSN is officially confirmed, and the application for dual ISSN is completed

C#委托用法--控制台项目,通过委托实现事件

Unity 实现简单画板画画功能(笔记)

JUC toolkit learning

Huawei Hongmeng 3 was officially released, and this security feature has solved a major pain point

C # delegate usage -- console project, which implements events through delegation
![[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

Bank marketing predicts the success rate of a customer's purchase of financial products

【C语言】通讯录(动态版本)
随机推荐
Can Siemens PLC collect analog data of multiple slave stations in real time and wirelessly?
The txt file named according to the sequence number is renamed from the back to the front
Nature review: preferential effects in the formation of microbial communities
[December Haikou] the 6th International Conference on ships, marine and Maritime Engineering in 2022 (naome 2022)
RPA流程自动化机器人是什么技术?如何实现办公自动化?
Arm32 for remote debugging
File&递归14.1
The first activity of togaf10 standard reading club was successfully held, and the wonderful moments were reviewed!
2022/7/24-7/25
In 2019, the world's top ten semiconductor manufacturers: Intel returned to the first place, and apple rose sharply against the trend
Unity 实现简单画板画画功能(笔记)
J9数字科普:Sui网络的双共识是如何工作的?
Socket interaction process of four waves
ELK日志分析系统安装和部署
76000 people shut down! Toshiba announced the closure of all factories in Japan
解决5G使用痛点,魅族17 mSmart 5G快省稳技术发布
真的很难理解?RecyclerView 缓存机制到底是几级缓存?
低代码开发前景如何,大家都真的看好低代码开发吗?
如果我们是那晚负责修复 B 站崩了的开发人员
NDK 系列(6):说一下注册 JNI 函数的方式和时机