Question: Trying to Encrypt a text message using RSA algorithm. To encrypt the message the key from client is required. How are the keys exchanged. I have studied few key exchange algorithms but could not find any example code.

Can some one guide to a tutorial, on how to exchange the keys on internet.

Answer: 
U can use this sample [program](http://publib.boulder.ibm.com/infocenter/java7sdk/v7r0/index.jsp?topic=/com.ibm.java.security.component.doc/security-component/JceDocs/api_sampleprograms.html) for key exchange algorithm. Its a Diffie-Hellman Key Exchange between 2 Parties. Just try to understand what this algo is [here](http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange).
The main methods of distributing keys are through e.g. PGP, which uses signatures from other trusted parties to confer trust in keys. X509 certificates, such as used in browsers, use certificate chains, where a certificate from a higher up authority is explicitly trusted. These methods of trusting certificates is called PKI, public key infrastructure.

If you don't have an established trust you might send each other a certificate or public key, by mail then calculate a "fingerprint" over the certificate. Then use an out of band procedure (telephone, envelope etc.) to validate the fingerprint. Or simply meet up and exchange a USB thumb drive after checking each others credentials.