# How does the receiver compute the private key when using RSA?

I understand the public key and the private key are generated at the same time. The payload is encrypted with the public key. Sender Alice sends public key $[n,e]$ and cipher text $C$ to Bob.

Now is Bob recomputing the private key $d$?

If Bob is computing the private key $d$, can a "Man in the Middle" who gets the public key and cipher text also compute the same?

Or is there a scenario in which both Alice and Bob know the value of $p$ and $q$?

I am confused. Please help me understand this in a scenario assuming Alice and Bob are located in two different geographical locations.

I understand public key and private key is generated same time.

Yes, Bob does that, and gives the public key to Alice.

The encryption is done with public key. Sender Alice sends public key [n,e] and cipher text C to Bob. Now is Bob recomputing the private key "d"?

No, Bob created the private key, and so doesn't need to 'recompute' it; he just saves it in secure storage.

If Bob is computing the private key d, a "Man in the Middle" who gets the public key and cipher text can also do compute the same?

No, the man in the middle didn't create the public/private key pair, and so (unlike Bob) he doesn't have the private key in storage.

The only nontrivial issue here is "when Bob initially gives the public key to Alice, how does Alice know she got Bob's public key (and not a "man in the middle" public key)?". Solutions for this include such as a one-time out-of-band authentication (say, a phone call, which might be worth it if Alice is going to encrypt a lot of messages with Bob's public key), or a "certificate" (which is something signed by a trusted authority which says "Bob's public key is [XXX], sincerely Big Brother")

• thanks @poncho that really helped. I had a wrong concept that Alice was creating the keys – Nithun Chand Dec 16 '16 at 20:46
• Strictly speaking, the public and private keys aren't generated at exactly the same time. Bob chooses a random private key first, and then computes the public key from that. – Gordon Davisson Dec 17 '16 at 7:47