Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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$.

share|improve this question
Can you expand a little bit on your protocol? What are you planning to do with Diffie-Hellman? Are you only going to use primes between 0 and 255? – pg1989 Nov 9 '13 at 21:05
yes, i only need it to produce values between 0 and 255, the rest i have to find a way, but it isnt a problem, but without this i cant do nothing – JaimeASV Nov 9 '13 at 21:33
It's really unclear what you need here, can you explain it a little more? – pg1989 Nov 9 '13 at 21:42
When two people agree on the generator and the primemodulus, they select two secret exponencials, they do the math and give that value that came from to each other, after that they do the math again and obtain the secret key, right? i need that after all this math, that secret key they both obtain only have values from 0 to 255. – JaimeASV Nov 9 '13 at 21:58
@JaimeASV Deriving an 8 bit key is trival using a hash function. But what's the point of using such a small key? Symmetric keys should be at least 80 bits, preferably 128. (and obviously 17 is much too small as well, you need primes larger than a thousand bits to be secure) – CodesInChaos Nov 9 '13 at 23:05
up vote -1 down vote accepted

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$.

share|improve this answer
-1, your proposed solution is highly insecure. Use a much larger modulus, hash the result and truncate to 8 bits. – orlp Nov 10 '13 at 13:11
I completely agree with @nightcracker's comment — as long as your “project” does not assume a world where computational power doesn't exist, your solution is not fit for practical implementation. – e-sushi Nov 10 '13 at 15:38
yes, one step at a time, this is a university project, thks for the advice, i have to reasearch in how to do that, but for know the ideia is to implement the ideia of Diffie-Hellman. But if we want to enter in those paths, there are also attacks that can be done that doesnt matter how secure or how large is the modulus. The thing is, i only needed to understand how to do this. but thanks for the attention, its always good to learn new things ;). – JaimeASV Nov 10 '13 at 15:58
btw. DH never returns 0 if both sides follow the protocol. Only way to obtain 0 is if one side uses 0 as public key $A$, which is obviously broken and doesn't result from computing $A=g^a$ for any private key $a$. – CodesInChaos Nov 10 '13 at 16:02
yeap your right, my bad – JaimeASV Nov 10 '13 at 16:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.