-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilis.py
263 lines (224 loc) · 6.81 KB
/
utilis.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
from sympy import randprime
import random
from math import gcd
import os
import json
def generate_prime(bits: int):
"""
Generate a prime number with the specified number of bits.
Parameters:
----------------
bits: int
The number of bits the prime number should have.
Returns:
----------------
prime : int
A prime number with the specified number of bits.
"""
min_value = 2 ** (bits - 1)
max_value = (2 ** bits) - 1
prime = randprime(min_value, max_value)
return prime
def find_e(phi_n):
"""
Find a number e such that 1 < e < phi_n and gcd(e, phi_n) = 1.
Parameters:
----------------
phi_n : int
The value of Euler's totient function for n.
Returns:
----------------
e : int
A number that satisfies the conditions.
"""
while True:
e = random.randint(2, phi_n-1)
if gcd(e, phi_n) == 1:
return e
def find_d(e, phi_n):
"""
Find the modular inverse d of e modulo phi_n.
Parameters:
----------------
e : int
The public exponent.
phi_n : int
The value of Euler's totient function for n.
Returns:
----------------
d : int
The modular inverse of e modulo phi_n.
"""
# Using the extended Euclidean algorithm to find the modular inverse
def extendedEuclid(a, b):
if b == 0:
return a, 1, 0
else:
d2, x2, y2 = extendedEuclid(b, a % b)
d, x, y = d2, y2, x2 - (a // b) * y2
return d, x, y
# The modular inverse of e is the d that satisfies the equation e * d ≡ 1 (mod phi(n))
d = extendedEuclid(e, phi_n)[1] % phi_n
return d
def generate_keys(bits: int):
"""
Generate public and private keys using the RSA algorithm.
Parameters:
----------------
bits : int
The number of bits the key should have.
Returns:
----------------
public_key : tuple
The public key.
private_key : tuple
The private key.
"""
p = generate_prime(bits//2)
# make sure they are not the same
while True:
q = generate_prime(bits//2)
if p != q:
break
# calculate phi_n : totient function
phi_n = (p - 1) * (q - 1)
# determine e and d
public_exponent = find_e(phi_n)
private_exponent = find_d(public_exponent, phi_n)
modulus = p * q
public_key = (public_exponent, modulus)
private_key = (private_exponent, modulus)
return public_key, private_key
def encrypt(message, public_key):
"""
Encrypt a message using the RSA algorithm.
Parameters:
----------------
message : str
The message to encrypt.
public_key : tuple
The public key to use for encryption.
Returns:
----------------
encrypted_message : list
The encrypted message.
"""
# unpack the public key
e, n = public_key
# convert the message to a list of integers
message = [ord(char) for char in message]
# encrypt the message
encrypted_message = [pow(char, e, n) for char in message]
return encrypted_message
def decrypt(encrypted_message, private_key):
"""
Decrypt a message using the RSA algorithm.
Parameters:
----------------
encrypted_message : list
The encrypted message.
private_key : tuple
The private key to use for decryption.
Returns:
----------------
message : str
The decrypted message.
"""
# unpack the public key
d, n = private_key
# decrypt the message
decrypted_message = [pow(char, d, n) for char in encrypted_message]
# convert the decrypted message to a string
message = ''.join([chr(char) for char in decrypted_message])
return message
def shor_algorithm(public_key: list):
"""
Break RSA algorithm by finding the private key for a given public key.
Parameters:
----------------
public_key : list
The public key to break.
Returns:
----------------
private_key : list
The private key for the given public key.
"""
n = public_key[1]
e = public_key[0]
p = 0
q = 0
# choose a random number g
g = random.randint(2, n-1)
if g % n == 0: # if g is a multiple of n, then we have found a factor
print("*Found a factor randomly: ", g)
p = g
q = n // g
else:
# find the period r of g^x mod n
r = 1
print(f"*Trying to find a period r for the given g : {g}...")
while True:
if r > n:
print("*Could not find a period. Trying again...")
return shor_algorithm(public_key)
if pow(g, r, n) == 1:
break
r += 1
if r % 2 != 0:
print(f"*Found an odd period r : {r} for the given g : {g}. Trying again...")
return shor_algorithm(public_key)
else:
print(f"*Found a suitable r : {r} for the given g : {g}")
candidate = pow(g, r // 2) - 1
p = gcd(candidate, n)
if p != 1 and p != n:
q = n // p
else:
print("*Period gave trivial factor, trying again...")
return shor_algorithm(public_key)
phi_n = (p - 1) * (q - 1)
d = find_d(e, phi_n)
private_key = [d, n]
return private_key
def IO(file, input=None, output=None):
"""
Read or write data to a JSON file.
Parameters:
----------------
file : str
The file to read or write to.
input : str
The data to write to the file.
output : str
The data to extract from the file.
Returns:
----------------
data : str
The data read from the file.
"""
if input != None:
# Create a new file if not exists or delete content if genrate_keys is called
if not os.path.exists(file) or "public_key" in input.keys():
with open(file, "w") as f:
f.write("{}")
print(f"Initialize {file} with empty object...")
# Read the file and update the data
with open(file, "r") as f:
data = json.load(f)
for key in input.keys():
data[key] = input[key]
with open(file, "w") as f:
json.dump(data, f, indent=4)
return
elif output != None:
assert os.path.exists(file), f"File {file} does not exist."
with open(file, "r") as file:
data = json.load(file)
value = data.get(output)
if output == "public_key" or output == "private_key":
assert value != None, f"Please generate keys first using -a generate_keys."
else:
assert value != None, f"Please encrypt a message first using -a encrypt."
return value
else:
assert "You must specify either input or output argument."