# Using Diffie-Hellman secret key to encrypt/decrypt data?

My end goal is to encrypt data with AES-128, send it to other process and decrypt it there. While using DH key exchange protocol.

I have used DH to generate a 256 bytes shared secret, but I can't use that key to fit the 128-bit encrypting function.

I read that the secret needs to be fed through a "Key-based key derivation function" (KBKDF), but as I understand that gives you a hash which then you won't be able to dehash... so how does the other program can decrypt the message?

Can I just truncate the secret message to 128-bit? (I don't think this is the right solution though).

How would the encryption and decryption of the hashed secret work?

Side note. I am trying to do all this with OpenSSL library in C

Thanks.

I read that the secret needs to be fed through a "Key based key derivation function" (KBKDF), but as I understand that gives you a hash which then you won't be able to dehash... so how does the other program can decrypt the message?

The encryptor sends the DH shared secret through the KDF, which gives him a key, which he uses as (for example) an AES key, and uses that AES key to encrypt his data.

The decryptor sends the same DH shared secret through the KDF, which gives him the same key, which he uses as an AES key, and uses that AES key to decrypt his data.

There's no need to 'invert' the KDF; both sides use it in the forward direction

• It makes sense now. I wasn't sure both ends were going to give the same key. Got the KDF by using the Bytes2Key() function on openSSL. Thanks for the help. Mar 15, 2019 at 16:56
Diffie Hellman is a key exchange protocol with perfect forward secrecy. Actually, both sides agree on the key, that is at the end of the protocol both sides have the key $$g^{ab}$$ where
• Alice chooses a random secret $$a$$ and sent $$g^a$$ to Bob, and
• Bob chooses a random secret $$b$$ and sent $$g^b$$ to Alice.
Alice calculates $$(g^b)^a = g^{ab}$$ and similarly Bob $$(g^a)^b = g^{ab}$$ by using their secrets.