目录

普通题目

RSA共模攻击

 RSA低加密指数攻击

RSA维纳攻击

easyrsa6

easyrsa7

easyrsa8

N不互素(funnyrsa1)

funnyrsa2

 funnyrsa3(dp泄露)

unusualrsa1

unusualrsa2

unusualrsa3

2022红明谷rsa

best_rsa

unusualrsa4

unusual5


普通题目

rsa最简单的题目,是给出pq等数据,要求求解d或是明文,使用脚本就可以轻松解密。

import libnum
import gmpy2

p=libnum.generate_prime(1024)#生成随机数
q=libnum.generate_prime(1024)
n=p*q
e=65537
phi_n=(p-1)*(q-1)
d=gmpy2.invert(e,phi_n)
c=pow(c,m,n)


print("p=",p)
print("q=",q)
print("n=",n)
print("d=",d)

print("e=",e)

import libnum

n=
e=
d=
c=4
m=pow(c,d,n)
print(libnum.n2s(m))

以上分别为解密与解密脚本。

RSA共模攻击

rsa共模攻击通俗来说,就是同一明文,同一个n,e不相同,进行加密。

简单解密脚本如下:

import libnum
import gmpy2

n=
e1=
e2=
c1=
c2=
s1,s2,s3=gmpy2.gcdext(e1,e2)#拓展欧几里得算法
print(s1,s2,s3)
m=(pow(c1,s2,n)*pow(c2,s3,n)%n)
print(libnum.n2s(int(m)))

其数学原理如下:

 RSA低加密指数攻击

该类型攻击一般指数e为3,ctf题目里有非常明显的特征,

 解密脚本如下:

import libnum

def key(c, e, n):
    k = 0
    while True:
        m1 = c + n*k
        result, flag = gmpy2.iroot(m1, e)
        if True == flag:
            return result
        k += 1
n=
e=
c=
m=key(c,e,n)
print(m)
print(libnum.n2s(int(m)).decode())

 在理解脚本时,有一些小白对mod捉摸不清,这里先介绍一下mod,我们可以把它比喻成钟表的运算,在日常面对钟表时,我们通常会说18点为6点,是因为大脑自动进行了mod运算,18mod12=6,而在早上6点是就称作6点,是因为6比12小,6mod12依然为6,可以在此基础上理解脚本。

RSA维纳攻击

维纳攻击是一种e过小或者e过大而进行的攻击方式(自我理解)

其原理因涉及到数论,因此针对一些小白这里直接提供最简便的代码(其实是自己也没理解透彻),有兴趣的可以去康康大佬的博客,

from Crypto.Util.number import *
from gmpy2 import *
from RSAwienerHacker import *

e = 
n=
c=


d=hack_RSA(e,n)
flag=long_to_bytes(pow(c,d,n))
print(flag)

在这个脚本里有以下几点需要注意。

首先是引用的RSAwienerHacker库不是python里的,它取自于github,如:文件地址

其次是Crypto库的使用,该库不可以在windows系统里使用(我的不行),需要在kali虚拟机里,使用python进行解密。这个使用pip的教程网上都是,可以自行查找也可以留言。

easyrsa6

这个主要是针对于一个题型,此次红明谷举办的赛事里也有这种类型的题目。

import gmpy2,libnum
from Crypto.Util.number import getPrime
from secret import flag

e = 0x10001
p = getPrime(1024)
q = gmpy2.next_prime(p)
n = p * q
print("n =",n)
m = libnum.s2n(flag)
c = pow(m,e,n)
print("c =", c)

这是easyrsa6的题目,该类型题目运行脚本会报错,因为它找不到flag模块,这里的第三行代码,就是说题目要读取内容,相当于flag=flag{***},如果要解题的话还是要自行解决脚本。

解题代码如下:

import gmpy2
import libnum

p=
q=
e=
n= 
c=

phi_n=(p-1)*(q-1)
d=gmpy2.invert(e,phi_n)
m=pow(c,d,n)
print(libnum.n2s(int(m)))

easyrsa7

 这是ctfshow中的rsa题目,其余的均采用了16进制可以理解,唯一疑惑的地方就是p>>128<<128

这其实是代表着p低位数据的损失,低位数据跟高位数据就相当于,你的银行卡存款的第一位+1和最后一位+1,感觉是完全不一样的,前者是高位,后者是低位。对于这种情况,应使用sagemath恢复数据,

 在恢复了p之后,解密也就和之前的解法一样了。

(题目里给出的16进制数,换与不换均不影响结果。)

enc与pem文件

有些题目下载附件之后即给出了enc文件与pem文件,这些均需要在linux里打开,对于这些题目,

我们的思路为

1.先破解公钥,找到其中的e和n

2 将n分解开来,找到两个质因数p和q

3.在python中编写rsa的简单脚本,找出明文。

1.首先在kali里输入openssl rsa -pubin -text -modulus -in warmup -in ****.pem,即可得到e和n

 2在网站上输入factordb.com,即可对n进行质因数分解,记得提前把n从十六进制转换为十进制。

3编写脚本解密

easyrsa8

这其中给了flag文件和key文件,则我们的解题思路是先从key文件中取出密钥,即e和n,然后再对n进行质因数分解,最后将得到的pq等等结合就好,

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from numpy import long
import gmpy2
import binascii

public = RSA.importKey(open('public.key').read())#因为该文件的命名是.key,因此可以使用该函数读取其中的密钥,也就是e和n
n = long(public.n)
e = long(public.e)
print(n)
print(e)
p = 97
q = 106249972159566919549855203174197828387397831115262336234662051342543151219702510584956705611794290291345944183845955839244363030579896461607496959399297130227066841321473005074379950936513608503266587950271044991876848389878395867601515004796212227929894460104645781488319246866661398816686697306692491058609
d = 4520639064487098151327174667961365516283539231992543792882057746866179464294032313887767783621724945557985447874376379715922452725597335427159165685648572663979688014560576024497341124412004366514253110547369977143739781801290219136578513871764574450392367530817034216313429071683911546803031169524669257788417
rsakey = RSA.importKey(open('public.key','r').read())
privatekey = RSA.construct((n,e,d,p,q))#该函数可以使其由数据变成公钥
rsa = PKCS1_OAEP.new(privatekey)#得到密码
m = rsa.decrypt(open('flag.enc','rb').read())
print(m)

该脚本也借鉴于是Mumuzi的博客_CSDN博客-ctf,ctfshow,笔记领域博主https://blog.csdn.net/qq_42880719?type=blog

N不互素(funnyrsa1)

简单来说,N不互素就是指该题目中的两个n生成时使用了相同的p或q,同一个m和e,有多个n与c时,那么n可能为共享素数。如ctfshow中的funnyrsa1.

e1 = 14606334023791426
p1 = 121009772735460235364940622989433807619211926015494087453674747614331295040063679722422298286549493698150690694965106103822315378461970129912436074962111424616439032849788953648286506433464358834178903821069564798378666159882090757625817745990230736982709059859613843100974349380542982235135982530318438330859
q1 = 130968576816900149996914427770826228884925960001279609559095138835900329492765336419489982304805369724685145941218640504262821549441728192761733409684831633194346504685627189375724517070780334885673563409259345291959439026700006694655545512308390416859315892447092639503318475587220630455745460309886030186593
c1 = 11402389955595766056824801105373550411371729054679429421548608725777586555536302409478824585455648944737304660137306241012321255955693234304201530700362069004620531537922710568821152217381257446478619320278993539785699090234418603086426252498046106436360959622415398647198014716351359752734123844386459925553497427680448633869522591650121047156082228109421246662020164222925272078687550896012363926358633323439494967417041681357707006545728719651494384317497942177993032739778398001952201667284323691607312819796036779374423837576479275454953999865750584684592993292347483309178232523897058253412878901324740104919248

