-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSA.py
More file actions
44 lines (34 loc) · 851 Bytes
/
RSA.py
File metadata and controls
44 lines (34 loc) · 851 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# RSA cryptographic security method
# TODO:
# decryption method
# class
# protected attributes
import secrets
from oaep import Oaep
from functions import *
from primitives import *
def generate_prime(bits):
while True:
rand = secrets.randbits(bits)
if is_prime(rand):
return rand
def generate_keys(bits):
p = generate_prime(bits // 2)
q = generate_prime(bits // 2)
n = p * q
totient = lcm(p - 1, q - 1)
e = 65537
d = mod_inverse(e, totient)
public_key = (n, e)
private_key = (n, d)
return public_key, private_key
def encode(message, key):
o = Oaep(hashlib.sha3_512(), key)
em = o.encrypt(message)
m = os2ip(em)
c = pow(m, key[1], key[0])
c = i2osp(c, o.k)
return c
def decode(cipher, key):
c = os2ip(cipher)
m = pow(c, key[1], key[0])