I am trying to get a better grapple on how public/private keys work. I understand that a sender may add a digital signature to a document using his/her private key to essentially obtain a hash of the document, but what I do not understand is how the public key can be used to verify that signature.
My understanding was that public keys encrypt, private keys decrypt... can anyone help me understand?
The public key encrypts and only the private key can decrypt it, and the reverse is true. They both encrypt to different hashes but each key can decrypt the other's encryption.
There are a few different ways to verify that a message came from some expected sender. For example:
The sender sends:
The message
The hash of the message encrypted with their private key
The receiver:
If they aren't identical it means either the message was tampered with or it was signed with some other key and not the one we thought...
Another example would be for the sender to use a common hash that the receiver might know to use as well. For example:
The sender sends:
The receiver:
This again ensures the message wasn't tampered with and it is from the expected sender.
Thought I'd provide a supplemental explanation for anyone looking for something more intuitively revealing.
A big part of this confusion arises from naming 'public keys' and 'private keys' as such because how these things actually work is directly at odds with how a 'key' is understood to be.
Take encryption for example. It could be thought of as working like so:
This allows secret messages to be sent between parties, but from an intuitive standpoint here, 'public lock' is a more suitable name than 'public key'.
However, for sending digital signatures the roles are somewhat reversed:
Then when the receiver gets the message, they can read it, and then use the public key to unlock the locked message and compare the two messages. If the messages are the same, then they know that:
The unlocked message wasn't tampered with during travel and,
The message must have been from the person who has the matching lock to their public key.
And finally, this entire system only works if anyone who wants to validate a signer's signature has an authoritative place to go to to get the matching key to the signer's locks. Otherwise, anyone can say "Hey, here's the key to so-and-so's private lock", send you a message pretending to be them but lock it with their private lock, you perform all the above steps and believe the message must actually be from the person you thought, but you're fooled because you were mislead as to the true owner of a public key.
So long as there's a trust-worthy source for retrieving a signer's public key, you'll know who the rightful owner of a public key is, and will be able to validate their signature.
Your understanding of "public keys encrypt, private keys decrypt" is correct... for data/message ENCRYPTION. For digital signatures, it is the reverse. With a digital signature, you are trying to prove that the document signed by you came from you. To do that, you need to use something that only YOU have: your private key.
A digital signature in its simplest description is a hash (SHA1, MD5, etc.) of the data (file, message, etc.) that is subsequently encrypted with the signer's private key. Since that is something only the signer has (or should have) that is where the trust comes from. EVERYONE has (or should have) access to the signer's public key.
So, to validate a digital signature, the recipient
If they match, the signature is considered valid. If they don't match, it either means that a different key was used to sign it, or that the data has been altered (either intentionally or unintentionally).
Hope that helps!
As pointed in atn's answer, the keys work inversely.
Below is an example script to test this whole flow with
openssl
.This script outputs the following: