-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
30 lines (23 loc) · 669 Bytes
/
app.py
File metadata and controls
30 lines (23 loc) · 669 Bytes
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
from math import gcd
p = int(input("Enter 1st Prime Number: "))
q = int(input("Enter 2nd Prime Number: "))
# Computation of modulus and Euler's Totient
n = p * q
phi = (p - 1) * (q - 1)
# Selecting public exponent [e]
e = 2
while e < phi:
if gcd(e, phi) == 1:
break
e += 1
# Computation of private exponent [d] (Modular Inverse of [e])
d = pow(e, -1, phi)
print("\nPublic Key (e, n):", (e, n))
print("Private Key (d, n):", (d, n))
# Encrypting message
msg = int(input("\nEnter message (number < n): "))
cipher = pow(msg, e, n)
print("Encrypted Message:", cipher)
# Decrypting message
plain = pow(cipher, d, n)
print("Decrypted Message:", plain)