-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hack.py
76 lines (62 loc) · 1.73 KB
/
Hack.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from Crypto.Util.number import *
import RSAwienerHacker
import gmpy2
import Arithmetic
def WienerHacker(e, n):
'''基于连分数的Wiener攻击'''
d = RSAwienerHacker.hack_RSA(e, n)
print(f"d:{d}")
def dpHacker(e, n, dp):
'''dp泄露'''
for x in range(1, e):
if(dp*e - 1) % x == 0:
if (n % ((dp * e - 1) // x + 1)) == 0:
p = (dp * e - 1) // x + 1
q = n//p
r = (p-1)*(q-1)
d = gmpy2.invert(e, r)
print(f"d:{d}")
def IntegerFactorizationHacker(n1, n2, e1, e2):
'''gcd去求解共同的素因子q'''
p = gmpy2.gcd(n1, n2)
q1 = n1 // p
q2 = n2 // p
r1 = (q1-1)*(p-1)
r2 = (q2-1)*(p-1)
d1 = gmpy2.invert(e1, r1)
d2 = gmpy2.invert(e2, r2)
print(f"d1:{d1}")
print(f"d2:{d2}")
def dpAnddqHacker(dp, dq, p, q, c):
'''dp和dq泄露'''
m1 = gmpy2.powmod(c, dp, p)
m2 = gmpy2.powmod(c, dq, q)
print(f"m1:{long_to_bytes(m1)}")
print(f"m2:{long_to_bytes(m2)}")
def LowEHacker(e, n, c):
'''低加密指数攻击,e过于小'''
k = 0
while 1:
res = gmpy2.iroot(c + k * n, e)
if (res[1] == True):
m = long_to_bytes(res[0])
print(f"m:{m}")
break
k = k + 1
def CommonModeHacker(e1 ,e2 ,c1 ,c2 , n):
'''共模攻击'''
s = Arithmetic.egcd(e1 ,e2)
s1 = s[0]
s2 = s[1]
if s1 < 0:
s1 = - s1
c1 = gmpy2.invert(c1, n)
elif s2 < 0:
s2 = - s2
c2 = gmpy2.invert(c2, n)
m = pow(c1, s1, n) * pow(c2, s2, n) % n
print(long_to_bytes(m))
def main():
pass
if __name__ == '__main__':
main()