-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.py
58 lines (47 loc) · 1.27 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
# Python Module for Encryption and Decryption by RSA Algorithm
import primes
from random import choice
def gcd(a, b):
while b != 0:
c = a % b
a = b
b = c
return a
def modinv(phi, m):
for x in range(1, m):
if (phi * x) % m == 1:
return x
return None
def coprimes(phi):
l = []
for x in range(2, phi):
if gcd(phi, x) == 1 and modinv(x, phi) != None:
l.append(x)
if len(l) > 5: break
for x in l:
if x == modinv(x, phi):
l.remove(x)
return l
def key_generator():
p, q = primes.choose_distinct_primes()
n = p * q
phi = (p-1) * (q-1) # Euler's function (totient)
e = choice(coprimes(phi))
d = modinv(e, phi)
public_key = [e, n]
private_key = [d, n]
return [public_key, private_key]
def encrypt_block(m, e, n):
c = (m**e) % n
return c
def decrypt_block(c, d, n):
m = (c**d) % n
return m
# method for encryption of a message
def encrypt_string(s, public_key):
e, n = public_key
return ''.join([chr(encrypt_block(ord(x), e, n)) for x in list(s)])
# method for decryption of a message
def decrypt_string(s, private_key):
d, n = private_key
return ''.join([chr(decrypt_block(ord(x), d, n)) for x in list(s)])