当前位置:网站首页>[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
边栏推荐
- 使用灰度滤镜
- Your list is too laggy? These four optimizations can make your list silky smooth
- Yijia will release ODM orders in 2020 and make efforts in the middle and low-end market
- Join hands with Changjiang storage, jiangbolong launches the world's smallest expansion card
- 疫情之下,台积电一季度增长超预期,7nm占比35%!二季度或创新高
- CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021
- TFRecord的Shuffle、划分和读取
- 股价暴涨180.46%!国产大硅片龙头沪硅产业上市:近4年净利累计不足6000万
- 钉钉报警工具
- 【12月海口】2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
猜你喜欢

File & recursion 14.1

西门子PLC能否实时无线采集多处从站模拟量数据?

Application of user portrait in precise push of wechat official account of scientific journals

JUC工具包学习

真的很难理解?RecyclerView 缓存机制到底是几级缓存?

File&递归14.1

Lua basic grammar learning

JUC toolkit learning

Remotely debug idea, configure remote debug, and add JVM startup parameter -xdebug in the program of remote server

Redis hash underlying data structure
随机推荐
Application of user portrait in precise push of wechat official account of scientific journals
【12月海口】2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
Huawei Hongmeng 3 was officially released, and this security feature has solved a major pain point
新技术引领大中型企业营销新变革,用友BIP CRM重磅发布!
Date的使用
Zabbix4.0使用SNMP代理方式监控vcenter6.5
The print version of imeta | international standard ISSN is officially confirmed, and the application for dual ISSN is completed
reduce错误示范
基于mediapipe的姿态识别和简单行为识别
Those "experiences and traps" in the data center
MySQL data query (where)
Solve 5g pain points, Meizu 17 smart 5g fast and stable technology release
C#委托用法--控制台项目,通过委托实现事件
2022夏暑假每日一题(五)
他山之石 | 蚂蚁超大规模知识图谱构建及应用
MySQL之数据查询(WHERE)
五子棋人机对战实现
Using the optical fingerprint scheme under the huiding screen, Samsung Galaxy a71 5g is listed
Yijia will release ODM orders in 2020 and make efforts in the middle and low-end market
Join hands with Changjiang storage, jiangbolong launches the world's smallest expansion card