-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
63 lines (52 loc) · 1.83 KB
/
utils.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
import base64
import hashlib
from Cryptodome import Random
from Cryptodome.Cipher import PKCS1_v1_5
from Cryptodome.Math.Numbers import Integer
from Cryptodome.Util.number import ceil_div, bytes_to_long, long_to_bytes, size
from Cryptodome.Util.py3compat import bchr, bord, b
'''
pycrypto doesn't allow non hash objects in Cryptodome.Signature.PKCS115_SigScheme.
This modified PKCS115_Cipher function encrypts messages using (d,n) instead of (e,n)
and using 0xFF when padding rather than random bytes.
It's equivalent to the RSA/ECB/PKCS1Padding when encrypting data using private keys in Java.
'''
def _encrypt(key, message):
mod_bits = size(key.n)
k = ceil_div(mod_bits, 8)
m_len = len(message)
# Step 1
if m_len > k - 11:
raise ValueError("Plaintext is too long.")
# Step 2a
ps = []
while len(ps) != k - m_len - 3:
new_byte = bchr(0xFF)
if bord(new_byte[0]) == 0x00:
continue
ps.append(new_byte)
ps = b("").join(ps)
assert (len(ps) == k - m_len - 3)
# Step 2b
em = b('\x00\x01') + ps + bchr(0x00) + message
# Step 3a (OS2IP)
em_int = bytes_to_long(em)
# Step 3b (RSAEP)
if not 0 < em_int < key._n:
raise ValueError("Plaintext too large")
m_int = int(pow(Integer(em_int), key._d, key._n))
# Step 3c (I2OSP)
c = long_to_bytes(m_int, k)
return c
def rsa_encrypt(key, data):
b = data.encode('utf-8')
s = b''.join([_encrypt(key, b[i:i + 117]) for i in range(0, len(b), 117)])
return base64.b64encode(s)
def rsa_decrypt(key, data):
sentinel = Random.new().read(35)
cipher = PKCS1_v1_5.new(key)
b = base64.b64decode(data)
s = b''.join([cipher.decrypt(b[i:i + 128], sentinel) for i in range(0, len(b), 128)])
return s.decode('utf-8')
def md5_digest(s):
return hashlib.md5(s.encode('utf-8')).hexdigest()