e2 = 13813369129257838
p2 = 121009772735460235364940622989433807619211926015494087453674747614331295040063679722422298286549493698150690694965106103822315378461970129912436074962111424616439032849788953648286506433464358834178903821069564798378666159882090757625817745990230736982709059859613843100974349380542982235135982530318438330859
q2 = 94582257784130735233174402362819395926641026753071039760251190444144495369829487705195913337502962816079184062352678128843179586054535283861793827497892600954650126991213176547276006780610945133603745974181504975165082485845571788686928859549252522952174376071500707863379238688200493621993937563296490615649
c2 = 7984888899827615209197324489527982755561403577403539988687419233579203660429542197972867526015619223510964699107198708420785278262082902359114040327940253582108364104049849773108799812000586446829979564395322118616382603675257162995702363051699403525169767736410365076696890117813211614468971386159587698853722658492385717150691206731593509168262529568464496911821756352254486299361607604338523750318977620039669792468240086472218586697386948479265417452517073901655900118259488507311321060895347770921790483894095085039802955700146474474606794444308825840221205073230671387989412399673375520605000270180367035526919

 由题干可以发现,该题目使用了相同的p,不同的n和c,是N不互素的题型,但现在的问题是e也不同,则现在就应该联想到,我们可能需要去化简e,使两个e相同,进而不断的改变c。

import math

e1=14606334023791426
e2=13813369129257838
e3=math.gcd(14606334023791426,13813369129257838)
print(e3)

运行该代码段,可以发现它们的公因数是14,也就是2*7,分别约去14后,我们得到的e1和e2分别为:1043309573127959和986669223518417,这时的e改变了,那我们就要开始对c进行转换成与e1,e2相对应的数字,代码如下

import libnum
import gmpy2
import  binascii

p1=121009772735460235364940622989433807619211926015494087453674747614331295040063679722422298286549493698150690694965106103822315378461970129912436074962111424616439032849788953648286506433464358834178903821069564798378666159882090757625817745990230736982709059859613843100974349380542982235135982530318438330859
q1=130968576816900149996914427770826228884925960001279609559095138835900329492765336419489982304805369724685145941218640504262821549441728192761733409684831633194346504685627189375724517070780334885673563409259345291959439026700006694655545512308390416859315892447092639503318475587220630455745460309886030186593
e=1043309573127959
c1=11402389955595766056824801105373550411371729054679429421548608725777586555536302409478824585455648944737304660137306241012321255955693234304201530700362069004620531537922710568821152217381257446478619320278993539785699090234418603086426252498046106436360959622415398647198014716351359752734123844386459925553497427680448633869522591650121047156082228109421246662020164222925272078687550896012363926358633323439494967417041681357707006545728719651494384317497942177993032739778398001952201667284323691607312819796036779374423837576479275454953999865750584684592993292347483309178232523897058253412878901324740104919248
n1=p1*q1
phi_n=(p1-1)*(q1-1)
d1=gmpy2.invert(e,phi_n)
c1_14=pow(c1,d1,n1)

e2 = 986669223518417
p2 = 121009772735460235364940622989433807619211926015494087453674747614331295040063679722422298286549493698150690694965106103822315378461970129912436074962111424616439032849788953648286506433464358834178903821069564798378666159882090757625817745990230736982709059859613843100974349380542982235135982530318438330859
q2 = 94582257784130735233174402362819395926641026753071039760251190444144495369829487705195913337502962816079184062352678128843179586054535283861793827497892600954650126991213176547276006780610945133603745974181504975165082485845571788686928859549252522952174376071500707863379238688200493621993937563296490615649
c2 = 7984888899827615209197324489527982755561403577403539988687419233579203660429542197972867526015619223510964699107198708420785278262082902359114040327940253582108364104049849773108799812000586446829979564395322118616382603675257162995702363051699403525169767736410365076696890117813211614468971386159587698853722658492385717150691206731593509168262529568464496911821756352254486299361607604338523750318977620039669792468240086472218586697386948479265417452517073901655900118259488507311321060895347770921790483894095085039802955700146474474606794444308825840221205073230671387989412399673375520605000270180367035526919
n2=p2*q2
phi_n2=(p2-1)*(q2-1)
d2=gmpy2.invert(e2,phi_n2)
c2_14=pow(c2,d2,n2)

print("c1=",c1_14)
print("c2=",c2_14)

这时我们就得到了n=14的c1,c2,此时有

m的14次=c1​ (mod q1​)

m的14次≡c2​ (mod q2​)

根据中国剩余定理,得到m的14次=c3 (mod q1*q2)

则我们需要求解c3,就需要使用代码

c3=libnum.solve_crt([c1,c2], [q1,q2])

之后即可得到c3,因为此时的e=14=2*7,且7与(q1-1)*(q2-1)是互素的,因此可以再进行化简,重复第一个步骤的操作,就有e=2的c=1468508928650711840448592864366550012730179472363882262465351327446412035872207980397128114769992338577161

则现在e=2,p=q1,q=q2,c也已知,即可求解m,

import gmpy2
import  libnum
from Crypto.Util.number import long_to_bytes

c = 1468508928650711840448592864366550012730179472363882262465351327446412035872207980397128114769992338577161
p = 130968576816900149996914427770826228884925960001279609559095138835900329492765336419489982304805369724685145941218640504262821549441728192761733409684831633194346504685627189375724517070780334885673563409259345291959439026700006694655545512308390416859315892447092639503318475587220630455745460309886030186593
q = 94582257784130735233174402362819395926641026753071039760251190444144495369829487705195913337502962816079184062352678128843179586054535283861793827497892600954650126991213176547276006780610945133603745974181504975165082485845571788686928859549252522952174376071500707863379238688200493621993937563296490615649
e = 2
n = p*q

print(long_to_bytes(gmpy2.iroot(c, 2)[0]).decode())

最后的结果是这样

中间的\xcf\x86代表希腊字母里的第21个字母,也就是φ,具体的参考表,参照如下,撇号表示为–tm。哪个PHP函数将其显示为'?你解码了什么?- 每日博客

参考博客:4XWi11的博客_CSDN博客-树哥让我天天写之Crypto,算法好水,20210201涅普冬令营领域博主https://blog.csdn.net/m0_49109277?type=blog

funnyrsa2

观察代码,该题目与之前不一样的是,该题用了三个随机素数组成了n,但不妨碍解题,依然使用

factordb.com分解素数即可(不用区分三个数里哪个是pq,哪个是r)。脚本如下,

from Crypto.Util.number import *
import gmpy2

e = 0x10001
p = 876391552113414716726089
q = 932470255754103340237147
r = 1098382268985762240184333
n = 897607935780955837078784515115186203180822213482989041398073067996023639
c = 490571531583321382715358426750276448536961994273309958885670149895389968
d= gmpy2.invert(e,(p-1)*(q-1)*(r-1))
m = pow(c,d,n)
print(long_to_bytes(m))

 funnyrsa3(dp泄露)

在解题之前,还是先简单介绍一下dp泄露,

dp=d%(p-1)
dq=d% (q-1)

dp泄露的原理就是这样,在了解清楚数学原理之后,编写脚本就很轻松

import libnum
import gmpy2

e = 65537
n = 13851998696110232034312408768370264747862778787235362033287301947690834384177869107768578977872169953363148442670412868565346964490724532894099772144625540138618913694240688555684873934424471837897053658485573395777349902581306875149677867098014969597240339327588421766510008083189109825385296069501377605893298996953970043168244444585264894721914216744153344106498382558756181912535774309211692338879110643793628550244212618635476290699881188640645260075209594318725693972840846967120418641315829098807385382509029722923894508557890331485536938749583463709142484622852210528766911899504093351926912519458381934550361
dp = 100611735902103791101540576986246738909129436434351921338402204616138072968334504710528544150282236463859239501881283845616704984276951309172293190252510177093383836388627040387414351112878231476909883325883401542820439430154583554163420769232994455628864269732485342860663552714235811175102557578574454173473
c = 6181444980714386809771037400474840421684417066099228619603249443862056564342775884427843519992558503521271217237572084931179577274213056759651748072521423406391343404390036640425926587772914253834826777952428924120724879097154106281898045222573790203042535146780386650453819006195025203611969467741808115336980555931965932953399428393416196507391201647015490298928857521725626891994892890499900822051002774649242597456942480104711177604984775375394980504583557491508969320498603227402590571065045541654263605281038512927133012338467311855856106905424708532806690350246294477230699496179884682385040569548652234893413
for i in range(1,65538):
    p=(dp*e-1)//i+1
    if n%p==0:
        q=n//p
        break
