This is in fact one of the great challenges in encryption – [key exchange](https://en.wikipedia.org/wiki/Key_exchange).
Historically this was done by using a pre-shared key – physically meeting and exchanging the keys.
However there are better ways to exchange keys. One way is the [Diffie Hellman key exchange](https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange). The idea is that each party generates some secret number x, uses it to calculate another number y, and shares it with the other party. Then both parties use those numbers to calculate some shared number z, which serves as their encryption key.
Another way is public key encryption. Standard (symmetric) encryption uses the same key to encrypt and decrypt. Public key, or asymmetric encryption, such as [RSA](https://en.wikipedia.org/wiki/RSA_(cryptosystem)), has different keys. Every person has a set of public and private keys. When something is encrypted with the public key, it can then be decrypted with the private key. Usually you don’t encrypt the entire data with this mechanism – instead, you randomly generate a key to be used in symmetric encryption, then you can encrypt that key using the recipient’s public key.
Latest Answers