This is a bit of a deep dive into cryptography and how it works. Many questions have been asked on how cryptography works and how it ensures the information contained in it is safe. What we will discuss is the concept of what is called asymmetric encryption as that is the most commonly used today.
Asymmetric encryption uses what is called one way functions to generate 2 extremely large prime numbers that are mathematically related. These keys are known as public and private keys where public key can be shared with anyone you would like to communicate with confidentially as long is your private key is held secret to you and you alone.
When a message is sent from lets say Bob to Alice, Bob will use Alice’ Public key to encrypt the message he sends to her. Now before this happens a function called hashing is used to create a long string of the original message. What is this for? well lets say I create a secret email. This email is hashed producing a long string of characters that represents all letters spaces, punctuation etc of that text. If that text is intercepted and changed for any reason, guess what? the hash changes. The hash is easily reversible to change it back to plaintext when received and is publicly known.
So what does this mean for secure communication? Well, Bob does not encrypt the plain text of the email and sends to her, he encrypts a hash of the text and sends that along with an encryption of the hash to Alice using Alice’s public Key to encrypt. So the question is why cant someone just use a known public key to decrypt the same said message. Well, due to alot of mathematics, using one way functions to generate 2 keys, this means that message MUST be decrypted using Alice’s private key. These keys are several thousands in size and at this time impossible for the aggregation of the entire worlds computational power to determine what the private key is in short order of time. Lets take this for example if i gave you 3 numbers
569 x 757 x 911
Asked you to give me the result of the equation. After a short amount of time you would be able to tell me the result is 392397763. However if I gave you 392397763 and asked you to give me the prime factors. It would be nearly impossible for you to come up with this. This is how cryptography works on a much larger scale. This also means that a hashed message to Alice encrypted with her public key, can only be decrypted using her private key that she holds. So any Man In The Middle that picked up the message would not be able to decrypt said message with the same public key.
Now when Alice receives this message, her system will then use its private key to decrypt the message. What comes out of the decryption is a long string of characters that is still unreadable. However this string is important as her system takes this string and compares it to another string that was sent along with the message. If the 2 strings match then the system knows the message was not forged in transit and displays to her, the plain text of the original message. If the 2 numbers do not match the system knows the message has been altered and will warn Alice that it has been compromised.
And this is how asymmetric cryptography works.