20190422做题总结

20190422做题总结

1. RSA2

首先对pubkey.pem进行信息提取

1
openssl rsa -pubin -text -modulus -in pubkey.pem

得到n和eImgur

对n在http://factordb.com/进行在线分解,得p,q![Imgur](https://i.imgur.com/nog6NnU.png)

此时已知p,q,e即可得到d为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# coding = utf-8
def computeD(fn, e):
(x, y, r) = extendedGCD(fn, e)
#y maybe < 0, so convert it
if y < 0:
return fn + y
return y

def extendedGCD(a, b):
#a*xi + b*yi = ri
if b == 0:
return (1, 0, a)
#a*x1 + b*y1 = a
x1 = 1
y1 = 0
#a*x2 + b*y2 = b
x2 = 0
y2 = 1
while b != 0:
q = a / b
#ri = r(i-2) % r(i-1)
r = a % b
a = b
b = r
#xi = x(i-2) - q*x(i-1)
x = x1 - q*x2
x1 = x2
x2 = x
#yi = y(i-2) - q*y(i-1)
y = y1 - q*y2
y1 = y2
y2 = y
return(x1, y1, a)

p = 275127860351348928173285174381581152299
q = 319576316814478949870590164193048041239
e = 65537

n = p * q
fn = (p - 1) * (q - 1)

d = computeD(fn, e)
print d

运行结果如下

Imgur

此时d已知,即可对flag.encrypt这一密文先进行读取:

1
2
>>f = open('flag.encrypt')
>>p = f.read()

Imgur

即可看到密文内容,接着对密文内容进行16进制编码:

1
>>p.encode('hex')

就得到它16进制的形式:8fbb3b0f58b88b53e79d85c1ed68c355ac57c75884bd1e8e21c7ae9c6c82ddbb

在转换为字符串,即为c:

1
2
>>c=0x8fbb3b0f58b88b53e79d85c1ed68c355ac57c75884bd1e8e21c7ae9c6c82ddbb
>>print c

现在利用公式pow(c,d,n)就可以得到明文十进制形式

1
2
3
4
5
>>d = 10866948760844599168252082612378495977388271279679231539839049698621994994673

>>n = 87924348264132406875276140514499937145050893665602592992418171647042491658461

>>pow(c.d.n)

十进制形式无法进行编码,故要转换为16进制,利用hex:

1
2
>>pp = 10786438895796771334553405582542071004794259807619608563847580041597
>>hex(pp)

最后解密可得flag:

1
2
>>s='666c61677b5930755f6d34795f754e6433727374616e645f5253417d'
>>s.decode('hex')

Imgur


Wiener Tricky

首先同上对rsa_pub.pem进行信息提取,再对n进行分解,发现n太大无法分解。根据题目,可知要采取维纳攻击,在网上查找到相关的代码,https://github.com/pablocelayes/rsa-wiener-attack 后下载所有代码于桌面,运行程序可得解密密钥d:

Imgur

接下来步骤同上题,提取flag.enc文件内容

Imgur

进行16进制编码,再转为10进制c:

Imgur

现已知c,d,n,即可求明文pp:

Imgur

将明文变为16进制后在解密即得flag

Imgur
Imgur

但这时发现报错TypeError: Odd-length string

经过搜索得知错误原因为字符串长度为奇数, 一位16进制数转换为4位二进制数, 所以奇数个16进制数无法刚好转换为整数个字节。而高位的0经常被省略,所以在高位补零后得到flag:

Imgur