当前位置:网站首页>[MRCTF2020]babyRSA
[MRCTF2020]babyRSA
2022-07-27 21:07:00 【[email protected]】
1.题目
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.复现
看着很复杂其实就是给出了e,c和p,q的生成方式让我们去解出p,q。
q很好求就是:
Q=pow(sub_Q,Q_2,Q_1)
q=sympy.nextprime(Q)然后求p,分析一下函数:
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就是一个列表里面都是素数,P[0]是随机生成的128位的素数,后面的P[1]就是P[0]的下一个素数,P[2]......P[16]也是这样。题目给出了P[9],所以其他的素数都可以推出来了。
P = [0 for i in range(17)] P[9]=P_p for i in range(9,-1,-1):#生成P[8]-P[0] P[i-1]=sympy.prevprime(P[i]) for i in range(9,16):#生成P[10]-P[16] P[i+1]=sympy.nextprime(P[i])
factor = pow(p, base, n)
上面我们把P求出来了n就是列表里面的数相乘:
n = 1 for i in range(17): n *= P[i]p^base mod n =factor所以
based=base 对n的欧拉函数的逆元
p=factor^based mod n
p=nextprime(p)
p,q算出来就简单了。
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)#求出q
# print(q)95170653714081687088760585440906768700419459767774333757336842864507607081809193370870747769993218256925111100260761958233280546585624501259121060195932474781731613458132842656517609786144352755126076860272047457230913808406105832246663969943550533958139118721153456230616182820319799156494938586844573835221
P = [0 for i in range(17)]
P[9]=P_p
for i in range(9,0,-1):#生成P[8]-P[0]
P[i-1]=sympy.prevprime(P[i])
for i in range(9,16):#生成P[10]-P[16]
P[i+1]=sympy.nextprime(P[i])
#求n
n = 1
phin=1
for i in range(17):
n *= P[i]
phin*=P[i]-1#n的欧拉函数
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://blog.csdn.net/qq_61774705/article/details/124806617
边栏推荐
- TSMC 3nm detail exposure: transistor density as high as 250million /mm ², Greatly improved performance and energy efficiency
- 2022夏暑假每日一题(五)
- Xu Jinbo: AI protein prediction and design
- Software test function test full set of common interview questions [function test] interview summary 4-2
- [signal denoising] signal denoising based on Kalman filter with matlab code
- sort排序
- Application of user portrait in precise push of wechat official account of scientific journals
- What are the advantages of Tita's OKR system over other similar products or shared documents?
- TCP的粘包拆包问题+解决方案
- [CVA valuation training camp] how to quickly read the annual reports of listed companies - Lesson 4
猜你喜欢

C # delegate usage -- console project, which implements events through delegation

My annual salary is 1million, and I don't have clothes more than 100 yuan all over my body: saving money is the top self-discipline
![[December Haikou] the 6th International Conference on ships, marine and Maritime Engineering in 2022 (naome 2022)](/img/a4/041268aadd5d8ff493b52ead9c5e79.png)
[December Haikou] the 6th International Conference on ships, marine and Maritime Engineering in 2022 (naome 2022)
Software test function test full set of common interview questions [function test] interview summary 4-2

2022夏暑假每日一题(五)

为什么 Redis 集群要使用反向代理? 看这篇就明白了

突发,微信重要通知

2022 summer vacation daily question (5)

Bank Marketing预测一个客户购买理财产品的成功率

Can Siemens PLC collect analog data of multiple slave stations in real time and wirelessly?
随机推荐
2022 summer vacation daily question (5)
Nature review: preferential effects in the formation of microbial communities
Reinforcement learning - pytorch realizes advantage actor critical (A2C)
技术认证 | 图扑软件携手华为云再创合作共赢新局面
Application of user portrait in precise push of wechat official account of scientific journals
2022 International Conference on civil, building and Environmental Engineering (iccaee 2022)
编辑复制粘贴判定问题(bug?),所见即所得显示符号问题反馈。
真的很难理解?RecyclerView 缓存机制到底是几级缓存?
Interviewer: let's talk about the specific process of network data transmission
Redis hash underlying data structure
台积电3nm细节曝光:晶体管密度高达2.5亿个/mm²,性能及能效大幅提升
urllib.error. URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: un
sort排序
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: un
Normality of confidence interval
Lua基础语法学习
Introduction to several common usage scenarios of message queue
Lua basic grammar learning
7.6万人停工!东芝宣布关闭日本所有工厂
并发和并行有什么区别?