-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrial Division.py
50 lines (44 loc) · 963 Bytes
/
Trial Division.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
N = 18
e = 9007 #public key
cipher = 16765951
import math
j = 0
i = 2
A = []
while i < math.sqrt(N):
if N%i == 0:
p = int(i)
A.append(p)
i = i+1
j = j+1
q = int(N/p)
print(p)
print(q)
def eea(a, b): #Extended Euclidian Algorithm
s, old_s = 0, 1
t, old_t = 1, 0
r, old_r = b, a
while r != 0:
quotient = old_r // r
old_r, r = r, old_r - quotient * r
old_s, s = s, old_s - quotient * s
old_t, t = t, old_t - quotient * t
return old_r, old_s, old_t
def inv(n, p):
gcd, x, y = eea(n, p)
assert (n * x + p * y) % p == gcd
if gcd != 1:
# Either n is 0, or p is not a prime number.
raise ValueError(
'{} has no multiplicative inverse '
'modulo {}'.format(n, p))
else:
return x % p
d = inv(e, (p-1)*(q-1))
print(d)
m=pow(cipher,d,N)
print(m)
c=pow(m,e,N)
print(c)
print(j)
print(A)