-
-
Save skywalker365/9e79aae2c01b7fb323d20ee5f2a10540 to your computer and use it in GitHub Desktop.
ECDH Encrypted Key Exchange (Python 3.6+)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
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) | |
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() | |
text = "Hello World!" | |
alice = DiffieHellman() | |
bob = DiffieHellman() | |
encrypted_message = bob.encrypt(alice.public_key, text) | |
print(encrypted_message) | |
decrypted_message = alice.decrypt(bob.public_key, encrypted_message, bob.IV) | |
print(decrypted_message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment