-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecdh.py
More file actions
75 lines (62 loc) · 2.74 KB
/
Copy pathecdh.py
File metadata and controls
75 lines (62 loc) · 2.74 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
# ECDH Encrypted Key Exchange
# https://gist.github.com/byt3bl33d3r/84e298c62b310509febf8a4a90f82893
# Modified by: Ilias Papalamprou
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 secrets import token_bytes
# Added
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ec
class DiffieHellman:
def __init__(self):
self.diffieHellman = ec.generate_private_key(ec.SECP384R1(), default_backend())
self.public_key = self.diffieHellman.public_key()
# self.IV = token_bytes(16)
# TODO : Better is to have random generation each time
self.IV = b'T\x19F\xbdy2\x0f\x918\x80\xa5\x1a\xe4\xf39\x06'
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 = Cipher(algorithms.AES(derived_key), modes.CBC(self.IV), backend=default_backend())
encryptor = aes.encryptor()
padder = padding.PKCS7(128).padder()
padded_data = padder.update(secret.encode()) + padder.finalize()
return encryptor.update(padded_data) + encryptor.finalize()
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)
aes = Cipher(algorithms.AES(derived_key), modes.CBC(iv), backend=default_backend())
decryptor = aes.decryptor()
decrypted_data = decryptor.update(secret) + decryptor.finalize()
unpadder = padding.PKCS7(128).unpadder()
return unpadder.update(decrypted_data) + unpadder.finalize()
# Added functions
def get_key_hex(public_key_object):
public_key_bytes = public_key_object.public_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
public_key_hex = public_key_bytes.hex()
return public_key_hex
def get_key_object(public_key_received_bytes):
received_public_key = serialization.load_der_public_key(
public_key_received_bytes,
backend=default_backend()
)
return received_public_key