-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecdh.py
57 lines (44 loc) · 1.77 KB
/
ecdh.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
from secrets import token_bytes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, padding
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePublicKey as ecpk
from base64 import b64encode
from Crypto.Cipher import AES
class ECDH:
# ECDH parameters
curve = "SECP256K1"
tipo = "ecdh"
def __init__(self):
self.diffieHellman = ec.generate_private_key(ec.SECP256K1(), default_backend())
self.public_key = self.diffieHellman.public_key()
self.IV = token_bytes(16)
def encrypt(self, public_key, secret):
shared_key = self.diffieHellman.exchange(ec.ECDH(), public_key)
derived_key = HKDF(
algorithm=hashes.SHA256(),
length=32,
salt=None,
info=None,
backend=default_backend()
).derive(shared_key)
AES_OBJ = AES.new(derived_key, AES.MODE_CFB, iv=self.IV, segment_size=128)
decrypt = AES_OBJ.encrypt(secret)
return decrypt
def decrypt(self, public_key, secret, iv):
shared_key = self.diffieHellman.exchange(ec.ECDH(), public_key)
derived_key = HKDF(
algorithm=hashes.SHA256(),
length=32,
salt=None,
info=None,
backend=default_backend()
).derive(shared_key)
decrypt_cipher = AES.new(public_key, AES.MODE_CFB, iv=self.IV, segment_size=128)
plain_text = decrypt_cipher.decrypt(secret)
ecpk.public_
return plain_text
def debugClass(self):
return self.curve