phi_n= (p-1)*(q-1)
d=gmpy2.invert(e,phi_n)
m=pow(c,d,n)
flag=libnum.n2s(int(m)).decode()
print(flag)

运行程序即可得到flag

unusualrsa1

该题目类似于之前的easyrsa7题目,也是有关于低位数据缺失,那么首先就要了解清楚低位缺失的原理,

 从题目给出的脚本可以看出,它丢失了明文的后315位,而且部分信息条件已知,那我们就可以用上次在easyrsa7中用到的sagemath解题,脚本与上次的类似,

n = 14113948189208713011909396304970377626324044633561155020366406284451614054260708934598840781397326960921718892801653205159753091559901114082556464576477585198060530094478860626532455065960136263963965819002575418616768412539016154873800614138683106056209070597212668250136909436974469812231498651367459717175769611385545792201291192023843434476550550829737236225181770896867698281325858412643953550465132756142888893550007041167700300621499970661661288422834479368072744930285128061160879720771910458653611076539210357701565156322144818787619821653007453741709031635862923191561438148729294430924288173571196757351837
mbar = 1520800285708753284739523608878585974609134243280728660335545667177630830064371336150456537012842986526527904043383436211487979254140749228004148347597566264500276581990635110200009305900689510908049771218073767918907869112593870878204145615928290375086195098919355531430003571366638390993296583488184959318678321571278510231561645872308920917404996519309473979203661442792048291421574603018835698487725981963573816645574675640357569465990665689618997534740389987351864738104038598104713275375385003471306823348792559733332094774873827383320058176803218213042061965933143968710199376164960850951030741280074168795136
c = 6635663565033382363211849843446648120305449056573116171933923595209656581213410699649926913276685818674688954045817246263487415328838542489103709103428412175252447323358040041217431171817865818374522191881448865227314554997131690963910348820833080760482835650538394814181656599175839964284713498394589419605748581347163389157651739759144560719049281761889094518791244702056048080280278984031050608249265997808217512349309696532160108250480622956599732443714546043439089844571655280770141647694859907985919056009576606333143546094941635324929407538860140272562570973340199814409134962729885962133342668270226853146819
e = 3
kbits = 315

PR.<x>=PolynomialRing(Zmod(n))
f = (mbar + x) ^ e - c
x0 = f.small_roots(X=2^kbits, beta=1)[0]  # find root < 2^kbits with factor = n

print(x0)

 该代码需要在sagemath上运行,python里会报错。然后我们就得到了缺失的x0,

x0=61514818447540079794645696540635377371238400473636364474100415817811801375393480494308563648125

那么我们现在已知m的高位与低位,加起来之后,直接long_to_bytes,再放到kali里运行即可。


"""
n = 14113948189208713011909396304970377626324044633561155020366406284451614054260708934598840781397326960921718892801653205159753091559901114082556464576477585198060530094478860626532455065960136263963965819002575418616768412539016154873800614138683106056209070597212668250136909436974469812231498651367459717175769611385545792201291192023843434476550550829737236225181770896867698281325858412643953550465132756142888893550007041167700300621499970661661288422834479368072744930285128061160879720771910458653611076539210357701565156322144818787619821653007453741709031635862923191561438148729294430924288173571196757351837
mbar = 1520800285708753284739523608878585974609134243280728660335545667177630830064371336150456537012842986526527904043383436211487979254140749228004148347597566264500276581990635110200009305900689510908049771218073767918907869112593870878204145615928290375086195098919355531430003571366638390993296583488184959318678321571278510231561645872308920917404996519309473979203661442792048291421574603018835698487725981963573816645574675640357569465990665689618997534740389987351864738104038598104713275375385003471306823348792559733332094774873827383320058176803218213042061965933143968710199376164960850951030741280074168795136
c = 6635663565033382363211849843446648120305449056573116171933923595209656581213410699649926913276685818674688954045817246263487415328838542489103709103428412175252447323358040041217431171817865818374522191881448865227314554997131690963910348820833080760482835650538394814181656599175839964284713498394589419605748581347163389157651739759144560719049281761889094518791244702056048080280278984031050608249265997808217512349309696532160108250480622956599732443714546043439089844571655280770141647694859907985919056009576606333143546094941635324929407538860140272562570973340199814409134962729885962133342668270226853146819
e = 3
kbits = 315

PR.<x>=PolynomialRing(Zmod(n))
f = (mbar + x) ^ e - c
x0 = f.small_roots(X=2^kbits, beta=1)[0]

print(x0)
"""
from Crypto.Util.number import long_to_bytes
m2 = 61514818447540079794645696540635377371238400473636364474100415817811801375393480494308563648125
m1= 1520800285708753284739523608878585974609134243280728660335545667177630830064371336150456537012842986526527904043383436211487979254140749228004148347597566264500276581990635110200009305900689510908049771218073767918907869112593870878204145615928290375086195098919355531430003571366638390993296583488184959318678321571278510231561645872308920917404996519309473979203661442792048291421574603018835698487725981963573816645574675640357569465990665689618997534740389987351864738104038598104713275375385003471306823348792559733332094774873827383320058176803218213042061965933143968710199376164960850951030741280074168795136
m =  m1+m2
print(long_to_bytes(m))








b'\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0cflag{r54__c0pp3r5m17h_p4r714l_m_4774ck_15_c00l~}'
                                                                                                                                                                                  

以上分别为解题的脚本和最后的输出结果,over。

unusualrsa2

# ********************
# @Author: Lazzaro
# ********************

from Crypto.Util.number import getPrime,bytes_to_long,long_to_bytes
from functools import reduce
from secret import flag, x, y

m = bytes_to_long(flag)
p = getPrime(1024)
q = getPrime(1024)
n = p*q
print(n)

assert(reduce(lambda x,y:x&y,[(i-5)*i+6==0 for i in x]))
assert(reduce(lambda x,y:x&y,[(j-15)*j+44==0 for j in y]))
"""
reduce模块的作用是对参数序列的元素进行累计,也就是将x和y相加
assert语句是调试的一部分,如果该语句正常,则继续运行,若该程序出错,则终止程序
"""
print(pow(reduce(lambda x,y:x*m+y,x),17,n))
print(pow(reduce(lambda x,y:x*m+y,y),17,n))

#n=23772599983135215481563178266884362291876571759991288577057472733374903836591330410574958472090396886895304944176208711481780781286891334062794555288959410390926474473859289842654809538435377431088422352076225067494924657598298955407771484146155998883073439266427190212827600119365643065276814044272790573450938596830336430371987561905132579730619341196199420897034988685012777895002554746080384319298123154671447844799088258541911028041717897434816921424155687677867019535399434825468160227242441375503664915265223696139025407768146464383537556265875013085702422829200814612395116961538432886116917063119749068212699
#add_x=10900151504654409767059699202929100225155892269473271859207513720755903691031362539478242920144073599515746938827937863835169270383721094542639011665235593065932998091574636525973099426040452626893461449084383663453549354608769727777329036059746386523843912382289597182615339786437186169811342356085836838520978047561127661777189045888648773949147220411427306098338616422692914110656004863767719312410906124366000507952960331116878197129010412361636679449281808407214524741732730279777729251515759320442591663641984363061618865267606007355576230009922421807527598213455112981354590909603317525854070358390622096569841
#add_y=17298679220717326374674940612143058330715465693318467692839033642321129433471254547497087746971317567301086124779289015934582615377165560688447452762043163082394944604062014490446763247008217251611443338103074143809936437694543761369945095202092750900940979469994907399829695696313513303922266742415376818434932335640062684245008822643258497589196668426788916969378417960200705779461808292296450298558001909603602502604228973101048082095642290047196235959438278631661658312398313171590515776453711432353011579809351076532129444735206408591345372296372378396539831385036814349328459266432393612919118094115543053115450

        以上为题目脚本,从题目中我们可以得到输出的c1,c2以及n,并且我们得到了该题目中x和y的条件。在题目中的assert语句中得知,x&y需要满足后面的方程,对后面的方程求解可得到第一组的x&y为[2,3],第二组为[4,11]。(这是自己的理解,如果有误的话求大佬指出)

       在这里可以简单了解一下reduce函数,

