-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.py
53 lines (32 loc) · 966 Bytes
/
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
import encode as en
import random
from Crypto.Random import get_random_bytes
from Crypto.Util import number
import math
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
def generate_keys(key_size):
# Choose two random prime numbers
p = number.getPrime(key_size)
q = number.getPrime(key_size)
while p == q:
q = number.getPrime(key_size)
# Calculate n and phi(n)
n = p * q
phi_n = (p - 1) * (q - 1)
# Choose a random integer e such that 1 < e < phi_n and gcd(e, phi_n) = 1
e = random.randint(2, phi_n - 1)
while gcd(e, phi_n) != 1:
e = random.randint(2, phi_n - 1)
# Calculate d such that d*e ≡ 1 (mod phi_n)
d = pow(e, -1, phi_n)
return (n, e), (n, d)
def encrypt(message, public_key):
n, e = public_key
return pow(message, e, n)
def decrypt(ciphertext, private_key):
n, d = private_key
return pow(ciphertext, d, n)