-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA encryption.py
119 lines (81 loc) · 2.17 KB
/
RSA encryption.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
import math
import os
def are_coprime(a, n):
return math.gcd(a, n) == 1
def miller_rabin_test(a, n):
s = 0
while (n - 1) % (2 ** (s + 1)) == 0:
s += 1
d = (n - 1) // (2 ** s)
if mod_exponentiation(a, d, n) == 1:
return True
for r in range(s):
if mod_exponentiation(a, (2 ** r) * d, n) == n - 1:
return True
return False
def random(length):
random_data = os.urandom(length)
number = ""
for num in list(random_data):
number += str(num)[-1]
return int(number)
def generate_prime(length, iterations=4, length_a=10):
prime = random(length)
i = 0
while i <= iterations:
a = random(length_a)
while not are_coprime(a, prime):
a = random(length_a)
if not miller_rabin_test(a, prime):
prime = random(length)
i = 0
else:
i += 1
return prime
def mod_exponentiation(a, b, n):
b_bin = bin(b)[2:]
result = 1
a = (a ** 1) % n
if b_bin[-1] == "1":
result *= a
result %= n
for i in range(1, len(b_bin)):
a = (a ** 2) % n
if b_bin[-i - 1] == "1":
result *= a
result %= n
return result
def extended_euclidean(a, b):
A = a
s2, t2, s1, t1 = 1, 0, 0, 1
while b > 0:
q = a // b
r, s, t = (a - b * q), (s2 - q * s1), (t2 - q * t1)
a, b, s2, t2, s1, t1 = b, r, s1, t1, s, t
t = t2
if t >= 0:
return t
else:
return A + t
def encode(m, e, n):
if m < 0 or m >= n:
return None
c = mod_exponentiation(m, e, n)
return c
def decode(c, d, n):
if c < 0 or c >= n:
return None
m = mod_exponentiation(c, d, n)
return m
p = generate_prime(100)
q = generate_prime(100)
n = p * q
phi = (p - 1) * (q - 1)
e = random(50)
while not are_coprime(e, phi):
e = random(50)
d = extended_euclidean(phi, e)
message = 651651313165432132163514631321365161316463210321651303206513130356
c = encode(message, e, n)
m = decode(c, d, n)
print(m)