reduce(function, iterable[, initializer])
  • function – 函数,有两个参数
  • iterable – 可迭代对象
  • initializer – 可选,初始参数

         那么接下来看最后两句代码,即可得到两句公式:

 从最后得到的c1,c2的公式可知,有一定的线性关系,是Related Message Attack。

参考脚本:

import binascii
def attack(c1, c2, n, e):
    PR.<x>=PolynomialRing(Zmod(n))

    g1 = (2*x+3)^e - c1
    g2 = (4*x+11)^e - c2

    def gcd(g1, g2):
        while g2:
            g1, g2 = g2, g1 % g2
        return g1.monic()
    return -gcd(g1, g2)[0]
c1=10900151504654409767059699202929100225155892269473271859207513720755903691031362539478242920144073599515746938827937863835169270383721094542639011665235593065932998091574636525973099426040452626893461449084383663453549354608769727777329036059746386523843912382289597182615339786437186169811342356085836838520978047561127661777189045888648773949147220411427306098338616422692914110656004863767719312410906124366000507952960331116878197129010412361636679449281808407214524741732730279777729251515759320442591663641984363061618865267606007355576230009922421807527598213455112981354590909603317525854070358390622096569841
c2=17298679220717326374674940612143058330715465693318467692839033642321129433471254547497087746971317567301086124779289015934582615377165560688447452762043163082394944604062014490446763247008217251611443338103074143809936437694543761369945095202092750900940979469994907399829695696313513303922266742415376818434932335640062684245008822643258497589196668426788916969378417960200705779461808292296450298558001909603602502604228973101048082095642290047196235959438278631661658312398313171590515776453711432353011579809351076532129444735206408591345372296372378396539831385036814349328459266432393612919118094115543053115450

n =23772599983135215481563178266884362291876571759991288577057472733374903836591330410574958472090396886895304944176208711481780781286891334062794555288959410390926474473859289842654809538435377431088422352076225067494924657598298955407771484146155998883073439266427190212827600119365643065276814044272790573450938596830336430371987561905132579730619341196199420897034988685012777895002554746080384319298123154671447844799088258541911028041717897434816921424155687677867019535399434825468160227242441375503664915265223696139025407768146464383537556265875013085702422829200814612395116961538432886116917063119749068212699
e =17
m1 = attack(c1, c2, n, e)
print(binascii.unhexlify("%x" % m1))

unusualrsa3

该题目给出了这样的数字

 该txt告诉了我们p  N  C  与e的值,与平常不同的是,它使用的是多项式rsa

p = 2470567871
R.<x> = PolynomialRing(GF(p))#构造以p为模的,关于x的多项式
N = 1932231392*x^255 + 1432733708*x^254 + 1270867914*x^253 + 1573324635*x^252 + 2378103997*x^251 + 820889786*x^250 + 762279735*x^249 + 1378353578*x^248 + 1226179520*x^247 + 657116276*x^246 + 1264717357*x^245 + 1015587392*x^244 + 849699356*x^243 + 1509168990*x^242 + 2407367106*x^241 + 873379233*x^240 + 2391647981*x^239 + 517715639*x^238 + 828941376*x^237 + 843708018*x^236 + 1526075137*x^235 + 1499291590*x^234 + 235611028*x^233 + 19615265*x^232 + 53338886*x^231 + 434434839*x^230 + 902171938*x^229 + 516444143*x^228 + 1984443642*x^227 + 966493372*x^226 + 1166227650*x^225 + 1824442929*x^224 + 930231465*x^223 + 1664522302*x^222 + 1067203343*x^221 + 28569139*x^220 + 2327926559*x^219 + 899788156*x^218 + 296985783*x^217 + 1144578716*x^216 + 340677494*x^215 + 254306901*x^214 + 766641243*x^213 + 1882320336*x^212 + 2139903463*x^211 + 1904225023*x^210 + 475412928*x^209 + 127723603*x^208 + 2015416361*x^207 + 1500078813*x^206 + 1845826007*x^205 + 797486240*x^204 + 85924125*x^203 + 1921772796*x^202 + 1322682658*x^201 + 2372929383*x^200 + 1323964787*x^199 + 1302258424*x^198 + 271875267*x^197 + 1297768962*x^196 + 2147341770*x^195 + 1665066191*x^194 + 2342921569*x^193 + 1450622685*x^192 + 1453466049*x^191 + 1105227173*x^190 + 2357717379*x^189 + 1044263540*x^188 + 697816284*x^187 + 647124526*x^186 + 1414769298*x^185 + 657373752*x^184 + 91863906*x^183 + 1095083181*x^182 + 658171402*x^181 + 75339882*x^180 + 2216678027*x^179 + 2208320155*x^178 + 1351845267*x^177 + 1740451894*x^176 + 1302531891*x^175 + 320751753*x^174 + 1303477598*x^173 + 783321123*x^172 + 1400145206*x^171 + 1379768234*x^170 + 1191445903*x^169 + 946530449*x^168 + 2008674144*x^167 + 2247371104*x^166 + 1267042416*x^165 + 1795774455*x^164 + 1976911493*x^163 + 167037165*x^162 + 1848717750*x^161 + 573072954*x^160 + 1126046031*x^159 + 376257986*x^158 + 1001726783*x^157 + 2250967824*x^156 + 2339380314*x^155 + 571922874*x^154 + 961000788*x^153 + 306686020*x^152 + 80717392*x^151 + 2454799241*x^150 + 1005427673*x^149 + 1032257735*x^148 + 593980163*x^147 + 1656568780*x^146 + 1865541316*x^145 + 2003844061*x^144 + 1265566902*x^143 + 573548790*x^142 + 494063408*x^141 + 1722266624*x^140 + 938551278*x^139 + 2284832499*x^138 + 597191613*x^137 + 476121126*x^136 + 1237943942*x^135 + 275861976*x^134 + 1603993606*x^133 + 1895285286*x^132 + 589034062*x^131 + 713986937*x^130 + 1206118526*x^129 + 311679750*x^128 + 1989860861*x^127 + 1551409650*x^126 + 2188452501*x^125 + 1175930901*x^124 + 1991529213*x^123 + 2019090583*x^122 + 215965300*x^121 + 532432639*x^120 + 1148806816*x^119 + 493362403*x^118 + 2166920790*x^117 + 185609624*x^116 + 184370704*x^115 + 2141702861*x^114 + 223551915*x^113 + 298497455*x^112 + 722376028*x^111 + 678813029*x^110 + 915121681*x^109 + 1107871854*x^108 + 1369194845*x^107 + 328165402*x^106 + 1792110161*x^105 + 798151427*x^104 + 954952187*x^103 + 471555401*x^102 + 68969853*x^101 + 453598910*x^100 + 2458706380*x^99 + 889221741*x^98 + 320515821*x^97 + 1549538476*x^96 + 909607400*x^95 + 499973742*x^94 + 552728308*x^93 + 1538610725*x^92 + 186272117*x^91 + 862153635*x^90 + 981463824*x^89 + 2400233482*x^88 + 1742475067*x^87 + 437801940*x^86 + 1504315277*x^85 + 1756497351*x^84 + 197089583*x^83 + 2082285292*x^82 + 109369793*x^81 + 2197572728*x^80 + 107235697*x^79 + 567322310*x^78 + 1755205142*x^77 + 1089091449*x^76 + 1993836978*x^75 + 2393709429*x^74 + 170647828*x^73 + 1205814501*x^72 + 2444570340*x^71 + 328372190*x^70 + 1929704306*x^69 + 717796715*x^68 + 1057597610*x^67 + 482243092*x^66 + 277530014*x^65 + 2393168828*x^64 + 12380707*x^63 + 1108646500*x^62 + 637721571*x^61 + 604983755*x^60 + 1142068056*x^59 + 1911643955*x^58 + 1713852330*x^57 + 1757273231*x^56 + 1778819295*x^55 + 957146826*x^54 + 900005615*x^53 + 521467961*x^52 + 1255707235*x^51 + 861871574*x^50 + 397953653*x^49 + 1259753202*x^48 + 471431762*x^47 + 1245956917*x^46 + 1688297180*x^45 + 1536178591*x^44 + 1833258462*x^43 + 1369087493*x^42 + 459426544*x^41 + 418389643*x^40 + 1800239647*x^39 + 2467433889*x^38 + 477713059*x^37 + 1898813986*x^36 + 2202042708*x^35 + 894088738*x^34 + 1204601190*x^33 + 1592921228*x^32 + 2234027582*x^31 + 1308900201*x^30 + 461430959*x^29 + 718926726*x^28 + 2081988029*x^27 + 1337342428*x^26 + 2039153142*x^25 + 1364177470*x^24 + 613659517*x^23 + 853968854*x^22 + 1013582418*x^21 + 1167857934*x^20 + 2014147362*x^19 + 1083466865*x^18 + 1091690302*x^17 + 302196939*x^16 + 1946675573*x^15 + 2450124113*x^14 + 1199066291*x^13 + 401889502*x^12 + 712045611*x^11 + 1850096904*x^10 + 1808400208*x^9 + 1567687877*x^8 + 2013445952*x^7 + 2435360770*x^6 + 2414019676*x^5 + 2277377050*x^4 + 2148341337*x^3 + 1073721716*x^2 + 1045363399*x + 1809685811

