If I had to re-phrase your question from how I understand it, you are asking the following:
If public key cryptography ensures that a public key can be derived from a private key, but a private key cannot be derived from a public key, then you might wonder, how can a public key decrypt a message signed with a private key without the sender exposing the private key within the signed message to the recipient? (re-read that a few times until it makes sense)
Other answers have already explained how asymmetric cryptography means that you can either:
- Encrypt with public key, decrypt with matching private key (pseudocode below)
var msg = 'secret message';
var encryptedMessage = encrypt(pub_key, msg);
var decryptedMessage = decrypt(priv_key, encryptedMessage);
print(msg == decryptedMessage == 'secret message'); // True
- Encrypt with private key, decrypt with matching public key (pseudocode below)
var msg = 'secret message';
var encryptedMessage = encrypt(priv_key, msg);
var decryptedMessage = decrypt(pub_key, encryptedMessage); // HOW DOES THIS WORK???
print(msg == decryptedMessage == 'secret message'); // True
We know that both example #1 and #2 work. Example #1 makes intuitive sense, while example #2 begs the original question.
Turns out, elliptic curve cryptography (also called "elliptic curve multiplication") is the answer to the original question. Elliptic curve cryptography is the mathematical relationship that makes the following conditions possible:
- A public key can be mathematically generated from a private key
- A private key cannot be mathematically generated from a public key (i.e. "trapdoor function")
- A private key can be verified by a public key
To most, conditions #1 and #2 make sense, but what about #3?
You have two choices here:
- You can go down a rabbit-hole and spend hours upon hours learning how elliptic curve cryptography works (here is a great starting point)... OR...
- You can accept the properties above--just like you accept Newton's 3 laws of motion without needing to derive them yourself.
In conclusion, a public/private keypair is created using elliptic curve cryptography, which by nature, creates a public and private key that are mathematically linked in both directions, but not mathematically derived in both directions. This is what makes it possible for you to use someone's public key to verify that they signed a specific message, without them exposing their private key to you.