题目:含有flag.enc和pub.key两个文件
解题:用openssl 读取pub.key里的公钥,
openssl rsa -pubin -inform PEM -text < '/root/桌面/pub.key'
获得m=c0:33:2c:5c:64:ae:47:18:2f:6c:1c:87:6d:42:33:69:10:54:5a:58:f7:ee:fe:fc:0b:ca:af:5a:f3:41:cc:dd
转换为十进制:
a = 'c0:33:2c:5c:64:ae:47:18:2f:6c:1c:87:6d:42:33:69:10:54:5a:58:f7:ee:fe:fc:0b:ca:af:5a:f3:41:cc:dd'
b = a.replace(':','')
b
'c0332c5c64ae47182f6c1c876d42336910545a58f7eefefc0bcaaf5af341ccdd'
c = int(b,16)
c
86934482296048119190666062003494800588905656017203025617216654058378322103517
用factordb分解n,获得p、q
factordb 86934482296048119190666062003494800588905656017203025617216654058378322103517
安装factordb
pip install factordb-pycli
读取flag.enc文件的内容,转换为十六进制,后转为十进制;
import binascii
with open('/root/桌面/flag.enc','rb+') as f:
f = f.read()
a = binascii.hexlify(f)
b = int(a,16)
print(b)
再用脚本解密,但之前由于文字转换出现问题,以为自己数据出现问题,搜索相关WP,发现运用RSA库进行解密(不需要将c转换为数):
import rsa
import gmpy2
c = 1854183526100811878807183372982532818560316522978821358738967769534081571682
p = 285960468890451637935629440372639283459
q = 304008741604601924494328155975272418463
e = 65537
n = 86934482296048119190666062003494800588905656017203025617216654058378322103517
fn = (p-1)*(q-1)
d = int(gmpy2.invert(e,fn))
key = rsa.PrivateKey(n,e,d,q,p)
with open(r'c:\111\flag.enc','rb') as f: #文件路径
f = f.read()
print(rsa.decrypt(f,key))
————————————————
版权声明:本文为CSDN博主「gal2xy」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_52193383/article/details/119428246
key = rsa.PrivateKey(n,e,d,q,p)
#在pkcs标准中,pkcs#1规定,私钥包含(n,e,d,p,q),相当于pow()
print(rsa.decrypt(f,key))
# f:公钥加密结果 key:私钥,相当于libnum.n2s(m)
之后通过其他方法,添加一行
decoded_message = libnum.n2s(m).decode('utf-8', 'replace')
#将解密后的整数 m 转换为字符串,并通过 'replace' 参数指定在遇到无法解码的字节时使用问号替代。
最终代码:
import libnum
p = 285960468890451637935629440372639283459
q = 304008741604601924494328155975272418463
e = 65537
n = 86934482296048119190666062003494800588905656017203025617216654058378322103517
phi = (p-1)*(q-1)
d = libnum.invmod(e, phi)
c = 29666689760194689065394649908301285751747553295673979512822807815563732622178
m = pow(c, d, n)
decoded_message = libnum.n2s(m).decode('utf-8', 'replace')
print(decoded_message)
flag{decrypt_256}