c = 922927962*x^254 + 1141958714*x^253 + 295409606*x^252 + 1197491798*x^251 + 2463440866*x^250 + 1671460946*x^249 + 967543123*x^248 + 119796323*x^247 + 1172760592*x^246 + 770640267*x^245 + 1093816376*x^244 + 196379610*x^243 + 2205270506*x^242 + 459693142*x^241 + 829093322*x^240 + 816440689*x^239 + 648546871*x^238 + 1533372161*x^237 + 1349964227*x^236 + 2132166634*x^235 + 403690250*x^234 + 835793319*x^233 + 2056945807*x^232 + 480459588*x^231 + 1401028924*x^230 + 2231055325*x^229 + 1716893325*x^228 + 16299164*x^227 + 1125072063*x^226 + 1903340994*x^225 + 1372971897*x^224 + 242927971*x^223 + 711296789*x^222 + 535407256*x^221 + 976773179*x^220 + 533569974*x^219 + 501041034*x^218 + 326232105*x^217 + 2248775507*x^216 + 1010397596*x^215 + 1641864795*x^214 + 1365178317*x^213 + 1038477612*x^212 + 2201213637*x^211 + 760847531*x^210 + 2072085932*x^209 + 168159257*x^208 + 70202009*x^207 + 1193933930*x^206 + 1559162272*x^205 + 1380642174*x^204 + 1296625644*x^203 + 1338288152*x^202 + 843839510*x^201 + 460174838*x^200 + 660412151*x^199 + 716865491*x^198 + 772161222*x^197 + 924177515*x^196 + 1372790342*x^195 + 320044037*x^194 + 117027412*x^193 + 814803809*x^192 + 1175035545*x^191 + 244769161*x^190 + 2116927976*x^189 + 617780431*x^188 + 342577832*x^187 + 356586691*x^186 + 695795444*x^185 + 281750528*x^184 + 133432552*x^183 + 741747447*x^182 + 2138036298*x^181 + 524386605*x^180 + 1231287380*x^179 + 1246706891*x^178 + 69277523*x^177 + 2124927225*x^176 + 2334697345*x^175 + 1769733543*x^174 + 2248037872*x^173 + 1899902290*x^172 + 409421149*x^171 + 1223261878*x^170 + 666594221*x^169 + 1795456341*x^168 + 406003299*x^167 + 992699270*x^166 + 2201384104*x^165 + 907692883*x^164 + 1667882231*x^163 + 1414341647*x^162 + 1592159752*x^161 + 28054099*x^160 + 2184618098*x^159 + 2047102725*x^158 + 103202495*x^157 + 1803852525*x^156 + 446464179*x^155 + 909116906*x^154 + 1541693644*x^153 + 166545130*x^152 + 2283548843*x^151 + 2348768005*x^150 + 71682607*x^149 + 484339546*x^148 + 669511666*x^147 + 2110974006*x^146 + 1634563992*x^145 + 1810433926*x^144 + 2388805064*x^143 + 1200258695*x^142 + 1555191384*x^141 + 363842947*x^140 + 1105757887*x^139 + 402111289*x^138 + 361094351*x^137 + 1788238752*x^136 + 2017677334*x^135 + 1506224550*x^134 + 648916609*x^133 + 2008973424*x^132 + 2452922307*x^131 + 1446527028*x^130 + 29659632*x^129 + 627390142*x^128 + 1695661760*x^127 + 734686497*x^126 + 227059690*x^125 + 1219692361*x^124 + 635166359*x^123 + 428703291*x^122 + 2334823064*x^121 + 204888978*x^120 + 1694957361*x^119 + 94211180*x^118 + 2207723563*x^117 + 872340606*x^116 + 46197669*x^115 + 710312088*x^114 + 305132032*x^113 + 1621042631*x^112 + 2023404084*x^111 + 2169254305*x^110 + 463525650*x^109 + 2349964255*x^108 + 626689949*x^107 + 2072533779*x^106 + 177264308*x^105 + 153948342*x^104 + 1992646054*x^103 + 2379817214*x^102 + 1396334187*x^101 + 2254165812*x^100 + 1300455472*x^99 + 2396842759*x^98 + 2398953180*x^97 + 88249450*x^96 + 1726340322*x^95 + 2004986735*x^94 + 2446249940*x^93 + 520126803*x^92 + 821544954*x^91 + 1177737015*x^90 + 676286546*x^89 + 1519043368*x^88 + 224894464*x^87 + 1742023262*x^86 + 142627164*x^85 + 1427710141*x^84 + 1504189919*x^83 + 688315682*x^82 + 1397842239*x^81 + 435187331*x^80 + 433176780*x^79 + 454834357*x^78 + 1046713282*x^77 + 1208458516*x^76 + 811240741*x^75 + 151611952*x^74 + 164192249*x^73 + 353336244*x^72 + 1779538914*x^71 + 1489144873*x^70 + 213140082*x^69 + 1874778522*x^68 + 908618863*x^67 + 1058334731*x^66 + 1706255211*x^65 + 708134837*x^64 + 1382118347*x^63 + 2111915733*x^62 + 1273497300*x^61 + 368639880*x^60 + 1652005004*x^59 + 1977610754*x^58 + 1412680185*x^57 + 2312775720*x^56 + 59793381*x^55 + 1345145822*x^54 + 627534850*x^53 + 2159477761*x^52 + 10450988*x^51 + 1479007796*x^50 + 2082579205*x^49 + 1158447154*x^48 + 126359830*x^47 + 393411272*x^46 + 2343384236*x^45 + 2191577465*x^44 + 1281188680*x^43 + 230049708*x^42 + 539600199*x^41 + 1711135601*x^40 + 1659775448*x^39 + 1716176055*x^38 + 904363231*x^37 + 2385749710*x^36 + 567278351*x^35 + 404199078*x^34 + 372670353*x^33 + 1286079784*x^32 + 1744355671*x^31 + 2316856064*x^30 + 2106475476*x^29 + 614988454*x^28 + 2149964943*x^27 + 1065233185*x^26 + 188130174*x^25 + 540415659*x^24 + 1031409799*x^23 + 1067085678*x^22 + 1005161755*x^21 + 249654085*x^20 + 1816791634*x^19 + 1437500292*x^18 + 448596413*x^17 + 2397497659*x^16 + 2353732701*x^15 + 2068949189*x^14 + 1826419168*x^13 + 1265366199*x^12 + 547031306*x^11 + 1016962374*x^10 + 160089486*x^9 + 2264803979*x^8 + 1081806194*x^7 + 824215340*x^6 + 497731793*x^5 + 45017166*x^4 + 317548920*x^3 + 1391127733*x^2 + 1752881284*x + 1290424106
S.<x> = R.quotient(N)#关于x的瑞利定理

