当前位置:网站首页>[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
边栏推荐
- Introduction to several common usage scenarios of message queue
- Common Taylor expansion
- New technology leads new changes in marketing of large and medium-sized enterprises, and UFIDA BiP CRM is launched!
- (十二)51单片机----用DS18B20浅测一下工(江)西的室外温度
- Master data management theory and Practice
- File & recursion 14.1
- Flutter pull_to_refresh-1.6.0/lib/src/internals/slivers.dart:164:13: Error: Method not found: ‘descr
- What are the methods of process synchronization?
- Use a grayscale filter
- [JS reverse hundred examples] a public resource trading network, reverse analysis of announcement URL parameters
猜你喜欢

【zer0pts CTF 2022】 Anti-Fermat

BUUCTF-RSA4

Realization of gobang man-machine combat

五子棋人机对战实现

4小时定单破20000+,自称“百万内最豪华”,国产品牌飘了?

BUUCTF-RSA

Sudden, wechat important notice

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

Ideas, methods and steps of making folding fans with 3DMAX

数据管理的重点
随机推荐
Flutter pull_ to_ refresh-1.6.0/lib/src/internals/slivers. dart:164:13: Error: Method not found: ‘descr
What are the methods of process synchronization?
重新定义分析 - EventBridge 实时事件分析平台发布
Join hands with Changjiang storage, jiangbolong launches the world's smallest expansion card
Accelerate IGBT localization! BYD semiconductor will be listed independently, with a market value of 30billion yuan!
Those "experiences and traps" in the data center
【zer0pts CTF 2022】 Anti-Fermat
Is it really hard to understand? What level of cache is the recyclerview caching mechanism?
BUUCTF-RSA4
Lua基础语法学习
Redis hash underlying data structure
Redis的分布式锁
Calling dht11/22 temperature and humidity sensor in Proteus simulation Arduino
Smartrefresh nested multiple recycleview sliding conflicts and incomplete layout display
[NCTF2019]babyRSA1
基于原生js实现今日新闻网站
JUC工具包学习
TOGAF10标准读书会首场活动圆满举办,精彩时刻回顾!
29. Learn the stacked column chart of highcharts using percentage
【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析