-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.py
111 lines (88 loc) · 2.82 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import random
def isPrime(num):
"""
This function checks if the given number is prime or not.
Parameters:
num (int): The number to be checked.
Returns:
bool: Returns True if the number is prime, False otherwise.
"""
isPrime = True
if num == 1:
isPrime = False
return isPrime
elif num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
isPrime = False
return isPrime
return isPrime
def mod_inv(a, m):
"""
This function calculates the modular inverse of a given number 'a' with respect to modulus 'm'.
Parameters:
a (int): The number for which the modular inverse is to be calculated.
m (int): The modulus.
Returns:
int: The modular inverse of 'a' with respect to 'm' if it exists, None otherwise.
"""
for x in range(1, m):
if (a * x) % m == 1:
return x
return None
def gcd(a, b):
"""
This function calculates the greatest common divisor (GCD) of two given numbers 'a' and 'b'.
Parameters:
a (int): The first number.
b (int): The second number.
Returns:
int: The GCD of the two numbers.
"""
while b != 0:
a, b = b, a % b
return a
def generate_keypair(p, q):
"""
This function generates a public and private key pair for the RSA cryptography algorithm.
Parameters:
p (int): A prime number.
q (int): Another prime number.
Returns:
tuple: A tuple containing two tuples, representing the public key (e, n) and private key (d, n).
"""
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 = mod_inv(e, phi)
return ((e, n), (d, n))
def encrypt(pk, plaintext):
"""
This function encrypts the plaintext using the public key.
Parameters:
pk (tuple): A tuple containing the public key 'e' and modulus 'n'.
plaintext (str): The plaintext message to be encrypted.
Returns:
list: A list of integers representing the encrypted message.
"""
key, n = pk
cipher = [(ord(char) ** key) % n for char in plaintext]
return cipher
def decrypt(pk, ciphertext):
"""
Decrypt the given ciphertext with the provided private key (pk).
Arguments:
pk (tuple): The private key, which consists of 2 elements,
the decryption exponent (d) and modulus (n).
ciphertext (list): The encrypted text as a list of integers.
Returns:
str: The decrypted plaintext as a string.
"""
key, n = pk
plain = [chr((char ** key) % n) for char in ciphertext]
return ''.join(plain)