I understand how public key infrastructure works.
There is a private and public key, and someone may use the public key to encrypt data that can only be decrypted with the private key.
However there seems to be a flaw with this...
When you want someone, E.g Bob to send you a secret message, you send the key to him:
Public key ==> Bob
Bob then encrypts the data and sends it back to me.
Data ==> Public key ==> back to me
But what happens if at the first stage Public key ==> Bob
an attacker modifies the public key to one he has the private key to, then decrypts the data Bob unknowingly sends?
How is this prevented?