-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa.py
More file actions
153 lines (121 loc) · 4.57 KB
/
rsa.py
File metadata and controls
153 lines (121 loc) · 4.57 KB
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
import time
import sys
#Checking whether the user input is prime number or composite number.
def check_prime(number):
counter = 0
for divider in range(1,number+1):
remainder = number % divider
if remainder == 0:
counter += 1
return counter
#Selecting First prime Number(p)
def first_prime():
print('********** FIRST PRIME NUMBER SELECTION *********\n')
while True:
try:
p = int(input("\nSelect first large random prime number: "))
no_of_divider = check_prime(p)
if no_of_divider == 2:
print(f"Congratulation, {p} is a prime number.\n")
return p
break
else:
print(f"Sorry, {p} is composite Number.\nTry Again...\n")
except:
print("Invalid Input. Try Again !!!")
#Selecting Second Prime Number(q)
def second_prime():
print('************* SECOND PRIME NUMBER SELECTION ****************\n')
while True:
try:
q = int(input("Select second large random prime number: "))
no_of_divider = check_prime(q)
if no_of_divider == 2:
print(f'Congratulation Again, {q} is a prime Number.\n')
time.sleep(1)
return q
break
else:
print(f'Sorry, {q} is a composite Number.\nTry Again...\n')
except:
Print("Invalid Input. Try Again !!!")
#Selecting public key(e) For Data Encryption
def select_public_key():
print("********* PUBLIC KEY SELECTION ************")
print("Conditions : ")
print(f' [1] : 1 < e < phi_n i.e. 1 < e < {phi_n}')
print(f' [2] : gcd(e , phi_n) = 1.\n') #gcd = greatest common divisor
while True:
try:
e = int(input('Select your public key in the given range:'))
if e > 1 and e < phi_n:
no_of_divider = check_prime(e)
if no_of_divider == 2:
print(f'public key : {e}\n')
return e
break
else:
print(f'gcd({e} , {phi_n}) != 1.\nTry Again...')
continue
else:
print(f'Please, Select Pubic Key in Range 1 < e < {phi_n}')
continue
except:
print('Please, Use Integer as Public key.')
continue
#Calculating Private Key For Data Encryption.
def private_key_selection():
for k in range(0,100):
d = (1 + k * phi_n) / e
if d.is_integer():
time.sleep(1)
print('Please Wait !!! Generating private key.')
time.sleep(1)
print('\n')
print(f'Private Key : {int(d)}\n')
return int(d)
break
#Encrypting / Decrypting Data According to user Choice
def encrypt_decrypt():
while True:
try:
time.sleep(1)
print('********** WELCOME TO ENCRYPTION / DECRYPTION PHASE ************\n')
time.sleep(0.6)
print('Press 1 for Encryption.\nPress 2 for Decryption.\nPress 3 to exit program\n')
time.sleep(0.6)
user_choice = input('Enter Your Choice : ')
print(f"User Choice : {user_choice}\n")
if user_choice == '1':
plain_text = int(input('Enter Plain text For Encryption: '))
time.sleep(1)
print('Please Wait !!! Generating Cipler Text...')
time.sleep(1)
cipher_text = (plain_text ** e) % n
print (f"Cipher Text : {cipher_text}\n")
if user_choice == '2':
cipher_text = int(input("Enter Cipher Text for Decryption : "))
plain_text = (cipher_text ** d) % n
print(f"Plain Text : {plain_text}\n")
if user_choice == '3':
print('Please Wait...')
time.sleep(1.4)
print('Program Ended Successfully.')
break
except:
print('Invalid Input..\n')
continue
#Main Method for executing all the created functions.
def final():
print('************ KEEP YOUR SYSTEM SECURE. ENCRYPT YOUR DATA WITH RSA *************\n')
global p, q, n, phi_n, e, d
p = first_prime()
q = second_prime()
n = p * q
print(f"The product of two prime numbers is [n : {n}]\n")
phi_n = (p-1) * (q-1)
print(f"The value of phi_n is [phi_n : {phi_n}]\n")
e = select_public_key()
d = private_key_selection()
encrypt_decrypt()
final()