Given an RSA encryption with public key $(n,e)$. Let say I found out the private key $d$.
So I know $d\cdot e = 1(mod (p-1)(q-1))$ for the primes $p\neq q$ that generate $n$ by $p\cdot q=n$
but from here, how can I efficiency find $p$ and $q$?
And on the other way, I assume the answer is similar. If I know $p$ and $q$, how can I generate $d$ to crack the encryption?
Thanks
You know that $de = k(p-1)(q-1)+1$ for some integer $k$, and you know $n=pq$. Find the first multiple of $n$ that is greater then $de$ - this will be $kn$, and
$kn-de+1 = kpq - k(p-1)(q-1) = k(p+q-1) \\ \Rightarrow p+q=\frac{kn-de+k+1}{k}$
Once you know $p+q$ then you also have
$p-q=\sqrt{(p+q)^2-4n}$
and then you can find $p$ and $q$.
For example, if $n=187$, $d=37$ and $e=13$ then $k=\lceil\frac{de}{n}\rceil=3$ and
$p+q=\frac{3\times187 -481 + 4}{3}=28\\p-q=\sqrt{28^2-4\times187}=6$
and so $p=17$, $q=11$.