P, Q = N.factor()
P, Q = P[0], Q[0]
phi = (p ** P.degree() - 1) * (p ** Q.degree() - 1)
e = 0x10001
d = inverse_mod(e, phi)

m = pow(c,d,N)
m = "".join([chr(c) for c in m.list()])
print(m)

这里感谢大佬的释疑解惑:

4XWi11 

2022红明谷rsa

from Crypto.Util.number import *
import os

def gen():
    e = 3
    while True:
        try:
            p = getPrime(512)
            q = getPrime(512)
            n = p*q
            phi = (p-1)*(q-1)
            d = inverse(e,phi)
            return p,q,d,n,e
        except:
            continue
    return
p,q,d,n,e = gen()
r = getPrime(512)
m = bytes_to_long(flag+os.urandom(32))
M = m%r
c = pow(m,e,n)
print("r = %d"%r)
print("M = %d"%M)
print("n = %d"%n)
print("e = %d"%e)
print("c = %d"%c)
'''
r = 7996728164495259362822258548434922741290100998149465194487628664864256950051236186227986990712837371289585870678059397413537714250530572338774305952904473
M = 4159518144549137412048572485195536187606187833861349516326031843059872501654790226936115271091120509781872925030241137272462161485445491493686121954785558
n = 131552964273731742744001439326470035414270864348139594004117959631286500198956302913377947920677525319260242121507196043323292374736595943942956194902814842206268870941485429339132421676367167621812260482624743821671183297023718573293452354284932348802548838847981916748951828826237112194142035380559020560287
e = 3
c = 46794664006708417132147941918719938365671485176293172014575392203162005813544444720181151046818648417346292288656741056411780813044749520725718927535262618317679844671500204720286218754536643881483749892207516758305694529993542296670281548111692443639662220578293714396224325591697834572209746048616144307282
'''

首先对出题脚本做一个解读,该脚本首先定义了一个函数,生成5个数字,在生成一个r,明文为在需要求得的flag后加入了32位字符串,然后就生成c,最后给出了5个数字的值。

那么,我们在写解密脚本的时候,就有了思路:该密文的生成主要经过了

M=m%r,c=m^{e}%n

m=M+k*r

c=(M+k*r)^{e}%n

因此我知道了,现在需要去求解k,最常用的方法,就是创建一个链表来求解

r = 7996728164495259362822258548434922741290100998149465194487628664864256950051236186227986990712837371289585870678059397413537714250530572338774305952904473
M = 4159518144549137412048572485195536187606187833861349516326031843059872501654790226936115271091120509781872925030241137272462161485445491493686121954785558
n = 131552964273731742744001439326470035414270864348139594004117959631286500198956302913377947920677525319260242121507196043323292374736595943942956194902814842206268870941485429339132421676367167621812260482624743821671183297023718573293452354284932348802548838847981916748951828826237112194142035380559020560287
e = 3
c = 46794664006708417132147941918719938365671485176293172014575392203162005813544444720181151046818648417346292288656741056411780813044749520725718927535262618317679844671500204720286218754536643881483749892207516758305694529993542296670281548111692443639662220578293714396224325591697834572209746048616144307282

PR.<x> = PolynomialRing(Zmod(n))
m0 = x*r + M
f = (m0^3) - c
f = f.monic()#这里的monic函数是判断方程必定有根
x0 = f.small_roots(X=2^100,beta=1)[0]
print(x0)
m=M+x0*r
print(m)

在求解出m之后,再使用long_to_bytes转换m即可求得flag。

best_rsa

该题目提供了pem文件,则需要打开kali虚拟机,把题目给出的文件拖到虚拟机里,执行以下代码

openssl rsa -pubin -text -modulus -in publickey1.pem
openssl rsa -pubin -text -modulus -in publickey2.pem

一般的pem文件都可以使用该代码来读取信息,因为该文件中的模均相同,所以这是共模攻击,但是与之前提到的共模攻击不一样,这次的信息较多,

from Crypto.PublicKey import RSA
import libnum
import gmpy2

c1=libnum.s2n(open('cipher1.txt','rb').read())
c2=libnum.s2n(open('cipher2.txt','rb').read())

pub1=RSA.importKey(open('publickey1.pem').read())
pub2=RSA.importKey(open('publickey2.pem').read())
n = pub1.n
e1= pub1.e
e2= pub2.e

s = gmpy2.gcdext(e1,e2)
s1 = s[1]
s2 = s[2]

if s1<0:
	s1 = -s1
	c1 = gmpy2.invert(c1, n)
elif s2<0:
	s2 = -s2
	c2 = gmpy2.invert(c2, n)

m = pow(c1,s1,n)*pow(c2,s2,n) % n
flag = libnum.n2s(int(m))
print(flag)

但是与之前的脚本差距不大,然后Crypto模块拖到kali运行,即可有正确答案。

unusualrsa4

# ********************
# @Author: Lazzaro
# ********************

from Crypto.Util.number import getPrime,bytes_to_long
from gmpy2 import invert
from secret import flag

m = bytes_to_long(flag)
p = getPrime(1024)
q = getPrime(1024)
n = p*q
print(invert(q,p))

e = 0x10001
d = invert(e,(p-1)*(q-1))
print(d)

c = pow(m,e,n)
print(c)


#113350138578125471637271827037682321496361317426731366252238155037440385105997423113671392038498349668206564266165641194668802966439465128197299073392773586475372002967691512324151673246253769186679521811837698540632534357656221715752733588763108463093085549826122278822507051740839450621887847679420115044512
#27451162557471435115589774083548548295656504741540442329428952622804866596982747294930359990602468139076296433114830591568558281638895221175730257057177963017177029796952153436494826699802526267315286199047856818119832831065330607262567182123834935483241720327760312585050990828017966534872294866865933062292893033455722786996125448961180665396831710915882697366767203858387536850040283296013681157070419459208544201363726008380145444214578735817521392863391376821427153094146080055636026442795625833039248405951946367504865008639190248509000950429593990524808051779361516918410348680313371657111798761410501793645137
#619543409290228183446186073184791934402487500047968659800765382797769750763696880547221266055431306972840980865602729031475343233357485820872268765911041297456664938715949124290204230537793877747551374176167292845717246943780371146830637073310108630812389581197831196039107931968703635129091224513813241403591357678410312272233389708366642638825455844282490676862737715585788829936919637988039113463707959069907015464745700766013573282604376277598510224455044288896809217461295080140187509519005245601483583507547733673523120385089098002298314719617693895392148294399937798485146568296114338393548124451378170302291

这是出题人给出的脚本,观察给出的两个hint,可以想到又是运用数学原理解题,

 这是我自己写下的过程,因为这上面的数学公式太难用了,上面用了不同颜色的笔进行了标注,基本解释了这个数学原理比较难的地方,看懂原理之后,我们就可以试着来写脚本

# -*- coding: utf-8 -*-
from Crypto.Util.number import *
from gmpy2 import *
from itertools import *

q_1 = 113350138578125471637271827037682321496361317426731366252238155037440385105997423113671392038498349668206564266165641194668802966439465128197299073392773586475372002967691512324151673246253769186679521811837698540632534357656221715752733588763108463093085549826122278822507051740839450621887847679420115044512
d = 27451162557471435115589774083548548295656504741540442329428952622804866596982747294930359990602468139076296433114830591568558281638895221175730257057177963017177029796952153436494826699802526267315286199047856818119832831065330607262567182123834935483241720327760312585050990828017966534872294866865933062292893033455722786996125448961180665396831710915882697366767203858387536850040283296013681157070419459208544201363726008380145444214578735817521392863391376821427153094146080055636026442795625833039248405951946367504865008639190248509000950429593990524808051779361516918410348680313371657111798761410501793645137
c = 619543409290228183446186073184791934402487500047968659800765382797769750763696880547221266055431306972840980865602729031475343233357485820872268765911041297456664938715949124290204230537793877747551374176167292845717246943780371146830637073310108630812389581197831196039107931968703635129091224513813241403591357678410312272233389708366642638825455844282490676862737715585788829936919637988039113463707959069907015464745700766013573282604376277598510224455044288896809217461295080140187509519005245601483583507547733673523120385089098002298314719617693895392148294399937798485146568296114338393548124451378170302291
e = 0x10001
#在写脚本要明确自己需要什么,例如从数学原理中我们需要求解k和 φ ,那就去写对应的过程
for k in range(1, e):

    t = e * d - 1#hint1
    if t % k == 0:
        phi = t // k#这里的phi就是 φ
        kp = q_1 * phi - q_1 + 1
        x1 = pow(3, phi, kp) - 1#这里3和5换别的数字也行
        x2 = pow(5, phi, kp) - 1
        x = gcd(x1, x2)
        if x.bit_length() == 1024:
            p = x
            q = invert(q_1, p)
            n, phi = p*q, (p-1)*(q-1)
            assert d == invert(e, phi)
            m = pow(c, d, n)
            print(long_to_bytes(m))
            break

