How a sender will ensure that the public key used for encryption is the key of intended reciver? Say Alice is the sender and Bob is the receiver. Alice has the public key of Bob and wants to send some data to Bob. How Alice will ensure that the public key she is having is really belongs to Bob?
This is usually guaranteed by a Public Key Infrastructure (PKI). Typically a certificate authority (CA) signs and issues certificates that bind public keys with the associated entities' (e.g., Bob's) identity. A CA is a trusted third party such that everyone can verify the CA's signature on a certificate to see if it is valid or not. Such a verification algorithm (or the CA's public key associated with its signing secret key) has already been distributed to Alice and any other users who trust the CA (e.g., pre-installed on a laptop).