-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA_Encryption_Decryption_Code.py
52 lines (43 loc) · 1.22 KB
/
RSA_Encryption_Decryption_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
import random
import math
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
def multiplicative_inverse(e, phi):
def extended_gcd(a, b):
if b == 0:
return (a, 1, 0)
else:
gcd, x, y = extended_gcd(b, a % b)
return (gcd, y, x - (a // b) * y)
gcd, x, y = extended_gcd(e, phi)
if gcd != 1:
return None
else:
return x % phi
def generate_keypair(p, q):
n = p * q
phi = (p - 1) * (q - 1)
e = random.randrange(1, phi)
g = gcd(e, phi)
while g != 1:
e = random.randrange(1, phi)
g = gcd(e, phi)
d = multiplicative_inverse(e, phi)
return ((e, n), (d, n))
def encrypt(public_key, plaintext):
e, n = public_key
cipher = [(ord(char) ** e) % n for char in plaintext]
return cipher
def decrypt(private_key, ciphertext):
d, n = private_key
plain = [chr((char ** d) % n) for char in ciphertext]
return ''.join(plain)
public_key,private_key = generate_keypair(7,11)
message = input("Enter The Message: ")
print("Original Message is: ",message)
cipher = encrypt(public_key,message)
print("Encrypted Message: ",cipher)
plain = decrypt(private_key,cipher)
print("Decrypted Message: ",plain)