-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_modulus_attack.py
172 lines (152 loc) · 2.78 KB
/
common_modulus_attack.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# -*- coding: utf-8 -*-
import random
def modular_exp(a, b, n):
res = 1
while b > 0:
if b & 1 == 1:
res = (res * a) % n
a = (a * a) % n
b >>= 1
return res
def gen_rand(bit_length):
bits = [random.randint(0,1) for _ in range(bit_length - 2)]
ret = 1
for b in bits:
ret = ret * 2 + int(b)
return ret * 2 + 1
def mr_primary_test(n, k=100):
if n == 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
d = n - 1
s = 0
while d % 2 != 0:
d /= 2
s += 1
r = [random.randint(1, n - 1) for _ in range(k)]
for a in r:
if modular_exp(a, d, n) != 1:
pl = [(2 ** rr) * d for rr in range(s)]
flg = True
for p in pl:
if modular_exp(a, p, n) == 1:
flg = False
break
if flg:
return False
return True
def gen_prime(bit):
while True:
ret = gen_rand(bit)
if mr_primary_test(ret):
break
return ret
def gcd(x, y):
if x < y:
x, y = y, x
if y == 0:
return x
return gcd(y, x % y)
def exgcd(x, y):
c0, c1 = x, y
a0, a1 = 1, 0
b0, b1 = 0, 1
while c1 != 0:
m = c0 % c1
q = c0 // c1
c0, c1 = c1, m
a0, a1 = a1, (a0 - q * a1)
b0, b1 = b1, (b0 - q * b1)
return c0, a0, b0
def gen_d(e, l):
_, x, _ = exgcd(e, l)
return x % l
# ロー法
def rho_algo(n, x, y, i):
d = 1
while d == 1:
x = (x * x + 1) % n
y = (y * y + 1) % n
y = (y * y + 1) % n
d = gcd(abs(x - y), n)
return d
def sqrt(n):
x = n
y = (x + 1) // 2
while y < x:
x = y
y = (x + n // x) // 2
return x
# フェルマー法 √nから探索
def fermat(n):
x = sqrt(n) + 1
y = sqrt(x * x - n)
while True:
w = x * x - n - y * y
if w == 0:
break
elif w > 0:
y += 1
else:
x += 1
return (x + y, x - y)
# 逆元
def invert(x, n):
(a, b, c) = exgcd(x, n)
if a != 1:
print("nashi")
return 0
return b
def simple(n, e, c):
p = rho_algo(n, 2, 2, 0)
q = n / p
print(p, q)
d = e
phi = (p - 1) * (q - 1)
while d % phi != 1:
d += e
return (p, q, d // e)
# c1 = m^e1 mod n
# c2 = m^e2 mod n
# e1s1 + e2s2 = 1
# c1^s1 c2^s2 = m^(e1s1 + e2s2) mod n = m
def CommonModulusAttack(e1, e2, c1, c2, n):
a, s1, s2 = exgcd(e1, e2)
if s1 < 0:
s1 = -s1
c1 = invert(c1, n)
if s2 < 0:
s2 = -s2
c2 = invert(c2, n)
v = modular_exp(c1, s1, n)
w = modular_exp(c2, s2, n)
m = (v * w) % n
return m
if __name__ == '__main__':
bits = 256
p = gen_prime(bits)
q = gen_prime(bits)
e1 = 65537
e2 = gen_prime(16)
d1 = gen_d(e1, (p-1)*(q-1))
d2 = gen_d(e2, (p-1)*(q-1))
n = p * q
print("p:", p)
print("q:", q)
print("e:", e1, e2)
print("d:", d1, d2)
print("n:", n)
print()
m = 123456789
c1 = modular_exp(m, e1, n)
c2 = modular_exp(m, e2, n)
print("clear text :", m)
print("secret text:", c1, c2)
print()
# e1, e2, c1, c2, n -> m
print("その8 Common Modulus Attack")
print("clear text:", CommonModulusAttack(e1, e2, c1, c2, n))
print()