Skip to content

当rsa遇到.enc或者.bin的密文

题目背景

题目很简单,n可以直接分解,但密文是flag.enc文件,不知道咋读取了

解题步骤

python
import gmpy2
from Crypto.Util.number import long_to_bytes
from Crypto.Util.number import bytes_to_long
from base64 import b64decode
import rsa
 
path = r'C:\Users\50180\Desktop\CTF练习\BUUCTF-RSA-1-0918\flag.enc'

p = 285960468890451637935629440372639283459
q = 304008741604601924494328155975272418463
e = 65537
n = q*p

d = gmpy2.invert(e, (p - 1) * (q - 1))

key = rsa.PrivateKey(n, e, int(d), p, q)

with open(path, "rb") as file:
    f = file.read()
    print(rsa.decrypt(f, key))

常见的获取n和e的方式

已知.pem文件

python
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa

# 从PEM格式文件中加载公钥
with open(r'C:\Users\50180\Desktop\CTF练习\wtc_rsa_bbq\key.pem', 'rb') as file:
    public_key = serialization.load_pem_public_key(file.read())

# 获取N和e值
n = public_key.public_numbers().n
e = public_key.public_numbers().e

# 打印N和e
print("N (Modulus):", n)
print("e (Exponent):", e)

已知.pub文件

python
from Crypto.PublicKey import RSA
 
path = r'C:\Users\50180\Desktop\CTF练习\wtc_rsa_bbq\key.pub'
with open(path) as f:
    key = RSA.import_key(f.read())
    print('e = %d' % key.e)
    print('n = %d' % key.n)

如果遇到密文是在.bin文件里的

python

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

# 打开二进制文件以供读取
with open(r'C:\Users\50180\Desktop\CTF练习\wtc_rsa_bbq\cipher.bin', 'rb') as file:
    # 读取文件内容
    binary_data = file.read()

c = bytes_to_long(binary_data)

n= 67957992944541709079637331495835288051017736172946219268979124310219676901814634141994056910249665182493729973853745613557654443204907939100289830019879722192443748372464385547701814777428863933615522890059773474417386931884425569247554583568455670977507333610910984675277207738543785144821532814109666026143830882670492412501367418900259525020329476259602896533289977520877332000639367622359213599508853567838502735818733930199768810238363613613753812700686852922490193705926713608040249887743216661319134457665231243871456616938275370046526015614319970725259025037328092187037543179610573655207460380491069632574369131778109901652462838149658009709174356861767870204803806924289315954871595222073859185353284268587331267173728221658781990119401252167899826047588583247362298572788397512447184590473718761927222549389515152849248566281272354480116085470203137413924993107429630882329043259530840382880179158766903387472716587067982240678129096467201138461960380680792953534366991098348930562736587887507018304132204828793333486018470656056870548645765390879310100168147762901154890481163444481122296433539288905754638768700952492512406731460604412447424967642115057341729310735982961515248311488333222612609961196477124084039771246638999119951171033639602489932159455170406551791127198312487436473494597495007637073871513066738388720194946877125132719068351961085662002556667562192631487439603084021927891681500086528284664252971146275389474911554158614058033716200975019522317823518017673425098559715637356338155079086810436188632405309750833509905138561418180066417475648209078560221064551872389391920606516486209161430247427717376721027499023891011742164463421915449354247944433039198608877315066424065203739793333831943021283319660097829517280179002430376294322395829290502762707146363340431306946572116838111246133390539315819023086956548363452657425340012155573512351731935196011505710069798425307656340650313899667019621505845424588367900827398564048322933092059258373872118350187669748998950161402077367239291462315563970921983573721268515566367155707585109243757143789901450148994239501684623655163637997105769222959494325064113278738038394139099703042640054592379346782002980751786063951491138135071807202634411442713870047730189971333170418112077390015852729766594918955255488280027359348811304774349052504612270369695008061623968574970758347117606577939555446238067430685787886502742201372798852010893885999941946548336103402517187248492765699490151394806591149028320530966365733320484047650429644420042692902092613911608152310152409036167526533619822851411589502970363903
p=2**4244*699549860111847-1
q=2**4244*699549860111847+1
e =65537

# phi(n)=(p-1)*(q-1)=pq*pq//n
phin = (p-1)*(q-1)
d = gmpy2.invert(e, phin)
# print("d=",d)
m = pow(c, d, n)
# print("m=",m)
print(long_to_bytes(m).decode())