脚本挺简单的,都能看懂,就是数学原理很麻烦,最后为

flag{wh47_1f_y0u_kn0w_1nv3r7_q_p~?}

unusual5

题目给我们提示的是有限域,那就要先了解什么叫有限域问题,

 然后介绍一下根的求法

R.<x> = Zmod(p)[]
f = x ^ e - c
f = f.monic()
res1 = f.roots()

首先创建一个链表,接下来输入公式,使用monic()函数判断一定有根,最后输出res1为一个列表,表中为所有解情况

 这张图片是rsa1和res2的结果这里列出来方便理解,然后再结合之前使用过的crt算法就可以写脚本,这里有两个脚本,第一个是得到res1和res2,第二个是求解最后的flag,

p = 733089589724903586073820965792963746076789390539824437962807679954808310072656817423828613938510684864567664345751164944269489647964227519307980688068059059377123391499328155025962198363435968318689113750910755244276996554328840879221120846257832190569086861774466785101694608744384540722995426474322431441
q = 771182695213910447650732428220054698293987458796864628535794956332865106301119308051373568460701145677164052375651484670636989109023957702790185901445649197004100341656188532246838220216919835415376078688888076677350412398198442910825884505318258393640994788407100699355386681624118606588957344077387058721
n = 9057141637995599750120273501711128117576789048411357158233050845658505488383724832915968443730006384810721595601723748471745315354759415044859624198755098491311647992728384572103262800310263916249536898582100747311978019829291619921741682336800665277699122504431456051606407509905004993708771825443764723285750825546500765451509998514747599779552241055519485714649825416851221219747115910385536482995890893190128149999622905611239433481756073333147782531765685320972075370276543786386451560493093416152466142374684450770169257924330366774896526508005296520372463932722237001341584625279676089901419404816917142209281664709940400762785892142918132066900664643155176180059403739
c = 406314720119562590605554101860453913891646775958515375190169046313074168423687276987576196367702523895650602252851191274766072774312855212771035294337840170341052016067631007495713764510925931612800335613551752201920460877432379214684677593342046715833439574705829048358675771542989832566579493199671622475225225451781214904100440695928239014046619329247750637911015313431804069312072581674845078940868349474663382442540424342613429896445329365750444298236684237769335405534090013035238333534521759502103604033307768304224154383880727399879024077733935062478113298538634071453067782212909271392163928445051705642
e= 20

R.<x> = Zmod(p)[]
f = x ^ e - c
f = f.monic()
res1 = f.roots()

R.<x> = Zmod(q)[]
f = x ^ e - c
f = f.monic()
res2 = f.roots()

print('res1=',res1)
print('res2=',res2)
​

from Crypto.Util.number import  *
import libnum

