r/askscience • • May 18 '20

Computing How does SSL prevent the initial key from being stolen?

Hi there,

I've been looking for this answer on the web and here, but haven't found anything yet.

The best I've seen so far is this How to Geek article on encryption. In step one of how SSL works, it says "The web server sends its public key with its certificate." (article here)

And from there on the browser and web server can use the key to encrypt/decrypt. My question is, how does the initial key hand off prevent an attacker from catching the key such as in a MIM attack? There always has to be a way for the browser/server to send an initial key, but all I can think of is a never-ending loop of encryption on top of encryption for the keys.

10 Upvotes

24 comments sorted by

View all comments

7

u/ModeCollapse May 18 '20

I think your question is really about: how can 2 people share a secret when anyone could be watching?

Some algorithms are very easy to go in 1 direction, but hard to invert. For example:

"Factor the number: 899 into it's prime components" would take you a few seconds, but the results are 29 and 31.

Now if instead I posed the inverse problem and said: "What is 29*31?" you could immediately say 899.

Public key exchange involves algorithms that are easy to compute one way but difficult to invert. So the server and client will do easy calculations like 29*31, while any MITM will be faced with the inverse problem: factor 899 problem.

The most accessible of these algorithms for encryption is probably RSA which has a key that involves 3 numbers: "e", "d", and "N". Of these 3 numbers, the entire world knows "e" and "N" (these make the public key) but only the server knows "d" (this is the private key).

A client connecting to the server, that wants to send the message "a" (which is just a number in binary) will compute "c = a^e (mod N)" where (mod N) means you take the remainder after division by N. And transmit that to the server.

Computing "c" if you know "a", "e", and "N" is a very fast calculation, but the inverse, computing "a" if you know "c", "e", and "N" is a very hard calculation. So someone listening in will see you send "c" to the server, but has no idea of what your original "a" was.

The reason the server keeps "d" secret, is because the number "d" (the private key) is special, and satisfies the equation "a = c^d (mod N)" which we said was a fast calculation. This means the server can decrypt your message back into "a", and do so quickly.

The last, most important thing, is that figuring out "d" is very hard unless you know how "N" was chosen. When "N" is generated it is very easy to compute "d", but if you didn't pick "N" yourself, figuring out "d" amounts to factoring "N", but "N" is a giant number (Reddit for example has an N that is ~1074).

So after your exchange, you and the server both know "a" but any MITM will only know "c", "e", "N" from which computing either "a" or "d" would let them see your message, but those calculations are very hard.

1

u/NoJudgies May 18 '20

Ahhh. This makes a lot of sense! So the initial hand-off is just hard to invert

1

u/G00dAndPl3nty May 19 '20

The keys are created with two pieces of information. One piece comes from the other party (and is therefore sent over the wire) the other piece is kept to yourself. This is why an eavesdropper who sees everything client and server send to eachother cannot recreate the keys.