import gmpy2
n = p*q
I = gmpy2.invert(p, q) #I为p(mod q)的逆元,即p*I = 1(mod q)
mp = gmpy2.powmod(c, dp, p) #计算mp = c^dp % p
mq = gmpy2.powmod(c, dq, q) #计算mq = c^dq % q
m = (mp + (I * (mq - mp)) * p) % n #明文求解公式
m = hex(m)[2:] #转十六进制数据
flag = ''
for i in range(len(m)//2):
flag += chr(int(m[i*2:(i+1)*2], 16))
print(flag)
0x01 RSA2
1
2
3
4
5
e = 65537
n = 248254007851526241177721526698901802985832766176221609612258877371620580060433101538328030305219918697643619814200930679612109885533801335348445023751670478437073055544724280684733298051599167660303645183146161497485358633681492129668802402065797789905550489547645118787266601929429724133167768465309665906113
dp = 905074498052346904643025132879518330691925174573054004621877253318682675055421970943552016695528560364834446303196939207056642927148093290374440210503657
c = 140423670976252696807533673586209400575664282100684119784203527124521188996403826597436883766041879067494280957410201958935737360380801845453829293997433414188838725751796261702622028587211560353362847191060306578510511380965162133472698713063592621028959167072781482562673683090590521214218071160287665180751
import gmpy2
for x in range(1, e):
if(e*dp%x == 1):
p = (e*dp-1)//x+1
if(n%p != 0):
continue
q = n//p
phin = (p-1)*(q-1)
d = gmpy2.invert(e, phin)
m = gmpy2.powmod(c, d, n)
print("flag:",bytes.fromhex(hex(m)[2:]))
0x02 [WUSTCTF2020]babyrsa
c = 28767758880940662779934612526152562406674613203406706867456395986985664083182
n = 73069886771625642807435783661014062604264768481735145873508846925735521695159
e = 65537
import gmpy2
N,p,q,e=920139713,18443,49891,19
d=gmpy2.invert(e,(p-1)*(q-1))
result=[]
with open("data.txt","r") as f:#删掉data.txt的前两行
for line in f.readlines():
line=line.strip('\n')#去掉列表中每一个元素的换行符
result.append(chr(pow(int(line),d,N)))
for i in result:
print(i,end='')
0x05 [HDCTF2019]basic rsa
import gmpy2
from Crypto.Util.number import *
from binascii import a2b_hex,b2a_hex
flag = "*****************"
p = 262248800182277040650192055439906580479
q = 262854994239322828547925595487519915551
e = 65533
n = p*q
c = pow(int(b2a_hex(flag),16),e,n)
print c
# 27565231154623519221597938803435789010285480123476977081867877272451638645710
代码如下
import gmpy2
from Crypto.Util.number import long_to_bytes
phi = (p-1)*(q-1)
d = gmpy2.invert(e,phi)
n = p*q
m = pow(c,d,n)
print (long_to_bytes(m))
0x06 rsa
pub.key如下
-----BEGIN PUBLIC KEY-----
MDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMAzLFxkrkcYL2wch21CM2kQVFpY9+7+
/AvKr1rzQczdAgMBAAE=
-----END PUBLIC KEY-----
from random import choice
from Crypto.Util.number import isPrime, sieve_base as primes
from flag import flag
def getPrime(bits):
while True:
n = 2
while n.bit_length() < bits:
n *= choice(primes)
if isPrime(n + 1):
return n + 1
e = 0x10001
m = int.from_bytes(flag.encode(), 'big')
p, q = [getPrime(2048) for _ in range(2)]
n = p * q
c = pow(m, e, n)
# n = 32849718197337581823002243717057659218502519004386996660885100592872201948834155543125924395614928962750579667346279456710633774501407292473006312537723894221717638059058796679686953564471994009285384798450493756900459225040360430847240975678450171551048783818642467506711424027848778367427338647282428667393241157151675410661015044633282064056800913282016363415202171926089293431012379261585078566301060173689328363696699811123592090204578098276704877408688525618732848817623879899628629300385790344366046641825507767709276622692835393219811283244303899850483748651722336996164724553364097066493953127153066970594638491950199605713033004684970381605908909693802373826516622872100822213645899846325022476318425889580091613323747640467299866189070780620292627043349618839126919699862580579994887507733838561768581933029077488033326056066378869170169389819542928899483936705521710423905128732013121538495096959944889076705471928490092476616709838980562233255542325528398956185421193665359897664110835645928646616337700617883946369110702443135980068553511927115723157704586595844927607636003501038871748639417378062348085980873502535098755568810971926925447913858894180171498580131088992227637341857123607600275137768132347158657063692388249513
# c = 26308018356739853895382240109968894175166731283702927002165268998773708335216338997058314157717147131083296551313334042509806229853341488461087009955203854253313827608275460592785607739091992591431080342664081962030557042784864074533380701014585315663218783130162376176094773010478159362434331787279303302718098735574605469803801873109982473258207444342330633191849040553550708886593340770753064322410889048135425025715982196600650740987076486540674090923181664281515197679745907830107684777248532278645343716263686014941081417914622724906314960249945105011301731247324601620886782967217339340393853616450077105125391982689986178342417223392217085276465471102737594719932347242482670320801063191869471318313514407997326350065187904154229557706351355052446027159972546737213451422978211055778164578782156428466626894026103053360431281644645515155471301826844754338802352846095293421718249819728205538534652212984831283642472071669494851823123552827380737798609829706225744376667082534026874483482483127491533474306552210039386256062116345785870668331513725792053302188276682550672663353937781055621860101624242216671635824311412793495965628876036344731733142759495348248970313655381407241457118743532311394697763283681852908564387282605279108
import gmpy2
import binascii
from Crypto.Util.number import isPrime, sieve_base as primes
#primes为前10000个素数的列表
#计算prd = ∏ primes
prd = 1
for i in primes:
prd *= i
#p为(2^prd-1)和n的公约数
p = gmpy2.gcd(gmpy2.powmod(2,prd,n)-1,n)
q = n // p
d = gmpy2.invert(e,(p-1)*(q-1)) #计算私钥d
m = gmpy2.powmod(c, d, n) #解密
flag = binascii.unhexlify(hex(m)[2:])
print(flag)
0x08 RSA4
1
2
3
4
5
6
7
8
N = 331310324212000030020214312244232222400142410423413104441140203003243002104333214202031202212403400220031202142322434104143104244241214204444443323000244130122022422310201104411044030113302323014101331214303223312402430402404413033243132101010422240133122211400434023222214231402403403200012221023341333340042343122302113410210110221233241303024431330001303404020104442443120130000334110042432010203401440404010003442001223042211442001413004
c = 310020004234033304244200421414413320341301002123030311202340222410301423440312412440240244110200112141140201224032402232131204213012303204422003300004011434102141321223311243242010014140422411342304322201241112402132203101131221223004022003120002110230023341143201404311340311134230140231412201333333142402423134333211302102413111111424430032440123340034044314223400401224111323000242234420441240411021023100222003123214343030122032301042243
N = 302240000040421410144422133334143140011011044322223144412002220243001141141114123223331331304421113021231204322233120121444434210041232214144413244434424302311222143224402302432102242132244032010020113224011121043232143221203424243134044314022212024343100042342002432331144300214212414033414120004344211330224020301223033334324244031204240122301242232011303211220044222411134403012132420311110302442344021122101224411230002203344140143044114
c = 112200203404013430330214124004404423210041321043000303233141423344144222343401042200334033203124030011440014210112103234440312134032123400444344144233020130110134042102220302002413321102022414130443041144240310121020100310104334204234412411424420321211112232031121330310333414423433343322024400121200333330432223421433344122023012440013041401423202210124024431040013414313121123433424113113414422043330422002314144111134142044333404112240344
N = 332200324410041111434222123043121331442103233332422341041340412034230003314420311333101344231212130200312041044324431141033004333110021013020140020011222012300020041342040004002220210223122111314112124333211132230332124022423141214031303144444134403024420111423244424030030003340213032121303213343020401304243330001314023030121034113334404440421242240113103203013341231330004332040302440011324004130324034323430143102401440130242321424020323
c = 10013444120141130322433204124002242224332334011124210012440241402342100410331131441303242011002101323040403311120421304422222200324402244243322422444414043342130111111330022213203030324422101133032212042042243101434342203204121042113212104212423330331134311311114143200011240002111312122234340003403312040401043021433112031334324322123304112340014030132021432101130211241134422413442312013042141212003102211300321404043012124332013240431242
import gmpy2
import binascii
#利用中国剩余定理求解同余方程,aList:余数,mList:模数
def CRT(aList, mList):
M = 1
for i in mList:
M = M * i #计算M = ∏ mi
#print(M)
x = 0
for i in range(len(mList)):
Mi = M // mList[i] #计算Mi
Mi_inverse = gmpy2.invert(Mi, mList[i]) #计算Mi的逆元
x += aList[i] * Mi * Mi_inverse #构造x各项
x = x % M
return x
if __name__ == "__main__":
guess = [3,10,17]
cList = [int(c1,5), int(c2,5), int(c3,5)]
nList = [int(n1,5), int(n2,5), int(n3,5)]
m_e = CRT(cList, nList) #计算m^e
for e in guess: #遍历e求解
m, f = gmpy2.iroot(m_e, e) #m_e开e次根
print("加密指数e = %d:"%e)
m = hex(m)[2:]
if len(m)%2 == 1:
m = m + '0' #binascii.unhexlify()参数长度必须为偶数,因此做一下处理
flag = binascii.unhexlify(m)
print(flag)
0x09 [HDCTF2019]bbbbbbrsa
enc如下
1
2
3
p = 177077389675257695042507998165006460849
n = 37421829509887796274897162249367329400988647145613325367337968063341372726061
c = ==gMzYDNzIjMxUTNyIzNzIjMyYTM4MDM0gTMwEjNzgTM2UTN4cjNwIjN2QzM5ADMwIDNyMTO4UzM2cTM5kDN2MTOyUTO5YDM0czM3Mj
from base64 import b64encode as b32encode
from gmpy2 import invert,gcd,iroot
from Crypto.Util.number import *
from binascii import a2b_hex,b2a_hex
import random
flag = "******************************"
nbit = 128
p = getPrime(nbit)
q = getPrime(nbit)
n = p*q
print p
print n
phi = (p-1)*(q-1)
e = random.randint(50000,70000)
while True:
if gcd(e,phi) == 1:
break;
else:
e -= 1;
c = pow(int(b2a_hex(flag),16),e,n)
print b32encode(str(c))[::-1]
# 2373740699529364991763589324200093466206785561836101840381622237225512234632
发现竟然直接把c给出了,下面就只需要在50000到70000之间爆破出e的值即可。代码如下
import gmpy2
from Crypto.Util.number import *
q = n//p
phi = (p-1)*(q-1)
for e in range(50000,70000):
if(gmpy2.gcd(e,phi)==1):
d = gmpy2.invert(e,phi)
m = pow(c,d,n)
if 'flag' in str(long_to_bytes(m)):
flag = long_to_bytes(m)
print (flag)
import gmpy2
from Crypto.Util.number import *
for e in range(100000):
res = pow(294,e,n1)
if (res == output):
break
p = gmpy2.gcd(n1,n2)
q1 = n1//p
phi1 = (p-1)*(q1-1)
d = gmpy2.invert(e,phi1)
m = pow(c1,d,n1)
flag = long_to_bytes(m)
print(flag)