Can Diffie-Hellman generate values in a specified range? Is there any way to generate values only between $0$ and $255$ with the Diffie-Hellman method? I need to create keys for communication algorithms with $32$ and $16$ numbers between that range.

The idea is to exchange the keys between the participants using the results of the Diffie-Hellman key agreement algorithm. Then we create the keys on the clients side, so they can use the keys.

Letting $x$ be any number,
$$3^x \bmod 17 = W$$

$W$ can be any number between $0$ and $17$. I need $W$ to be a number between $0$ and $255$.
The only values we can obtain from Diffie-Hellman are values between $0$ and a prime number.

So, if i want $255$, i can use $251$ or $257$ and work with those. This is what is called the prime modulus. Then we have the generator, that is a primitive square root of the prime modulus. This value only is the primitive square root if after doing all the maths we obtain all the values from $0$ to $251$, if we use the prime modulus of $251$.

This is the way we compute the square primitive root.


  If $p$ is prime, then $b$ is a primitive root for $p$ if the powers of $b$ include all of the residue classes mod $p$.


So, $x$ being any number, $11^x \mod 251$ equals to any number between $0$ and $251$. If we used the $257$ we need to see what if the primitive square root of this value, and substitute the $11$.