p = 733089589724903586073820965792963746076789390539824437962807679954808310072656817423828613938510684864567664345751164944269489647964227519307980688068059059377123391499328155025962198363435968318689113750910755244276996554328840879221120846257832190569086861774466785101694608744384540722995426474322431441
q = 771182695213910447650732428220054698293987458796864628535794956332865106301119308051373568460701145677164052375651484670636989109023957702790185901445649197004100341656188532246838220216919835415376078688888076677350412398198442910825884505318258393640994788407100699355386681624118606588957344077387058721
n = 9057141637995599750120273501711128117576789048411357158233050845658505488383724832915968443730006384810721595601723748471745315354759415044859624198755098491311647992728384572103262800310263916249536898582100747311978019829291619921741682336800665277699122504431456051606407509905004993708771825443764723285750825546500765451509998514747599779552241055519485714649825416851221219747115910385536482995890893190128149999622905611239433481756073333147782531765685320972075370276543786386451560493093416152466142374684450770169257924330366774896526508005296520372463932722237001341584625279676089901419404816917142209281664709940400762785892142918132066900664643155176180059403739
c = 406314720119562590605554101860453913891646775958515375190169046313074168423687276987576196367702523895650602252851191274766072774312855212771035294337840170341052016067631007495713764510925931612800335613551752201920460877432379214684677593342046715833439574705829048358675771542989832566579493199671622475225225451781214904100440695928239014046619329247750637911015313431804069312072581674845078940868349474663382442540424342613429896445329365750444298236684237769335405534090013035238333534521759502103604033307768304224154383880727399879024077733935062478113298538634071453067782212909271392163928445051705642
e= 20
res1= [(733089589724903586073820965792963746076789390539824437962807679954808310072656817423828613938510684864567664345751164944269489647964227519307980688068059059377123391499328155021524914773359906357153671313308407478602554319510265005085762860382683102940576377254668898119652133942180899304170329371707150164, 1), (678923693209704969852381465896650019425870260030383443168808423368601278880685781654734919678958092826800695579102973572447355792679848485826507692704568923813419881117766044423608229711484763917744685055881596752291255735808048270822124796786471676188662798449644642235738223466855946913879538299578053411, 1), (660147311087779881169338548239431864756054612093222666998714749603875812596923472248793683967669014397990139199125982652383529516284367882454604795633984744310817735291839918294987220579419849435923577095190063747058566440604560174772236817980884692508913814109640678513633481822665261866470516425285518285, 1), (656681791452498481654878002485070757012861926668381629152705461263764608739765714942200770232734120738697878503189820956143274111419030898383980731253182784661086554234440727684677249747860741931943770929571071514986737487620545162638141249825931408013949005076717924474881049785216136013509251488319927117, 1), (561634200460522548441571592444405305656486299630777500288946777401428565675621685143645766539546494757183424411676233243328896459121008394512864385381461601416842364770773986015469061716463481506742063309940039220834724151367845375195245551456554735236102965922407472562800115189890526203678874326289793386, 1), (523855729860851889306276946831707325654966799659419971520448260469324992736982246556398347232993411720682146837875080052611076416766800095923505800210207882786055877101711481659849019405604591067848093320332336464705973049736439281995689523942160283871387832933740244138033780213828909573276367363492479416, 1), (452900182350324645394395847267638113925741103910755278893324277011525974119646860179890955142636700494806732282459985420380030145373473199426467982810188731584443697329277404941404099390850352429776960314100205943189386609329123884794268027928641499784873014598307047385649837271871153730516499924907326391, 1), (416370840270302176660974325650517943145426999919261282193286142404178655729051589183139277588858670001090144796272169390365633208042166264602215868369443740484308138194802614266542495845026116458981772823239562965336426568684608791041112836390368657079922805426770894892558562813602601339340032501607476909, 1), (415121711750653986259101201654940133924221603939397750124825760079422401181007421592643535836083617458305454708658832229662210103019264900837109397638935012953657209660214900590221340670067523952418432762094850952735077742516293665730069986289396136048668366129437705942925977098013178518939090064725293698, 1), (374350165605081425148746685793120637325474206554845188409530082476947889392734021316375944344139500560536749012063348621966245700071777272257861879442907414106249944743486841972083642001920163764631575788319343002998139757149039708713099627493777332364552928705440787113844863245759659045732547084424923730, 1), (358739424119822160925074279999843108751315183984979249553277597477860420679922796107452669594371184304030915333687816322303243947892450247050118808625151645270873446755841313053878556361515804554057537962591412241278856797179801170508021218764054858204533933069025997987849745498624881677262879389897507711, 1), (317967877974249599814719764138023612152567786600426687837981919875385908891649395831185078102427067406262209637092332714607279544944962618470871290429124046423466181839113254435740857693368444366270680988815904291541918811812547213491050859968436054520418495645029079158768631646371362204056336409597137743, 1), (316718749454601409412846640142445802931362390620563155769521537550629654343605228240689336349652014863477519549478995553903856439922061254705764819698615318892815253304525540759419702518409851859707340927671192278940569985644232088180008009867463533489164056347695890209136045930781939383655393972714954532, 1), (280189407374578940679425118525325632151048286629069159069483402943282335953009957243937658795873984369760932063291179523889459502590754319881512705257870327792679694170050750084558098972585615888912153436810549301087609944999716994426852818329190690784213847176159737716044771472513386992478926549415105050, 1), (209233859864051696767544018961256420421822590880404466442359419485483317335674570867430266705517273143885517507876084891658413231197427423384474887857851176591067514397616673366113178957831377250841020430578418779571023504592401597225431322315671906697699028840726540963660828530555631149719059110829952025, 1), (171455389264381037632249373348558440420303090909046937673860902553379744397035132280182847398964190107384239934074931700940593188843219124795116302686597457960281026728554169010493136646972486811947050440970716023442272402960995504025875294801277455332983895852059312538894493554494014519316552148032638055, 1), (76407798272405104418942963307892989063927463871442808810102218691043701332891102481627843705776564125869785842561343988126215536545196620923999956814876274716036837264887427341284948615575226386745342821339683729290259066708295716582979596431900782555137856697748860626813558959168404709486174986002504324, 1), (72942278637123704904482417553531881320734778446601770964092930350932497475733345175034929970841670466577525146625182291885960131679859636853375892434074315066305656207488236730974977784016118882765536655720691497218430113724280704448884028276947498060173047664826106588061126921719278856524910049036913156, 1), (54165896515198616221439499896313726650919130509440994793999256586207031191971035769093694259552592037766968766648191371822133855284379033481472995363490135563703510381562110602353968651951204400944428695029158491985740818520792608398996049471360514380424063324822142865956385277528593809115888174744378030, 1), (4437283590076061961535442437602347765674442234818575874135357985875149087628510484519797886982042474802203641418825097102615281277, 1)]
res2= [(771182695213910447650732428220054698293987458796864628535794956332865106301119308051373568460701145677164052375651484670636989109023957702790185901445649197004100341656188532242400936626843773453840636251285728911675970163379867036690526519443109306012484303887302812373344206821914965170132246974771777444, 1), (766556454188661342717183441830753958949866417494716064331308873001400640185512932266682617111675304940672393843056881975135838366629757426494074139282338667675175892353840088889909195938963702112766349904198019372558645141384798583534383188883052267525748815134207587721641571601904662560056137622206973489, 1), (697252901173481151775023987079186148934671702095573239439077382532564148810196647295908000048808838516420504907242412420648310319468654564849006791509953203253591756022067984445917819993348421480837383431739473210443093160532003684668638512409524578887612424272056500338100724912084684072451333028872106867, 1), (637570615943233551403071638054753581415867520631437658239907745441121975126808316940174715476757666870531167942851357415707186407189108694841070152707092245309731475509435021166725640089960139181871460785130772780846084823629102758039859600829870217520667468256249544673326609044266687450424519008597139038, 1), (632327011426499994189315859401811158465010667761983995201046936822397685624551714172153350143852663041545272586491513748551456122474881151730273573849696707727190136249011496534362800230143294818909726182851035741169036440948865157971659975327810387018738693032165869052686855576696780303333160382173118510, 1), (603167942216453034450263940413887436639768290870012743059006350279961914111784943446402628202860617054930140073926171776018499373620898476085657301360306887162235526834606378738074341266663645930616670338128013164422994099750629106515540742416974080150559276654019646352903381932805758744985229930025215589, 1), (530466680163017973833308694998245966701824340964042532641356089137879243186221081260746979341797675072378875885003058544820764758046575857925699945279145064801793820479054735901259358468262299180565694104881447986591164582548172740211166270336596928161447369253455709413986912995034928572096835678138857265, 1), (465540790416036816247600567320870976232163306630453288403325643427712780000576148136992329436841499597503643564252159872723910561052802444806966727618888569275392200705998247985267055114981110557102594418390662751642665628146458339378829747379733648664063153493767191146530568857443383384153565928824587631, 1), (416074710242252176401044812192140737030784340427479537135890013577916016785682458613448218282243236911584933444225849145353809933396731357693587977285142052644526842758534194394543388181747428573306033684592492190246496510265285685848535401569621598453391802767037373579958843978087833160938909090562342099, 1), (394136899723687893136044300058080845383423389432958570785292421247573616646066598716388388801380799317671805332945423860473016951589636840148122537670603031835993896874831423637875155136425033395934564235988401408318773868625275203793169278596739058672957150515186335729596174779705646162358930520576725303, 1), (377045795490222554514688128161973852910564069363906057750502535085291489655052709334985179659320346359492247042706060810163972157434320862642063363775046165168106444781357108608963065080494802019441514452899675269031638529573167707032715226721519334968037637891914363625790506844412960426598413556810333418, 1), (355107984971658271249687616027913961263203118369385091399904942754949089515436849437925350178457908765579118931425635525283179175627226345096597924160507144359573498897654337852294832035172406842070045004295584487103915887933157224977349103748636795187602985640063325775427837646030773428018434986824716622, 1), (305641904797873631403131860899183722061824152166411340132469312905152326300543159914381239023859646079660408811399324797913078547971155257983219173826760627728708140950190284261571165101938724858273484270497413925707746770051984571447054757938524744976931634913333508208856112766675223204803778148562471090, 1), (240716015050892473817423733221808731592163117832822095894438867194985863114898226790626589118903470604785176490648426125816224350977381844864485956166504132202306521177133796345578861748657536234810384584006628690759247815650270170614718234981661465479547419153644989941399768629083678016860508399248201456, 1), (168014752997457413200468487806167261654219167926851885476788606052903192189334364604970940257840528622233912301725312894618489735403059226704528600085342309841864814821582153508763878950256189484759408350760063512927418298447813804310343762901284313490435511753081053002483299691312847843972114147361843132, 1), (138855683787410453461416568818243539828976791034880633334748019510467420676567593879220218316848482635618779789159970922085532986549076551059912327595952489276910205407177035712475419986776540596466352506037040936181375957249577752854224529990448006622256095374934830302699826047421826285624183695213940211, 1), (133612079270676896247660790165301116878119938165426970295887210891743131174310991111198852983943478806632884432800127254929802701834849007949115748738556951694368866146753511080112580126959696233504617903757303896504327574569340152786024904488388176120327320150851154682060072579851919138532825068789919683, 1), (73929794040429295875708441140868549359315756701291389096717573800300957490922660755465568411892307160743547468409072249988678789555303137941179109935695993750508585634120547800920400223571413934538695257148603466907319237666439226157245992908733814753382364135044199017285956712033922516506011048514951854, 1), (4626241025249104933548986389300739344121041302148564204486083331464466115606375784690951349025840736491658532594602695501150742394200276296111762163310529328924449302348443356929024277956133302609728784690057304791767256813644327291501316435206126115245973272893111633745110022213944028901206455180085232, 1), (4437283590076061961535442437602347765674442234818575874135357985875149087628510484519797886982042474802203641418825097102615281277, 1)]
for i in res1:
    for j in res2:
        # 普普通通中国剩余定理
        m =libnum.solve_crt([int(i[0]),int(j[0])],[p,q])#c3=libnum.solve_crt([c1,c2], [q1,q2])
        flag = long_to_bytes(m)
        if flag.startswith(b'flag'):
            print(flag)

[点击并拖拽以移动]
​

最后那个if语句就是判断,如果是以后面那个字符串开头的,就输出flag,最后的flag为

b'flag{r54__d34l1n6_w17h_3v3n_3 _&_f1nd1n6_n-7h_r0075~~}'。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