当前位置:网站首页>[MRCTF2020]babyRSA
[MRCTF2020]babyRSA
2022-07-27 23:54:00 【[email protected]】
1. subject
import sympy
import random
from gmpy2 import gcd, invert
from Crypto.Util.number import getPrime, isPrime, getRandomNBitInteger, bytes_to_long, long_to_bytes
from z3 import *
flag = b"MRCTF{xxxx}"
base = 65537
def GCD(A):
B = 1
for i in range(1, len(A)):
B = gcd(A[i-1], A[i])
return B
def gen_p():
P = [0 for i in range(17)]
P[0] = getPrime(128)
for i in range(1, 17):
P[i] = sympy.nextprime(P[i-1])
print("P_p :", P[9])
n = 1
for i in range(17):
n *= P[i]
p = getPrime(1024)
factor = pow(p, base, n)
print("P_factor :", factor)
return sympy.nextprime(p)
def gen_q():
sub_Q = getPrime(1024)
Q_1 = getPrime(1024)
Q_2 = getPrime(1024)
Q = sub_Q ** Q_2 % Q_1
print("Q_1: ", Q_1)
print("Q_2: ", Q_2)
print("sub_Q: ", sub_Q)
return sympy.nextprime(Q)
if __name__ == "__main__":
_E = base
_P = gen_p()
_Q = gen_q()
assert (gcd(_E, (_P - 1) * (_Q - 1)) == 1)
_M = bytes_to_long(flag)
_C = pow(_M, _E, _P * _Q)
print("Ciphertext = ", _C)
'''
P_p : 206027926847308612719677572554991143421
P_factor : 213671742765908980787116579976289600595864704574134469173111790965233629909513884704158446946409910475727584342641848597858942209151114627306286393390259700239698869487469080881267182803062488043469138252786381822646126962323295676431679988602406971858136496624861228526070581338082202663895710929460596143281673761666804565161435963957655012011051936180536581488499059517946308650135300428672486819645279969693519039407892941672784362868653243632727928279698588177694171797254644864554162848696210763681197279758130811723700154618280764123396312330032986093579531909363210692564988076206283296967165522152288770019720928264542910922693728918198338839
Q_1: 103766439849465588084625049495793857634556517064563488433148224524638105971161051763127718438062862548184814747601299494052813662851459740127499557785398714481909461631996020048315790167967699932967974484481209879664173009585231469785141628982021847883945871201430155071257803163523612863113967495969578605521
Q_2: 151010734276916939790591461278981486442548035032350797306496105136358723586953123484087860176438629843688462671681777513652947555325607414858514566053513243083627810686084890261120641161987614435114887565491866120507844566210561620503961205851409386041194326728437073995372322433035153519757017396063066469743
sub_Q: 168992529793593315757895995101430241994953638330919314800130536809801824971112039572562389449584350643924391984800978193707795909956472992631004290479273525116959461856227262232600089176950810729475058260332177626961286009876630340945093629959302803189668904123890991069113826241497783666995751391361028949651
Ciphertext = 1709187240516367141460862187749451047644094885791761673574674330840842792189795049968394122216854491757922647656430908587059997070488674220330847871811836724541907666983042376216411561826640060734307013458794925025684062804589439843027290282034999617915124231838524593607080377300985152179828199569474241678651559771763395596697140206072537688129790126472053987391538280007082203006348029125729650207661362371936196789562658458778312533505938858959644541233578654340925901963957980047639114170033936570060250438906130591377904182111622236567507022711176457301476543461600524993045300728432815672077399879668276471832
'''
2. Reappear
It looks very complicated, but it actually gives e,c and p,q Let's figure out p,q.
q It's good. Just ask :
Q=pow(sub_Q,Q_2,Q_1)
q=sympy.nextprime(Q)Then seek p, Analyze the function :
def gen_p():
P = [0 for i in range(17)]
P[0] = getPrime(128)
for i in range(1, 17):
P[i] = sympy.nextprime(P[i-1])
print("P_p :", P[9])
n = 1
for i in range(17):
n *= P[i]
p = getPrime(1024)
factor = pow(p, base, n)
print("P_factor :", factor)
return sympy.nextprime(p)————————————————
P It's a list of primes ,P[0] It's randomly generated 128 The prime number of bits , hinder P[1] Namely P[0] The next prime of ,P[2]......P[16] In the same way . The title gives P[9], So other primes can be deduced .
P = [0 for i in range(17)] P[9]=P_p for i in range(9,-1,-1):# Generate P[8]-P[0] P[i-1]=sympy.prevprime(P[i]) for i in range(9,16):# Generate P[10]-P[16] P[i+1]=sympy.nextprime(P[i])
factor = pow(p, base, n)
Up here we put P Come on n It is the multiplication of the numbers in the list :
n = 1 for i in range(17): n *= P[i]p^base mod n =factor therefore
based=base Yes n The inverse of Euler function of
p=factor^based mod n
p=nextprime(p)
p,q It's easy to calculate .
import sympy
import libnum
import gmpy2
P_p =206027926847308612719677572554991143421
P_factor =213671742765908980787116579976289600595864704574134469173111790965233629909513884704158446946409910475727584342641848597858942209151114627306286393390259700239698869487469080881267182803062488043469138252786381822646126962323295676431679988602406971858136496624861228526070581338082202663895710929460596143281673761666804565161435963957655012011051936180536581488499059517946308650135300428672486819645279969693519039407892941672784362868653243632727928279698588177694171797254644864554162848696210763681197279758130811723700154618280764123396312330032986093579531909363210692564988076206283296967165522152288770019720928264542910922693728918198338839
Q_1= 103766439849465588084625049495793857634556517064563488433148224524638105971161051763127718438062862548184814747601299494052813662851459740127499557785398714481909461631996020048315790167967699932967974484481209879664173009585231469785141628982021847883945871201430155071257803163523612863113967495969578605521
Q_2=151010734276916939790591461278981486442548035032350797306496105136358723586953123484087860176438629843688462671681777513652947555325607414858514566053513243083627810686084890261120641161987614435114887565491866120507844566210561620503961205851409386041194326728437073995372322433035153519757017396063066469743
sub_Q= 168992529793593315757895995101430241994953638330919314800130536809801824971112039572562389449584350643924391984800978193707795909956472992631004290479273525116959461856227262232600089176950810729475058260332177626961286009876630340945093629959302803189668904123890991069113826241497783666995751391361028949651
Ciphertext = 1709187240516367141460862187749451047644094885791761673574674330840842792189795049968394122216854491757922647656430908587059997070488674220330847871811836724541907666983042376216411561826640060734307013458794925025684062804589439843027290282034999617915124231838524593607080377300985152179828199569474241678651559771763395596697140206072537688129790126472053987391538280007082203006348029125729650207661362371936196789562658458778312533505938858959644541233578654340925901963957980047639114170033936570060250438906130591377904182111622236567507022711176457301476543461600524993045300728432815672077399879668276471832
e=65537
base=65537
Q=pow(sub_Q,Q_2,Q_1)
q=sympy.nextprime(Q)# Find out q
# print(q)95170653714081687088760585440906768700419459767774333757336842864507607081809193370870747769993218256925111100260761958233280546585624501259121060195932474781731613458132842656517609786144352755126076860272047457230913808406105832246663969943550533958139118721153456230616182820319799156494938586844573835221
P = [0 for i in range(17)]
P[9]=P_p
for i in range(9,0,-1):# Generate P[8]-P[0]
P[i-1]=sympy.prevprime(P[i])
for i in range(9,16):# Generate P[10]-P[16]
P[i+1]=sympy.nextprime(P[i])
# seek n
n = 1
phin=1
for i in range(17):
n *= P[i]
phin*=P[i]-1#n The Euler function of
based=gmpy2.invert(base,phin)
p=pow(P_factor,based,n)
p=sympy.nextprime(p)
#print(p)160735380264118564161835536782782924160005620631679929855445290207351945863258282088265202232862202180668844947205806261323713945818872852303248590355632665886900928520533421774721590935485773234619558181513033385642711706205607543347313747616062185115981201425568780146693758544521883683953378438266703113683
n=p*q
phi=(p-1)*(q-1)
d=gmpy2.invert(e,phi)
m=pow(Ciphertext,d,n)
flag=libnum.n2s(int(m))
print(flag)
# b'MRCTF{[email protected][email protected]_qu3st10n}'版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/208/202207272106550199.html
边栏推荐
- XSS Payload 学习浏览器解码
- 为什么 Redis 集群要使用反向代理? 看这篇就明白了
- Which one is better to request to merge -- three skills of interface request merging, and the performance directly explodes the table
- UE4 official AEC blueprint case course learning notes
- Xss.haozi.me practice customs clearance
- urllib.error. URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: un
- The txt file named according to the sequence number is renamed from the back to the front
- Current situation and future of Nb IOT industry: cross the threshold of 100million shipments and rush to 5g connection!
- Elk log analysis system installation and deployment
- The 4-hour order exceeds 20000+, claiming to be "the most luxurious in a million". Is the domestic brand floating?
猜你喜欢

