-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA final .py
122 lines (95 loc) · 3.01 KB
/
RSA final .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
import math
import random
relative_primes=[]
def generate_Primeno():
def is_prime(n):
if n == 1:
return False
p = 2
bound = math.floor(math.sqrt(n))
while p<=bound:
if n%p == 0:
return False
p+=1
return True
primes = []
for i in range(1, 100):
if is_prime(i):
primes.append(i)
return random.choice(primes)
def eculids_GCD(phi,e):
if e==0:
return phi
else:
return eculids_GCD(e,phi%e)
def multiplicative_inverse(e,phi):
private_key=1
for i in range(phi):
if((e*i)%phi == 1):
private_key=i
return private_key
def encrypt(message,public_key):
asci_message=[]
for char in message:
var=ord(char)
value=(var**public_key[0])%public_key[1]
asci_message.append(value)
return asci_message
def decrypt(encrypted_message,private_key):
#decrypt using the private key (SECRET KEY)
#for i in encrypted_message:
# message=i
message=(encrypted_message).split(',')
decrypted_message=''
for num in message:
if (num=='32'):
decrypted_message+=' '
else:
var=(int(num)**private_key[0])%private_key[1]
char=chr(var)
decrypted_message+=char
return decrypted_message
#generate key 1 which is P
prime_no=generate_Primeno()
print("prime_no 1(p) ", prime_no)
#generate key 2 which is Q
prime_no2=generate_Primeno()
print("Prime no 2 (q)",prime_no2)
# finding N the product of the primes
product=prime_no*prime_no2
print("The product of the two primes(N) ",product)
#finding the qootient phi(Ø)
phi=(prime_no-1)*(prime_no2-1)
print("Here is Phi(N)-quotient ",phi)
#find e or public key such that it's relatively prime with Ø or phi
for e in range(1,phi):
if eculids_GCD(phi,e)==1:
relative_primes.append(e)
public_key=(random.choice(relative_primes), product)
print("Here is the public key(e) ", public_key)
#find d(secret key) such that it's de = 1 (mod (p-1)(q-1))
private_key=(multiplicative_inverse(public_key[0],phi), product)
print("here is the private key(d)", private_key)
#interacting with the user
while True:
command=input("Do you want to decrypy or encrypt , enter E for encryption and enter D for decryption: ")
if command.lower()=='e':
message=input("enter message to encyrpt: ")
print("Encypting your message...")
encrypted_message=encrypt(message,public_key)
print("here is your encrypted message: ", encrypted_message)
else:
i=0
while i<3 and command.lower()=='d':
secret_key=eval(input("please enter secret key: "))
if secret_key==private_key:
to_decrypt=input("enter message separated by commas: ")
print("Decrypting your message... ")
decrypted_message=decrypt(to_decrypt,private_key)
print("Decrypted message ", decrypted_message)
break
elif i==4:
print("you have run out of tries...Good Bye")
else:
print("wrong secret key!!!")
#PLEASE WORK!!!