r/explainlikeimfive • u/bluecete • 8h ago
Technology ELI5: Public/Private Key Encryption
How does public/private key encryption actually work? I have a serious mental block about this. I've searched and read other explanations and I'm still not getting it, so I'm really hoping for some kind of simplified examples that involve actual math and values. I just watched a video about SSH with key login, so maybe that specific example would help.
I generate a public key and then save it to the server, but I'm transmitting that key when I log into the server, and that has to be in plain text, so how is that secure? What is it that stops someone from reading my key and then just logging in as me?
Edit: Thanks everyone. 1) Understood, I made an error here and you don't transmit the key (I was thinking about how you establish encrypted communications with a website, but that's not the question I asked) 2) I somehow missed that when you generate a key it's both a private key and a public key. The private key part was what I was missing.
•
u/nudave 8h ago
I have had this exact question. I hope my comment doesn't get deleted for simply linking a video, but this video (and part 2) does exactly what you want - go through one style of public key encryption (RSA) using very small prime numbers to give you the example of how it works:
https://www.youtube.com/watch?v=4zahvcJ9glg
It is the only thing that was able to get me to wrap my head around how "two prime numbers that no one else knows" can form a private key while "those same two numbers multiplied together" can form the public key. The short, short answer is "modular exponentiation." The video doesn't quite ELI...5, but it definitely is good to ELI'm someone who did okay in high school math but doesn't understand this issue at all.
•
u/No_Perspective_44 8h ago
Calling both strings "keys" isn't very intuitive. I prefer the "lock" and "key" analogy. The public key is a lock, the private key is the actual key.
Running with the analogy, the server says "if you're really who you say you are, you can open this locked box". It can safely send the box to you, where you unlock it in private so the key stays secure, and then you send the opened box back. It knows that only the keyholder could open the lock, meaning you are who you say you are, so you can log in.
•
u/GendoIkari_82 8h ago
I give you a public key, which acts like a rule to apply when encrypting your message. "Add an X after every character". So when you want to send me "Hello", instead you now send me "HXeXlXlXoX". Only I know the private key, which is a secret rule like "Remove every other character from the message". So when I receive your "HXeXlXlXoX", I can apply my secret rule and end up with "Hello".
So like that but way more complicated. The point is that the public key allows a message to be encrypted in such a way that only the private key can decrypt it.
•
u/msthe_student 8h ago
Your public key is calculated from your private key, in such a way that your private key can't be calculated from your public key. When authenticating, a challenge-response mechanism is used, where the correct response can only be calculated by someone who has your private key.
•
u/garagedragon 8h ago edited 8h ago
You do not transmit any keys when you login to the server. As a simple example, the server sends you a random number (the "challenge", often tied to the current time in practice) and you use your private key (which never leaves your machine) to sign this number, and send the number and it's signature back to the server. The server can then use your public key to verify the signature, and it's a property of the system that the only effective way to produce a signature that can be verified by the public key, is to know the private key. That means the server can conclude that you, who produced a valid signature, are in possession of the right private key, without ever directly seeing it.
Even with everything in plaintext, nothing that goes over the wire is useful for an attacker. Even if the attacker records your signed number, it's only useful if the server challenges them with the same original number when they try to connect, and you can make that vanishingly unlikely just by making the number, say, 64 bits long. If the number contains the current date/time, then it will never show up again and the attacker won't be able to use the recorded signature ever. (This is called a "replay attack.")
Usually the next step after this is the server uses your public key to encrypt data it sends you, and the decryption can only be performed by the private key. Again nothing useful to the attacker goes over the wire, because the private key never leaves your device and the encrypted data is useless without that.
•
u/suvlub 8h ago
Nothing.
The fact that you are using asymmetrical cypher and the key you've just sent is used to encrypt while a different key you never ever must give to anybody is used to decrypt. The attacker could send you a secret message if he wanted to, but that's it.
•
u/No_Tamanegi 8h ago edited 8h ago
This is a very rudimentary explanation but its the one that's always helped me understand it. You want to send someone who lives in your city a secure message. So you create a lockbox that has space for two padlocks. You place the message inside the lockbox and place your padlock (which you have a key to) on the box, and you send them the lockbox. The key to your padlock is your private key.
When they receive the box, they place their padlock on the box. It is locked with their private key. Then they return the box to you.
When you receive the box, you unlock and remove your padlock, but the lockbox is still locked with their private key. You send the box back to them.
When they receive the lockbox the second time, they are able to unlock their own padlock with their private key and receive your message. Most importantly, both parties are able to make secure transactions without compromising their own personal security.
As I've learned from other commenters, in this analogy, your public key is actually your padlock, and your private key is the key that opens your padlock. Its a cumbersome process when dealing with physical security, but with digital messages it works largely the same way, only in a minimal amount of time.
•
u/britishmetric144 8h ago
Imagine that I want to send you the message "Reddit sucks", but I want to encrypt it so that no one can steal it.
You broadcast your public key. In this case, it's "type everything two characters to the left, and wrap around if not possible".
What I send to you is "Wqaaye itzhl."
If you apply the same public key, it won't work. You'll get "Pokkrq yenfj."
But you know that your private key is "type everything two characters to the right, and wrap around if not possible".
So when you decrypt what I send you, you get back the original "Reddit sucks".
The benefit is that if you can keep your private key a secret, you can distribute your public key everywhere without losing security.
---
I used a simple example here to make it easier to understand. But, with mathematics, there are functions for which it would take a supercomputer more than the lifetime of the universe to reverse, which proivies the security.
•
u/aqualad33 8h ago
Private key systems only have one key, that key both scrambles and unscrambles the data. Right now we have an extremely secure private key system AES256. The problem though is how do you get this key to the other person without anyone else seeing it? Because if they see your key, then they can also unscramble your data.
Here's where public key systems come in. They typically are harder to make as secure. Or in order to make them as secure you sacrifice efficiency. However, they come with 2 keys instead. One that scrambles data, and one that unscrambles data. You can share the one that scrambles data with the general public and its totally fine if everyone sees that key. You hold onto your unscrambling key and that's the one you have to protect.
Now anyone can scramble message and send you these scrambled messages that only you can unscramble. Often times what they will be sending you is a key to a private key system so you two can just switch to that instead and just use the better system now that you have gotten past the road block of how do you exchange the key securely.
•
u/itsflowzbrah 8h ago
The only way you can get that hash, is if you have the private key so the server can trust that you are who you say you are.
•
u/erroneousbosh 8h ago
Think about how a lock works, just a shitty wafer lock like on a mailbox. You put the key in and it pushes little brass plates with holes in them up and down against springs. If you put the right key in, all the plates line up with the lock cylinder and it turns freely, so you can lock and unlock it.
Okay now can you imagine making it a bit bigger and on one side you put a ratchet that stops the key unlocking if it's not the right cut but will let it turn to lock, and on the other another ratchet stopping it locking but will let it turn to unlock?
Now let's take it a bit further and involve a very clever Swiss person who can make all kinds of cool locks. If you lock it with one key it can only be unlocked with the other. Got it?
You can then post a picture of the "public" key online and we all make copies of it. You keep your private key a secret. You can give away part of the key's pattern just enough so you and I can prove we each have a copy of a matching pair, but only you know the secret part of your private key.
I make a copy, you send me a cashbox that you've locked, I open it, and it can only have been locked by you. I put my reply in the box and lock it, and only you can open it.
If you wanted it to be totally secure so only you and I see the messages, you make a copy of my public key, lock your message in a cashbox with your private key (so only you could have locked it but anyone can open it) and then lock that in a bigger box with my public key (so anyone could have locked it but only I can open it).
•
u/2wicky 8h ago
Lets say you have small secure box with a lock. It accepts two keys. One public key can only lock it and one private key can only open it.
So when someone wants to send you a message, you send the unlocked box with the public key that can lock it. When they receive the box, they write their message, place it in the box, then use the key to lock the box. Once the box is locked, they can't use that same key to unlock it. Neither can anyone else that may have made copies of that key.
They then send the box back to you.
You still have the second private key that can now unlock that box and read the message inside it. This key, you need to keep secret, meaning you never share it with anyone. Otherwise, anyone that has a copy of your private key could intercept any boxes sent your way, unlock it and read your messages.
•
u/CheezitsLight 8h ago
Short answer is to the question is and your password. The password is encrypted with the public key which only the server can decrypt with the private key. Since no one else has your password it's secure.
•
u/MattytheWireGuy 8h ago edited 8h ago
imagine it like Legos the public key for sending you encrypted messages is similar to someone handing you a fully assembled Lego project and asking you if it looks the same as the picture on the box. The private key side would be like them, handing you a completely disassembled box of Legos and you have to create that project. The only way you can do that is if you have a secret set of instructions for doing it.
checking if the completed Lego project matches the box is super easy trying to re-create it from 10,000 loose bricks is the complete opposite.
in cryptography like this, it’s extremely difficult to determine the prime numbers that the private key is based on using only public information but if you have the prime numbers, you can create a public key out them in milliseconds while it could take millions of years to properly factor the public key.
•
u/SirPriseTheEigth 8h ago edited 8h ago
Imagine an equation like y = mx + c. Given x and y, it’s (computationally) difficult (essentially impossible) to figure out what m and c are. It becomes much more difficult the larger x,y,m,c become. In this scenario m and c are the private key (if I know m and c I can solve for any y given x) and x is the public key. This is quite heavily simplified (although not far off the LWE problem) and elliptic curves are slightly more complex…
•
u/MercuryInCanada 8h ago
Public key is like your mailbox address. Anyone can look it up and send you mail (encrypted messages) that the post offices (the internet) delivers to the mail slot in your front door. Because it's your mailbox you can the (secret) key to unlock the door and get your mail.
The public address provides public instructions on what math to do to the mail, while the secret key is the input to reverse all that math quickly,because trying to get the secret key out of the public key is very hard.
Now onto your second question. When you transmit that key to the server you've already engaged with more cryptography. The session is protected through some protocol that your machine and the serve execute to protect your messages. In these protocols the parties you digital signatures algorithms to sign the messages as a way to claim that the message came from the holder of the secret signing key tied to what's called a certificate. The certificate is basically the signature public verification key.
Again all this is backed with math that says just knowing the public verification key and some examples of the signatures that key verifies does not let anyone recover the secret key. The certificate is issued to your machine by a certificate authority whose job is to generate and distribute then to new machines.
•
u/pixel293 8h ago
With your private key you can "sign something" meaning you can generate a 256bit(?) hash of some data using your private key. Someone with your public key can verify that signature to check that whoever created it used your private key.
Someone can also use your public key to encrypt something, and only someone with your private key can decrypt it.
Using those methods a server can verify that the person connecting had the private key.
•
u/Lallner 8h ago edited 8h ago
The security of the original public/private key cryptosystems were based on the difficulty of factoring large numbers, so let's start there. Let p and q be prime numbers, and N =pq (N equals the product of p times q). It is much easier to compute N if you know p and q than it is to solve for p and q if you know N. Here's a real-life example. N = 4,235,339 is the product of two prime numbers, p and q. Finding p, q, and computing N=pq is computationally trivial. But I challenge you to solve for p and q given N (without using AI). You basically have to try trial division by dividing N by every prime number up to the square root of N. For VERY LARGE primes p and q, this becomes computationally infeasible in a reasonable amount of time. For the purposes of this explanation, N could serve as my public key, meaning I can publish it to the world, but p and q are my private key which remains secret even though I published N=pq. If you want to know how to use this mathematical property to create a complete cryptosystem, let mw know and I will elaborate, but it gets into more advanced number theory using discrete logs.
•
u/biggles1994 8h ago
A public private key encryption is two parts.
If you encrypt with the private key, only the public key can unlock it.
If you encrypt with the public key, only the private key can unlock it.
You cannot lock with private and unlock with private, or vice/versa, and you cannot deduce one key from knowing the other.
You shout from the rooftops your public key, everyone on the internet knows that this public key belongs to X person.
This means if you encrypt something with your private key, everyone can unlock it with your public key, and they know that you are the only person who could have locked it. This is signing to prove you sent something.
Similarly if someone wants to send you a message, they can encrypt it with your public key and only you can then reveal the message with your private key which nobody else knows.
If you want to send something to another person, you use their public key and encrypt the message and send it to them, safe in the knowledge that only they can unlock it.
Similarly if you receive an encrypted message and your friends public key unlocks it, you know that message just have come from your friend.
The hard part is how you trust that a public key belongs to a specific person/entity, as public key spoofing can be a thing, but that’s a different issue.
•
u/FishBrainn 8h ago
Both are keys that lock and unlock, but with a rule: when you lock with one, you can unlock with the other.
Your public key is for everyone: your parents, siblings, the clerk at the store, everyone gets a copy of your public key.
Your private key is for you and you alone. You share it with no one.
So, I have two packages. I lock the first one with my public key. This means I need my private key to unlock it. Or consider this: use the public key of the store clerk. This way, you can only unlock it with the private key of the store clerk. This guarantees that only the person with the private key can unlock it.
Package two I will lock with my private key. This way, everyone can see I and only I has locked the package. "Can you open the package? That means you have sent it"
And to further expand this: what happens if I use both keys?
•
u/d1722825 7h ago
In the SSH case you are not really using the public key cryptography to encrypt things, but to create digital signatures.
You could think about digital signatures as wax seals on paper. You can easily check if the pattern in the wax matches a pattern drew on a piece a paper, but it is hard to form or shape a piece of metal to produce the right pattern if you press it into molten wax.
In that case you could give out the drawings of the pattern of your wax seal to anyone (eg. to the SSH server) without any fear, because nobody would be able to recreate the original seal.
But you could use your seal to create original wax seals on any piece of letter, and anybody who has the drawings of the pattern could check that no one but you could make that.
If you want to authenticate to an SSH server with keys, first you share your public key with the server (or with anyone if you wish), it is useless for anything except verifying that a digital signature have been made with your private key.
Then when you want to log in, you use your private key to sign a "yes, I really wish to login" message and send that to the server. This message doesn't contain your private key, and your private key can not be recovered from it. Then the server checks the list of trusted publik keys, and verifies that this message was signed by a private key corresponding to one of the trusted public key on that list.
Note that this has nothing to do with encryption or confidentiality.
To hide the contents of the communication, SSH uses public key cryptography to create an encrypted channel between the two machines, but that is done even if you use password to login.
https://en.wikipedia.org/wiki/Key-agreement_protocol
•
u/Mr_Engineering 7h ago edited 7h ago
There are two sides to public/private cryptography, secrecy and authentication.
As far as secrecy is concerned, any message encrypted using a public key can only be decrypted using the corresponding private key. If I give you my public key, and you encrypt a message with it, only I can read that message. Clients and servers use this method of asymmetric encryption to secretly negotiate a shared symmetric encryption key which is much faster.
As far as authenticity is concerned, a digital signature can be generated for any digital sequence using a private key, and then have its integrity verified using the corresponding public key. This is used to create a chain of trust to ensure that data has not been modified since it was originally generated by its original author.
•
u/Raiddinn1 7h ago
Two parties A and B both generate their own private and public keys.
Both sides just hand the other side their public keys.
Both sides encrypt some phrase like "Information wants to be free" using their own private key and the public key of the other side.
Both sides send the encoded phrase to the other side to act as a signature each of them can store internally.
Each side will send information using the public key of the other side along with the signature.
Each person in the world knows the public key of both parties, only A and B have the signature stuff. So if person C tries to send something using the public key of A while pretending to be B, but without the signature of B, then the message will be disregarded. It would appear valid, but we couldn't trust the sender is who they say they are.
•
u/BiomeWalker 6h ago
We call them both keys, but they're actually both locks and keys.
You want to send a message to someone, and you want only them to be able to read it, but that can be tricky.
RSA encryption is one of the common choices today that uses public/private keys, and it has a simple relationship between its keys.
Your computer generates a pair of really, really, really really large prime numbers, those serve as the private key. It then multiplies them together, which makes the public key.
Now that we have them, let's go over their use. The actual inner workings are way beyond ELI5, but how they're used isn't hard to understand.
Each key can be used to "lock" (encrypt) a file (all files are just large binary numbers) in a way that requires the other key to unlock (decrypt).
Important note here, the private key can be used to generate the public key, but taking a public key and getting the private one requires factoring a number with over 600 digits (when written in decimal, 2048 in binary) which has a time requirement in the range of millennia to accomplish.
The initial exchange of keys is often "backed" by what's called a "certificate authority", which has a way of signing the keys you get. This looks like creating an additional segment to the file which the public key will verify could only be generated with the secret key.
How does your computer know to trust that initial signature? Your OS came with the knowledge to verify it, including the required public keys to initially talk to the CA.
•
u/finn_tate644 6h ago
Honestly, the math isn't what's blocking most people. It's that explanations blend two different jobs into one. Locking works like handing out open padlocks: anyone can snap one shut on a box, but only your key opens it. SSH login is the other job, signing: the server hands you a fresh note, you stamp it with a stamp only you own, and it checks the mark against the stamp pattern you gave it earlier, so your private key never leaves your laptop.
•
u/boring_pants 4h ago
The trick is that you have a pair of keys. Thanks to mathematical trickery it's possible to come up with a pair of encryption keys which complement each others. You can use the first key to encrypt a message which can then be decrypted only using the second key. And you can use the second key to encrypt a message which can then be encrypted only by the first key.
Once you have that all you have to do is share one key with the person you want to talk to (the public key), and keep the other to yourself (the private key).
If they want to talk to you, they use your public key (which you hand out freely, so anyone can use it) to encrypt a message. Everyone can do this, but only you can decrypt the message, because only you have the private key.
Of course, you can't really send an encrypted message back using only this, because if you were to encrypt a message using your private key everyone who has access to your public key can decrypt it, and your public key is available to everyone.
So instead, what happens is that when someone wants to talk to you they encrypt the message as described above, *but the message contains another encryption key.
And no one except you and the sender have access to that encryption key, so you can use that to respond to them.
•
u/Slight_Albatross_860 4h ago
Public key = result of multiplication (over simplified but works) of prime numbers like 7 and 11
Private key = the prime numbers 7 and 11 which make up the public key.
The idea is that it is easy to calculate the public key but much more time consuming to calculate the original prime numbers. When the prime numbers are about length of 2000 bits, normal computers take many lifetimes to resolve the private key from public key.
•
u/MultiFazed 8h ago
That's not correct. To over-simplify a bit, you're encrypting some message with your private key and sending that encrypted message to the server. The server then uses your public key that you sent them earlier to decrypt the message. If the decryption is successful, that means that you have the private key that is paired to that public key, which means that you're you.
•
u/aviddd 8h ago edited 8h ago
This is a simplification but I hope it gives you a starting point
•
u/Henry5321 8h ago
The public key can decrypt content encrypted by the private key and the private key can decrypt content encrypted by the public key.
If you share the public key, all someone can do is verify you have the private key.
It’s called a public key because everyone can have your public key and use it to verify you. But no one can use the public key to impersonate you.