-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA_Calculations_Code.py
72 lines (45 loc) · 1.25 KB
/
RSA_Calculations_Code.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
import random
import math
def generate_p_and_q():
p = int(input("Enter The Value Of P: "))
q = int(input("Enter The Value OF Q: "))
return p, q
p,q = generate_p_and_q()
n = p * q
P = p -1
Q = q -1
phi = (P*Q)
print("Value Of N is = ",n,"\nAnd Value Of Φ(N) = ",phi)
def generate_e(phi):
possible_e_values = []
for i in range(2,phi):
if math.gcd(i,phi) == 1:
e=i
possible_e_values.append(e)
# print("The All Possible Values Are : ",possible_e_values)
return random.choice(possible_e_values)
e = generate_e(phi)
print("Value Of E is = ",e)
def generate_d(e,phi):
# d_list = []
for i in range(2,phi):
if (i*e) % phi == 1:
d = i
# d_list.append(d)
break
# print(d_list)
return d
d = generate_d(e,phi)
print("Value of D is = ",d)
message = random.randint(1,n)
print("Random Generated Message is : ",message)
def encrypt(message,e,n):
c = pow(message,e,n)
return c
Encrypted_Message = encrypt(message,e,n)
print("Encrypted message is : ",Encrypted_Message)
def decrypt(message,d,n):
p = pow(message,d,n)
return p
Decrypted_message = decrypt(Encrypted_Message,d,n)
print("Decrypted Message is : ",Decrypted_message)