-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRSA.py
188 lines (172 loc) · 4.17 KB
/
RSA.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import random
import math
import ctypes as c
import os
'''
getPrime() returns probably prime 64-bit length number
'''
def getPrime():
random.seed()
prime = 0
while not primeTest(prime, 100):
odd = 0
while odd % 2 == 0:
prime = random.getrandbits(64)
odd = int(str(prime)[-1])
return prime
'''
primeTest() returns True if prime, otherwise - False
uses Miller-Rabin primality test
k - times repeat
'''
def primeTest(prime, k):
# initial conditions
if prime == 0:
return False
elif prime == 1:
return True
else:
# get prime - 1 as (2^s)*t (t is odd)
t = prime - 1
s = 0
while t % 2 == 0:
t = int(t / 2)
s += 1
for i in range(0, k):
a = random.randint(2, prime - 2)
x = pow(a, t, prime)
if x == 1 or x == prime - 1:
continue
else:
for j in range(0, s - 1):
x = pow(x, 2, prime)
if x == 1:
return False
elif x == prime - 1:
break
return False
return True
'''
gcd(a,b) returns the greatest common divisor (a,b) and a list of quotients
uses Euqlid algorithm
'''
def gcd(a, b):
q_list = []
q = 0
while b != 0:
new_a = b
q = math.floor(a / b)
q_list.append(q)
b = a % b
a = new_a
return a, q_list
'''
inverse(a,m) returns inverse element to a modulo m
'''
def inverse(a, m):
gd, q_list = gcd(a, m)
if gd == 1:
p_list = [1, q_list[1]]
i = 2
for item in q_list[2:]:
p_list.append(item * p_list[i - 1] + p_list[i - 2])
i += 1
return (((-1) ** (len(q_list[1:]) - 1)) * p_list[-2]) % m
'''
modPow(a,b,m) returns a^b mod m
uses Montgomery modular multiplication
'''
def modPow(a, b, m):
b_iter = 1
while b != 0:
if b % 2 == 0:
b = math.floor(b / 2)
a = (a * a) % m
else:
b -= 1
b_iter = (b_iter * a) % m
return b_iter
'''
rsaKeys generates private & public keys tuples
'''
def rsaKeys():
p = getPrime()
q = getPrime()
n = p * q
phi = (p - 1) * (q - 1)
e = getPrime()
d = inverse(e, phi)
private_key = (d, n)
public_key = (e, n)
return private_key, public_key
'''
rsaEncrypt(pub_e, n, str) encrypts string text with public key (pub_e,n)
'''
def rsaEncrypt(pub_e, n, text):
txt_l = list(text)
i = 0
for item in txt_l:
txt_l[i] = bin(ord(item))[2:]
# padding for space
if len(txt_l[i]) == 6:
txt_l[i] = '0' + txt_l[i]
i += 1
txt_b = "".join(txt_l)
words = []
start = 0
end = 63
while end <= len(txt_b):
words.append(txt_b[start:end])
start = end
end += 63
words.append(txt_b[start:])
i = 0
for item in words:
words[i] = pow(int(item, 2), pub_e, n)
i += 1
return words
'''
rsaDecrypt() decrypts list enc_txt with private key (priv_d,n)
'''
def rsaDecrypt(priv_d, n, enc_txt):
i = 0
for item in enc_txt:
enc_txt[i] = pow(item, priv_d, n)
i += 1
i = 0
for item in enc_txt:
enc_txt[i] = bin(item)[2:]
i += 1
bin_str = ""
for item in enc_txt:
bin_str += item
start = 0
end = 7
char_l = []
while end <= len(bin_str):
char_l.append(bin_str[start:end])
start = end
end += 7
i = 0
for item in char_l:
char_l[i] = int(item, 2)
i += 1
i = 0
for item in char_l:
char_l[i] = chr(int(item))
i += 1
return "".join(char_l)
# print("generating keys pair")
# private, public = rsaKeys()
# print("done!")
# print("private key is: ", private)
# print("public key is: ", public)
# text = ""
# while(True):
# text = input("enter string to encrypt ")
# if text == 'q':
# break
# encrypted = rsaEncrypt(public[0], public[1], text)
# print("encrypted message: ", encrypted)
# decrypted = rsaDecrypt(private[0], private[1], encrypted)
# print("decrypted message: ", decrypted)