# Computing p and q from private key

We are given n (public modulus) where n=pq and e (encryption exponent). Then I was able to crack the private key d, using Wieners attack. So now, I have (n,e,d). My question is, is there a way to calculate p and q from this information? If so, any links and explanation would be much appreciated!

-
–  Ricky Demer Nov 4 '13 at 16:56

It's actually fairly easy to factor $n$ given $e$ and $d$. Here's the standard way to do this:

• Compute $f = ed - 1$. What's interesting about $f$ is that $x^f \equiv 1\ (\bmod n)$ for (almost) any $x$.

• Write $f$ as $2^s g$ for an odd value $g$.

• Select a random value $a$, and compute $b = a^g \bmod n$.

• If $b = 1$ or $-1$, then go back and select another random value of $a$

• Repeatedly (in practice, up to $s$ times):

• compute $c = b^2 \bmod n$.

• If $c = 1$ then the factors for $n$ are $gcd(n, b-1)$ and $gcd(n+1)$

• If $c = -1$, then go back and select another random value of $a$

• Otherwise, set $b = c$, and go through another iteration of the loop.

If you are familiar with the Miller-Rabin primality test, this will look familiar; the logic is the same (except that we use $ed-1$ rather than $n-1$ as the startign place for the exponent)

-
Just to clarify, so when we write $f$ as $2^s g$, do you mean $f=2^s g$? Also, is it $2^s g$ or $2^{s g}$? –  hhel uilop Nov 4 '13 at 17:09
@hheluilop: $f = 2^s \times g$; keep on dividing $f$ by two until you get an odd number. –  poncho Nov 4 '13 at 17:16