i春秋 Crypto模块rsa wrtiteup

一、rsa256(100)

下载得到:

打开public.key:

1、使用openssl,分解publickey得到:

e=65537,n=D99E952296A6D960DFC2504ABA545B9442D60A7B9E930AFF451C78EC55D555EB

2、使用网站http://factordb.com/分解n,得到p,q

3、得到足够的数据,利用python解密得到flag:

import gmpy2
import rsa
p = 302825536744096741518546212761194311477
q = 325045504186436346209877301320131277983
n = 98432079271513130981267919056149161631892822707167177858831841699521774310891
e = 65537
d = int(gmpy2.invert(e , (p-1) * (q-1)))
privatekey = rsa.PrivateKey(n , e , d , p , q)      #根据已知参数,计算私钥
with open("encrypted.message1" , "rb") as f:
    print(rsa.decrypt(f.read(), privatekey).decode())       #使用私钥对密文进行解密,并打印
with open("encrypted.message2" , "rb") as f:
    print(rsa.decrypt(f.read(), privatekey).decode())       #使用私钥对密文进行解密,并打印
with open("encrypted.message3" , "rb") as f:
    print(rsa.decrypt(f.read(), privatekey).decode())       #使用私钥对密文进行解密,并打印

二、medium RSA(200)

与上题类似,首先openssl分解pubkey.pem(利用python的rsa模块自动生成的公私钥文件一般保存为pem文件),得到e,n,然后分解n得到p,q,再利用python解密

上题是直接利用python的rsa模块,该题应该直接利用rsa的解密方法,原因:上题数据较小,这题利用到了gmpy2

三、hard RSA(300)

分解pubkey.pem得到e和N,e=2

刚开始一直用常规rsa解题方法,求d的时候一直报错:ZeroDivisionError: invert() no inverse exists,无法求逆(确实无法求,但不知道为什么)

然后百度得到e=2是一种特殊情况,应特殊对待:

import gmpy2
import rsa
import string
from Crypto.PublicKey import RSA

public_key = RSA.importKey(open("pubkey.pem").read())
N = public_key.n
e = public_key.e

p=275127860351348928173285174381581152299

q=319576316814478949870590164193048041239

with open('flag.enc', 'r') as f:
    cipher = f.read().encode('hex')
    cipher = string.atoi(cipher, base=16)
    print cipher

# 计算yp和yq
yp = gmpy2.invert(p,q)
yq = gmpy2.invert(q,p)

# 计算mp和mq
mp = pow(cipher, (p + 1) / 4, p)
mq = pow(cipher, (q + 1) / 4, q)

# 计算a,b,c,d
a = (yp * p * mq + yq * q * mp) % N
b = N - int(a)
c = (yp * p * mq - yq * q * mp) % N
d = N - int(c)

for i in (a,b,c,d):
    s = '%x' % i
    if len(s) % 2 != 0:
        s = '0' + s
    print s

很迷啊。。

猜你喜欢

转载自www.cnblogs.com/dx-yll/p/11396278.html
今日推荐