-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA.py
87 lines (69 loc) · 1.9 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
# Rivest, Shamir, Adleman(RSA) Algorithm
import random
import math
def is_prime(number):
if number <= 1:
return False
elif number <= 3:
return True
elif number % 2 == 0 or number % 3 == 0:
return False
i = 5
while i * i <= number:
if number % i == 0 or number % (i + 2) == 0:
return False
i += 6
return True
def generate_prime(intial_range,final_range):
prime = random.randint(intial_range,final_range)
while not is_prime(prime):
prime = random.randint(intial_range,final_range)
return prime
def mod_inverse(e,phi):
for d in range(3,phi):
if(d*e)%phi==1:
return d
# now taking two random large prime numbers
x,y = generate_prime(1000,5000),generate_prime(1000,5000)
# here in case we get two similar prime for that we checking here
while x==y:
y = generate_prime(1000,5000)
n = x*y
# euler totient function
phi_n = (x-1)*(y-1)
# to find the co-prime
e = random.randint(3,phi_n-1)
while math.gcd(e,phi_n)!=1:
e = random.randint(3,phi_n-1)
d= mod_inverse(e,phi_n)
print()
print(" two prime numbers x,y respectively are : ",x,y)
print()
print("n = x*y :",n)
print()
print("Phi of n",phi_n)
print()
print("public key",e)
print()
print("private key",d)
print()
message = input("Enter the message :")
print()
message_encode = []
for ch in message:
message_encode.append(ord(ch))
ciphertext = []
for ch in message_encode:
ciphertext.append(pow(ch, e, n))
print("Cypher-Text :",ciphertext)
print()
decrypted_message = []
for ch in ciphertext:
decrypted_message.append(pow(ch, d, n))
print("decrypted_message :",decrypted_message)
print()
decrypted_string = ""
for ch in decrypted_message:
decrypted_string += chr(ch)
print("Decrypted message : ",decrypted_string)
print()