记录几道RSA入门题
RSA
题目:在一次RSA密钥对生成中,假设p=473398607161,q=4511491,e=17
求解出d作为flga提交
基础题,直接解1
2
3
4
5
6import gmpy2
p=473398607161
q=4511491
e=17
d=gmpy2.invert(e,(p-1)*(q-1))
print d
rsarsa
题目给出了RSA的相关参数,直接求解1
2
3
4
5
6
7
8
9
10import gmpy2
p =
q =
e =
c =
phi = (p - 1) * (q - 1)
d = gmpy2.invert(e, phi)
n = p * q
print pow(c, d, n)
RSA1
- p,q,dp,dq,c
脚本如下:
1 | import gmpy2 |
RSA2
- e,n,c,dp
1 | import gmpy2 |
RSA3
- 共模攻击
- n,e1,e2,c1,c2
1 | import gmpy2 |
RSA
openssl的使用
1 | openssl rsa -pubin -text -modulus -in warmup -in pub.key |
RSAROLL
- 给出多个密文,逐一解密
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
44
45
46
47
48
49import gmpy2
n = 920139713
q = 18443
p = 49891
e = 19
d = int(gmpy2.invert(e , (p-1) * (q-1)))
c = '''704796792
752211152
274704164
18414022
368270835
483295235
263072905
459788476
483295235
459788476
663551792
475206804
459788476
428313374
475206804
459788476
425392137
704796792
458265677
341524652
483295235
534149509
425392137
428313374
425392137
341524652
458265677
263072905
483295235
828509797
341524652
425392137
475206804
428313374
483295235
475206804
459788476
306220148'''
falg = ''
for i in c.split('\n'):
falg += chr(pow(int(i),d,n))
print falg
Dangerous RSA
- 低加密指数攻击(e=3)
1 | import gmpy2 |
rsa2
- 低解密指数攻击(e非常大)
rsa-wiener-attack1
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
32import gmpy2
import hashlib
import ContinuedFractions, Arithmetic, RSAvulnerableKeyGenerator
def hack_RSA(e,n):
# Finds d knowing(e,n)
# applying the Wiener continued fraction attack
frac = ContinuedFractions.rational_to_contfrac(e, n)
convergents = ContinuedFractions.convergents_from_contfrac(frac)
for (k,d) in convergents:
# check if d is actually the key
if k!=0 and (e*d-1)%k == 0:
phi = (e*d-1)//k
s = n - phi + 1
# check if the equation x^2 - s*x + n = 0
# has integer
discr = s*s - 4*n
if(discr >= 0):
t = Arithmetic.is_perfect_square(discr)
if t!=-1 and (s+t)%2==0:
print("Hacked!")
return d
n =
e =
d = hack_RSA(e,n)
flag = "flag{" + hashlib.md5(hex(d)).hexdigest() + "}"
print flag
RSA5
题目给出了20对n,c
先将20个n两两取最大公因数,即可得到p(q),然后选取得到这个最大公因数的两个n中的其中一个,即可得到另一个素数
1 | list=[n,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17,n18,n19] |
我这选的是n4,q=n4/p
1 | p= |
SameMod
- 共模攻击
脚本跟RSA3相同,但解出的m需要用ASCII码解1
2
3
4
5
6
7
8
9
10
11
12string = str(m)
flag=''
i=0
j=1
while i < len(string):
if int(string[i:i+j]) >= 33 and int(string[i:i+j]) <=126:
flag+=chr(int(string[i:i+j]))
i=i+j
j=1
else:
j+=1
print(flag)
RSA4
- 中国剩余定理
使用中国剩余定理求解,但之前需做些转换,题目给出的n和c是5进制的
求出m之后转换出错,估计不是直接模的n,然后猜测e=3,开个根即可
1 | import gmpy2 |
RSA&what
- 共模攻击
- base64隐写
1 | import gmpy2 |
在将结果写进txt时,注意将base64合并一下,确保每行结尾为’=’
base64隐写脚本
1 | #https://www.jianshu.com/p/48fe4dd3e5ce |
坏蛋是雷宾
- Rabin算法
原理详见:https://wiki.x10sec.org/crypto/asymmetric/rsa/rsa_e_attack/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
29from gmpy2 import *
import hashlib
n=523798549
p=10663
q=49123
e=2
c=162853095
inv_p = invert(p, q)
inv_q = invert(q, p)
mp = pow(c, (p + 1) / 4, p)
mq = pow(c, (q + 1) / 4, q)
a = (inv_p * p * mq + inv_q * q * mp) % n
b = n - int(a)
c = (inv_p * p * mq - inv_q * q * mp) % n
d = n - int(c)
for i in (a, b, c, d):
print(bin(i)[2:])
#10010011100100100101010110001
m='10010011100100100101010'
mc=str(int(m,2))
md=hashlib.md5()
md.update(mc.encode("utf8"))
flag = md.hexdigest()
print("flag{"+str(flag)+'}')