当前位置:网站首页>Buuctf childrsa Fermat theorem
Buuctf childrsa Fermat theorem
2022-07-27 23:54:00 【[email protected]】
1. Title code :
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. Reappear
Our goal is to seek phi=(p-1)*(q-1)
here primes For the former 10000 A list of primes
p,q The generation method of is a breakthrough , Analysis function getprime(),p,q Just in front 10000 Find several primes randomly in the list of primes, multiply them and add one to get , So this 10000 The product of prime numbers x yes p-1 and q-1 Multiple ,x=k*(p-1),n=p*q, From here n and x obtain p.
Fermat's small Theorem
if b Is a prime number , For any integer a, Yes a^(b-1) = 1 (mod b), namely a^k*(b-1) = 1 (mod b)
Why use Fermat's theorem here , I dare not ask ,>-<, Maybe there is one minus one .
Push it yourself :
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
here 2^x-1 yes p Multiple ,n It's also p Multiple , Their common factor is p.
use gcd(2^x-1,n)=p, however x It's too big , It takes too long to calculate the common factor directly .
I think so p Than n Small , hold 2^x-1 mod n It should also be possible to find the common factor .
Let's see the proof :
2^x= 1 (mod p), namely 2^x = 1 + k1*p
and 2^x% n = 2^x - k2n = 2^x - k2pq
Both sides at the same time % p, Yes 2^x% n = 2^x (mod p)
So there are also 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}'summary : When you know a certain number x yes p-1,q-1 A multiple of the , Just use gcd(2^x-1,n)=p, further gcd(pow(2,x,n)-1,n)=p.
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/208/202207272106553239.html
边栏推荐
- Realization of gobang man-machine combat
- How to use FTP to realize automatic update of WinForm
- proteus仿真arduino中调用DHT11/22温湿度传感器
- 硬布线控制器的特点:
- Reinforcement learning - pytorch realizes advantage actor critical (A2C)
- TOGAF10标准读书会首场活动圆满举办,精彩时刻回顾!
- Spark 离线开发框架设计与实现
- The share price soared 180.46%! Shanghai silicon industry, the leader of domestic large silicon wafers, is listed: the cumulative net profit in recent four years is less than 60million
- JUC toolkit learning
- 2022夏暑假每日一题(五)
猜你喜欢

Common Taylor expansion
![[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

数据管理的重点

详解分布式系统的幂等
![[RoarCTF2019]babyRSA威尔逊定理](/img/c1/52e79b6e40390374d48783725311ba.gif)
[RoarCTF2019]babyRSA威尔逊定理

Is it really hard to understand? What level of cache is the recyclerview caching mechanism?

TCP sticking and unpacking problem + Solution

(十二)51单片机----用DS18B20浅测一下工(江)西的室外温度

【开发教程9】疯壳·开源蓝牙心率防水运动手环-心率监测

TOGAF10标准读书会首场活动圆满举办,精彩时刻回顾!
随机推荐
Can Siemens PLC collect analog data of multiple slave stations in real time and wirelessly?
Calling dht11/22 temperature and humidity sensor in Proteus simulation Arduino
BUUCTF-[BJDCTF2020]RSA1
File & recursion 14.1
[NPUCTF2020]EzRSA
This is the most concise guide to tcpdump in history. It's enough to read this one
Latex中如何加粗字体 & 如何打出圆圈序号
Arm32进行远程调试
TOGAF10标准读书会首场活动圆满举办,精彩时刻回顾!
[ACTF新生赛2020]crypto-aes
The first activity of togaf10 standard reading club was successfully held, and the wonderful moments were reviewed!
远程调试 idea配置remote debug、在远程服务器的程序中,添加JVM启动参数-Xdebug
加速IGBT国产化!比亚迪半导体将独立上市,市值或达300亿元!
org.junit.runners.model. InvalidTestClassError: Invalid test class ‘com.zhj.esdemo. MysqlTests‘: 1.
How to use C WinForm to copy files and display progress
CPU的控制方式
smartRefresh嵌套多个RecycleView滑动冲突及布局显示不全
Xss.haozi.me practice customs clearance
BUUCTF-RSA
UE4官方AEC蓝图案例课程学习笔记