Search code examples
c#encryptioncryptographypublic-key-encryptionencryption-asymmetric

combining Diffie-Hellman key exchange with RSA Encryption


I've successfully exchanged keys using DiffieHellman and now wanted to use those keys to encrypt the object I need to send across the wire using RSA encryption.

DiffieHellman gives me the actual key so:
Question #1
Is there a way to import the actual key directly into a RSACryptoServiceProvider variable?

It seems I can only find examples of importing XML based but those have extra info that I do NOT get from DiffieHellman (like for Public keys, the XML carries the Exponent value, and for Private key the XMLS carries P,Q,DP,DQ,Inverse ...which none of those are provided by the DiffieHellman)

Question #2
What would be the right way to combine the DiffieHellman private/public keys provided into an RSA based encryption call so that the calls encryption/decryption would work across the wire on both ends?


Solution

  • Diffie-Hellman allows two parties to agree a mutual key over an insecure channel. Such a key would then normally be used to encrypt/decrypt the data using a symmetric algorithm (e.g. AES).

    The DH-agreed key cannot be used as RSA key, as RSA keys have a specific construction requiring prime numbers that is not shared by a DH-agreed key, and even if it was, RSA uses distinct public and private keys, whereas DH agrees a single shared key. You would also not use RSA for the main data encryption anyway, as it's significantly slower than modern symmetric algorithms.

    One way you might combine them would be if RSA was used to sign the key exchange to provide authentication and protect against a man-in-the-middle attack, since DH on its own does not provide authentication.