How to calculate key size for Diffie-Hellman key exchange? How to calculate key size for Diffie-Hellman key exchange? The strength obviously depends on both generator and prime modulus.


  Formula:
  
  $$g^x\bmod p$$
  
  Public values:
  
  $$g=3$$
  
  $$p=17$$
  
  Alice:
  
  $$x=15$$
  
  $$3^{15}\bmod 17=6$$
  
  Bob:
  
  $$x=13$$
  
  $$3^{13}\bmod 17=12$$
  
  Alice:
  
  $$12^{15}\bmod 17=10$$
  
  Bob:
  
  $$6^{13}\bmod 17=10$$


The original numbers $(3,17,13,15)$ weren't that (no way secure, but not that) low and they ended up with 10 which is much easier to brute force than guessing each part from $g^x\bmod p$.
With Diffie-Hellman in its most general description, there are three different types of objects involved:

The group
The exponents
The generator


The group
Diffie-Hellman operates in a group. This group may be $\mathbb Z_p^*$ or may be the points on your favorite elliptic curve usually. Usually the group parameters are what needs to be adjusted over time the most due to computational advances and the advances in cryptanalysis of the discrete logarithm problem (DLP). So what you want is a group where the DLP is assumed to be hard and for this you need to adapt the group or change parameters (like the prime or curve parameters).*
The exponents
There isn't much to the exponents with diffie-hellman except that you want to avoid predictable exponents, because then the other party or an adversary could predict your exponent and calculate the shared secret from that.
The generator
The generator of the subgroup of the group is actually fairly irrelevant. You need to make sure it indeed generates a subgroup of appropriate size and that's about it, especially you should avoid the neutral element or other elements with small order.

*This is actually not true as you usually want the computational diffie-hellman (CDH) problem to be hard, but these two are very closely related most of the time such that there's no known way to break CDH without breaking DLP.

As for the &quot;the shared secret is actually easier to brute-force than all other values&quot;. While this is true here, this doesn't scale.
Brute-force of the shared secret scales at $\mathcal O(q)$ (because the shared secret is a random element from the group, with $q$ being the order of said group).
The best generic attack on the discrete logarithm problem scales at $\mathcal O(\sqrt q)$ (there are more efficient ones for prime-moduli-groups).
Now chunk in some numbers. If you multiply $q$ by, let's say, 10,000 you get a workload increase of 10,000 for brute-force, but only 100 for the better attack. So brute-forcing the shared secret clearly isn't the way to go for real-life parameters.

If you're seeking for advice on which keylengths to choose and which parameter set corresponds to which security level, you can use Keylength.com for the TL;DR and read the Lenstra-Verheul paper (PDF) for all the details.