-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa.py
More file actions
30 lines (26 loc) · 706 Bytes
/
Copy pathrsa.py
File metadata and controls
30 lines (26 loc) · 706 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
from util import *
def rsa_pkcs1_v15_pad(m, nbytes):
pslen = nbytes-len(m)-3
ps = b""
while len(ps) < pslen:
b = getRandomBytes(1)
if b != b"\0":
ps += b
return b"\x00\x02"+ps+b"\x00"+m
def rsa_pkcs1_v15_unpad(m):
#TODO: handle padding oracle attacks
return m[m.index(b'\0')+1:]
def rsa_pkcs1_v15_encrypt(m, key):
n, e = key
m = rsa_pkcs1_v15_pad(m, (n.bit_length()+7)//8)
m = bytes_to_long(m)
c = pow(m, e, n)
c = long_to_bytes(c)
return c
def rsa_pkcs1_v15_decrypt(enc, key):
d, n = key
enc = bytes_to_long(enc)
dec = pow(enc, d, n)
dec = long_to_bytes(dec)
dec = rsa_pkcs1_v15_unpad(dec)
return dec