-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWienerAttack.py
113 lines (88 loc) · 3.78 KB
/
WienerAttack.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import random
import math
def check(d, e, N): #to check if the guessed private exponent is indeed a correct one with 10 random guessed numbers.
ans = True
for i in range(10):
x = random.randint(1 , N - 1)
if pow(pow(x, e, N), d, N) != pow(x, 1, N):
ans = False
break
return ans
def Wiener_Attack(e, N): #(public exponent, modulo) #works when assumed that private_exponent < 1/3 * sqrt(sqrt(N))
value = e / N
c_values = {0:math.floor(value)}
epsilon_values = {0:value - c_values[0]}
for j in range(1, 50):
c_values[j] = math.floor(1 / epsilon_values[j-1])
epsilon_values[j] = (1 / epsilon_values[j-1]) - c_values[j]
if epsilon_values[j] == 0:
break
ab_values = {0:[math.floor(value) , 1], 1: [c_values[0]*c_values[1] + 1, c_values[1]]}
for j in range(2, 30):
ab_values[j] = [(c_values[j] * ab_values[j-1][0]) + ab_values[j-2][0] , (c_values[j] * ab_values[j-1][1]) + ab_values[j-2][1]]
for j in range(1, 30):
if check(ab_values[j][1], e, N):
print("Broken. d = " , ab_values[j][1])
return
print("Not breakable by Weiner.")
print("""
Choose 1 for a pre-given input
Choose 2 for your own input
Anything else to exit
""")
n = input("Enter input: ")
if n in ["1" , "2"]:
if n == "1":
print("""
Choose 1 for
Public exponent = 6792605526025
Public mod = 9449868410449
Choose 2 for
Public exponent = 60728973
Public mod = 160523347
Choose 3 for
Public exponent = 5597398903514514944085308943326735497233822078816676736953861609324173763722590906046481333508341162032988975190944444906965270737899030239417191295375713,
Public mod = 6872175570244113261343553619685355269430741805534067877664766359877826450975952558312939101056163249928920301629052963413312251251547274155116249285958867
Anything else to exit
""")
n = input("Enter input: ")
if n == "1":
print("""
Using pre-given Input :
Public exponent = 6792605526025
Public mod = 9449868410449
""")
Wiener_Attack(6792605526025, 9449868410449)
if n == "2":
print("""
Using pre-given Input :
Public exponent = 60728973
Public mod = 160523347
""")
Wiener_Attack(60728973, 160523347)
if n == "3":
print("""
Using pre-given Input :
Public exponent =
5597398903514514944085308943326735497233822078816676736953861609324173763722590906046481333508341162032988975190944444906965270737899030239417191295375713
Public mod =
6872175570244113261343553619685355269430741805534067877664766359877826450975952558312939101056163249928920301629052963413312251251547274155116249285958867
""")
Wiener_Attack(5597398903514514944085308943326735497233822078816676736953861609324173763722590906046481333508341162032988975190944444906965270737899030239417191295375713, 6872175570244113261343553619685355269430741805534067877664766359877826450975952558312939101056163249928920301629052963413312251251547274155116249285958867)
else:
e = input("Enter your public exponent : ")
N = input("Enter your public mod : ")
try :
e = int(e)
N = int(N)
Wiener_Attack(e , N)
except:
print("Invalid input, please try again")
print("\n\n--------------A project by Dhruv Jaiswal-----------")
#
#
# ╱|、
# (˚ˎ。7
# |、˜ 〵
# じしˍ,)ノ
#