# Use Diffie Hellman to share a secret

I was trying to use Diffie Hellman to first agree on a key and then use that key to symmetrically share a secret. The python3 code using cryptography library look like

#Generate some parameters. These can be reused.
parameters = dh.generate_parameters(generator=2, key_size=2048)
# Generate a private key for use in the exchange.
server_key = parameters.generate_private_key()
n=1
client_key=[parameters.generate_private_key() for i in range(n)]

#shared key for each client
shared_key = [server_key.exchange(client_key[i].public_key()) for i in range(n)]



Now the size of the shared key is 256 bytes so when I am trying to use AES wrap and unwrap or any symmetric encryption scheme, I am getting errors related to key sizes. Can anyone please help me with what scheme to use of maybe format DH key to be used with different encryption scheme in the crypto library. Thanks

• You would normally use a KDF one or more times on the statically sized output to derive keys. As such, the shared_key is probably better named shared_secret because, as you found out, it isn't directly usable as a key. For a modern KDF, you can use HKDF. For an quick hack, just use SHA-256. Sep 7 at 16:57