一个公钥文件和一个flag文件,进行公钥解析后发现n特别大而e是65537。所以大概就是一道小明文爆破。会运行10分钟作业好像才能有结果
import Crypto.PublicKey.RSA as RSA
from Crypto.Util.number import *
with open(r"C:\Users\mqy23\Downloads\attachment\safety_in_numbers\pubkey.pem",'r')as f:
key=RSA.importKey(f.read())
e=key.e
#print(e)
with open(r"C:\Users\mqy23\Downloads\attachment\safety_in_numbers\flag.enc",'rb')as s:
flag=s.read()
c=int.from_bytes(flag,byteorder='little')
import gmpy2
print(long_to_bytes(gmpy2.iroot(c,e)[0]))
import sys
import Crypto.PublicKey.RSA as RSA
def enc(msg, pubkey):
(n,e) = pubkey
m = int.from_bytes(msg, byteorder = 'little')
c = pow(m, e, n)
ctxt = (c).to_bytes(c.bit_length() // 8 + 1, byteorder = 'little')
return ctxt
with open("pubkey.pem", "r") as f:
ciph = RSA.importKey(f.read()) # chill out, Crypto.RSA takes its sweet time... (minutes)
pubkey = (ciph.n, ciph.e)
with open("flag.txt", "rb") as f:
flag = f.read()
sys.stdout.buffer.write(enc(flag, pubkey))
这里题目文件里写了m = int.from_bytes(msg, byteorder = 'little')
byteorder部分可以写little(逆序输出)或者big(正序)
最后得到flag的倒叙,转换一下即可。
c='}f00Rp_3RutUf!{ftcp'
print(c[::-1])
#pctf{!fUtuR3_pR00f}