Skip to content

Instantly share code, notes, and snippets.

@iafisher
Last active April 5, 2018 17:07
Show Gist options
  • Save iafisher/96bf0ae49e0dbd11a1621ed2c4503c4f to your computer and use it in GitHub Desktop.
Save iafisher/96bf0ae49e0dbd11a1621ed2c4503c4f to your computer and use it in GitHub Desktop.
Minimal working crypto example with Diffie-Helman
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives.padding import PKCS7
from cryptography.hazmat.primitives.ciphers import Cipher, modes, algorithms
import os
private_key = X25519PrivateKey.generate()
peer_public_key = X25519PrivateKey.generate().public_key()
shared_key = private_key.exchange(peer_public_key)
print(len(shared_key))
backend = default_backend()
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(shared_key), modes.CBC(iv), backend=backend)
encryptor = cipher.encryptor()
padder = PKCS7(256).padder()
ct = encryptor.update(padder.update(b'hello') + padder.finalize()) + encryptor.finalize()
print(ct)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment