To provide some additional context for the information provided in the other answers.
It is true that RSA is used to encrypt / decrypt information
However:
In almost all real-world cases RSA is not used to encrypt a large message being exchanged but rather just used to encrypt the hash of the message (this is signing / creating a signature for the message)
As other answers point out, this makes it possible for the receiver to know with certainty that the message has not been tampered with by some intermediary.
The steps are (a bit simplified for illustration purposes):
- Alice creates message for Bob
- Alice creates a hash for the message
- Alice encrypts the hash value using her private key - this 'encrypted hash value' is the signature for the message
- Alice sends the message, in clear text, plus the signature (encrypted hash value) to Bob
- Bob creates a hash for the message (using the same hash algorithm as Alice used in step 2)
- Bob decrypts the encrypted hash value provided by Alice, using Alice's public key
- Bob compares his hash value for the message (from step 5) with the decrypted hash value (from step 6)
If the two hash values compared in step 7 are the same, Bob knows with certainty that the signature was created by Alice (since only she has access to her private key) and that the message he received was exactly how Alice authored it, i.e. no one has made changes to the message.
On the topic of confidentiality, encrypting the message itself is almost always done using an encryption algorithm other than RSA (since RSA is slow and optimized for encrypting small information sets). This can be accomplished many different ways but one option is to add to the steps:
- Alice creates message for Bob
- Alice encrypts the message using a symmetric key encryption algorithm using key of her choosing that only she knows
- Alice encrypts the symmetric key using Bob's public key and adds it to the end of the message
- Alice creates a hash for the message
- Alice encrypts the hash value using her private key - this 'encrypted hash value' is the signature for the message
- Alice sends the encrypted message, plus the signature (encrypted hash value) to Bob
- Bob creates a hash for the message (using the same hash algorithm as Alice used in step 4)
- Bob decrypts the encrypted hash value provided by Alice, using Alice's public key
- Bob compares his hash value for the message (from step 7) with the decrypted hash value (from step 8)
- If the hashes match, Bob takes the encrypted symmetric key value from the end of the message and decrypts it using his secret key
- Bob decrypts the encrypted message using the same symmetric encryption algorithm Alice used in step 2, using the symmetric key value from step 10
With these steps Bob knows Alice authored the message and only he is able to decrypt it since the symmetric key used to encrypt the message was encrypted using his public key. I.e. only Bob is able to get the symmetric key value since only he has access to his private key.