Explain the idempotence of distributed system in detail

Master data management theory and Practice

29.学习Highcharts 使用百分比的堆叠柱形图

Starfish OS X metabell strategic cooperation, metauniverse business ecosystem further

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

Redis hash underlying data structure

BUUCTF-RSA

TCP的粘包拆包问题+解决方案

如果我们是那晚负责修复 B 站崩了的开发人员

JUC工具包学习
随机推荐
NDK series (6): let's talk about the way and time to register JNI functions
数据管理的重点
Can Siemens PLC collect analog data of multiple slave stations in real time and wirelessly?
Ideas, methods and steps of making folding fans with 3DMAX
[NCTF2019]babyRSA1
Accelerate IGBT localization! BYD semiconductor will be listed independently, with a market value of 30billion yuan!
MapReduce (III)
如果我们是那晚负责修复 B 站崩了的开发人员
Arm32 for remote debugging
Reinforcement learning - pytorch realizes advantage actor critical (A2C)
Monologue of a software Investor: why don't I pursue fast-growing companies
Master data management theory and Practice
数据中台的那些“经验与陷阱”
Latex common summary (2): input matrix (input matrix, diagonal matrix, equations, etc.)
How to use C WinForm to copy files and display progress
Zcmu--1720: death is like the wind, I want to pretend to force
Calling dht11/22 temperature and humidity sensor in Proteus simulation Arduino
为什么需要等待计时2MSL?
[JS reverse hundred examples] a public resource trading network, reverse analysis of announcement URL parameters
BUUCTF-RSA roll