Latest / CyberCode Academy / Course 17 - Computer Network Security Protocols And Techniques | Episode 4: Asymmetric Cryptography: RSA, Diffie-Hellman
Transcript
- 0:00Speaker 1: How do you send a secret across a completely public,
- 0:03Speaker 1: unsecured space. That's the fundamental challenge that underpins pretty much
- 0:08Speaker 1: all of modern digital security.
- 0:10Speaker 2: It really is every text message, every secure website you visit,
- 0:14Speaker 2: every transaction, it's all traveling across networks that are well
- 0:18Speaker 2: inherently open. Anyone could be listening.
- 0:21Speaker 1: It seems like a paradox, right, we expect total privacy,
- 0:24Speaker 1: but we're using this wide open public infrastructure.
- 0:27Speaker 2: It is a paradox. And the solution lies in mathematics,
- 0:31Speaker 2: specifically in a concept called asymmetric key cryptography.
- 0:35Speaker 1: The idea that you can use two different but linked keys,
- 0:39Speaker 1: one public, one private, to make sure only the right
- 0:41Speaker 1: person can read the message exactly.
- 0:44Speaker 2: And today we're doing a deep dive into the two
- 0:46Speaker 2: algorithms that really started it all, RSA for securing data
- 0:50Speaker 2: and Diffy Hellman for exchanging the keys in the first place.
- 0:53Speaker 1: There's sort of the twin engines of your online safety.
- 0:55Speaker 2: Absolutely, and before we get into the nitty gritty, there's
- 0:58Speaker 2: one one key concept we have to establish, okay, in encryption,
- 1:02Speaker 2: when we talk about a message, whether it's an email
- 1:04Speaker 2: or a picture, we're always always talking about a number.
- 1:07Speaker 1: Right, All data is just bits, and that string of
- 1:10Speaker 1: bits can be read as one single gigantic integer colossal integer.
- 1:15Speaker 2: So encrypting a message just means doing a mathematical transformation
- 1:19Speaker 2: on that specific number.
- 1:20Speaker 1: Got it. So when we say message M, just picture
- 1:23Speaker 1: an unimaginably large number. So with that in mind, where
- 1:26Speaker 1: should we start RSSA.
- 1:28Speaker 2: Let's start with RSA. It's named after its inventors, Revist
- 1:32Speaker 2: Shmir and Adlman, and it's been a cornerstone of secure
- 1:35Speaker 2: communication for decades.
- 1:37Speaker 1: And its power is all in that dual key approach
- 1:40Speaker 1: you mentioned it is.
- 1:41Speaker 2: So let's say Bob wants to receive secure messages from Alice.
- 1:44Speaker 2: Bob needs to generate a keypair.
- 1:46Speaker 1: A public one and a private one.
- 1:47Speaker 2: Exactly. He generates a public key, which you can give
- 1:50Speaker 2: to Alice or post online for anyone to use, and
- 1:53Speaker 2: then he generates a private key, which he keeps completely secret.
- 1:57Speaker 1: The public key locks the box, and only his price
- 2:00Speaker 1: that key can unlock it.
- 2:01Speaker 2: That's the idea. The public key encrypts the private key.
- 2:04Speaker 1: Dcryps I get the roles. But how does Bob just
- 2:07Speaker 1: conjure these up? How does the math actually work to
- 2:10Speaker 1: create two keys that are perfect inverses of each other.
- 2:13Speaker 2: It's a really clever process built on one simple, very
- 2:16Speaker 2: hard mathematical problem. It all starts with Bob choosing two
- 2:21Speaker 2: extremely large prime numbers. Let's call them P and Q.
- 2:24Speaker 1: And when you say large, we're not talking about like
- 2:27Speaker 1: ninety seven.
- 2:28Speaker 2: No, No, we're talking thousands of digits long. In modern security,
- 2:32Speaker 2: each one is around one twenty four bits. They are massive,
- 2:36Speaker 2: and they must be kept secret.
- 2:37Speaker 1: Okay, so P and Q are the secret ingredients. What's next.
- 2:40Speaker 2: Bob multiplies them together to get a new number N.
- 2:43Speaker 2: So N equals P times Q. This N the product
- 2:47Speaker 2: is actually public, so.
- 2:48Speaker 1: You can tell the world what N is, but not
- 2:50Speaker 1: the P and Q that.
- 2:51Speaker 2: Made it precisely, and that's the whole secret. Multiplying P
- 2:54Speaker 2: and Q is instant for a computer. But if you
- 2:56Speaker 2: only have N, try and figure out which two primes
- 2:59Speaker 2: multiplied to create. That's called factoring, and for numbers this big,
- 3:02Speaker 2: it's practically impossible.
- 3:03Speaker 1: So that difficulty of factoring is the lock on the
- 3:06Speaker 1: whole system.
- 3:06Speaker 2: It's the entire foundation. Now Bob needs a couple more
- 3:09Speaker 2: numbers to finish his keys. He calculates a secret helper
- 3:12Speaker 2: number Z using his secret P and Q.
- 3:14Speaker 1: We don't need the exact formula, but what is Z
- 3:16Speaker 1: four Z is.
- 3:17Speaker 2: The mathematical bridge between the public and private keys. It
- 3:21Speaker 2: helps create the inverse relationship. With Z. He can pick
- 3:24Speaker 2: a public number E, which is the public exponent.
- 3:27Speaker 1: And then use Z to find the matching private one.
- 3:30Speaker 2: Exactly, he calculates the private exponent D, so that E
- 3:34Speaker 2: and D have a special inverse relationship that depends entirely
- 3:37Speaker 2: on that secret Z. If you don't know Z, you
- 3:40Speaker 2: cannot find D.
- 3:41Speaker 1: So to recap Bob's public key is the pair of
- 3:44Speaker 1: numbers N E, and his private key is n D.
- 3:48Speaker 1: The whole thing is secure because D can only be
- 3:50Speaker 1: calculated if you know the original secret primes P and Q.
- 3:54Speaker 2: You've got it. That's the whole setup.
- 3:55Speaker 1: Okay, So Alice has Bob's public key. She wants to
- 3:58Speaker 1: send him the message M. How does she use N
- 4:01Speaker 1: and E.
- 4:02Speaker 2: The operation itself is actually quite elegant. She takes her
- 4:04Speaker 2: message number M, raises it to the power of E,
- 4:07Speaker 2: and then she divides by N and takes the remainder.
- 4:10Speaker 1: That mod n operation right.
- 4:12Speaker 2: That remainder is the cipher test. That's the encrypted message
- 4:14Speaker 2: C It looks like total gibberish.
- 4:16Speaker 1: And when Bob gets this gibberish number C, he just
- 4:19Speaker 1: does the reverse. With his private key.
- 4:21Speaker 2: He performs the exact same kind of operation, but with
- 4:24Speaker 2: his secret exponent D. He takes c raises it to
- 4:27Speaker 2: the power of d and finds the remainder after dividing
- 4:30Speaker 2: by N.
- 4:30Speaker 1: And the math just magically works out to be the
- 4:32Speaker 1: original message M.
- 4:33Speaker 2: The properties of modular arithmetic guarantee it it perfectly reverses
- 4:37Speaker 2: the encryption.
- 4:38Speaker 1: Let's try a tiny example just to see it in action. Obviously,
- 4:41Speaker 1: the real numbers are huge, but say Bob chose tiny
- 4:44Speaker 1: primes like five and seven.
- 4:45Speaker 2: Okay, so P five and Q seven. That means the
- 4:47Speaker 2: public number N would be thirty five.
- 4:50Speaker 1: And let's say he chose a public exponent E of
- 4:52Speaker 1: five and then calculated the private one D, which would
- 4:56Speaker 1: turn out to be twenty nine.
- 4:57Speaker 2: Right, So public key is thirty five five, private key
- 5:00Speaker 2: is thirty five twenty nine.
- 5:02Speaker 1: Now, if Alice wants to send the message, say the
- 5:04Speaker 1: number twelve, she'd calculate twelve to the power of five
- 5:07Speaker 1: divided by thirty five and find the.
- 5:08Speaker 2: Remainder, which comes out to seventeen. So she sends the
- 5:11Speaker 2: number seventeen.
- 5:11Speaker 1: Bob receives seventeen. He then calculates seventeen to the power
- 5:15Speaker 1: of his private D, which is twenty nine, finds the
- 5:18Speaker 1: remainder after dividing by thirty five, and.
- 5:20Speaker 2: Out pops the number twelve the original message. It works perfectly.
- 5:24Speaker 1: The numbers don't really matter, but the principle is incredible.
- 5:27Speaker 1: The public key does a specific scramble, and the private
- 5:30Speaker 1: key is the only thing in the universe that knows
- 5:32Speaker 1: how to unscramble it.
- 5:33Speaker 2: And it's all secure because of that factoring problem we've
- 5:36Speaker 2: talked about. An attacker sees and an E. To find
- 5:39Speaker 2: the private key D, they need the secret number Z.
- 5:43Speaker 1: And to find Z they need to know the original
- 5:45Speaker 1: primes P and.
- 5:46Speaker 2: Q, which means they have to factor N and for
- 5:49Speaker 2: a two thousand, forty eight bit number, which could be
- 5:52Speaker 2: over six hundred digits long, you'd need, you know, thousands
- 5:56Speaker 2: of years.
- 5:57Speaker 1: With the fastest supercomputers we have today, it's just computationally infeasible.
- 6:01Speaker 2: That difficulty is the bedrock. As long as factoring large
- 6:04Speaker 2: numbers is hard, RSA is secure.
- 6:06Speaker 1: Okay, that makes perfect sense, but let's talk practicalities. If
- 6:09Speaker 1: RSA is the secure, why don't we just use it
- 6:11Speaker 1: for everything? Why not encrypt a whole movie file with it?
- 6:14Speaker 2: The answer is simple, it's incredibly slow, slow, painfully slow.
- 6:19Speaker 2: Doing those calculations with massive exponents. Raising a six hundred
- 6:22Speaker 2: digit number to the power of another six hundred digit
- 6:24Speaker 2: number takes a lot of processing power. It's about one
- 6:27Speaker 2: hundred times slower than other methods.
- 6:29Speaker 1: Right. That would be a huge bottleneck for big data transfers.
- 6:32Speaker 1: So what's the workaround? How is it actually used in
- 6:35Speaker 1: the real world.
- 6:36Speaker 2: It's used as part of what you could call cryptographic teamwork.
- 6:40Speaker 2: RSA isn't used to encrypt your whole multi gigabyte download.
- 6:43Speaker 1: What is it used for?
- 6:44Speaker 2: Then? It's used to encrypt one tiny but very important
- 6:48Speaker 2: piece of data, a session key.
- 6:51Speaker 1: A session key, right, it's a key for.
- 6:53Speaker 2: A different kind of encryption, symmetric encryption, like as symmetric
- 6:57Speaker 2: algorithms are blazing fast, but have their own problem. You
- 7:00Speaker 2: the other person need to have the same key.
- 7:02Speaker 1: Ah, So you can't just send that key over the
- 7:04Speaker 1: open Internet.
- 7:05Speaker 2: Not without encryption. So Alison Bob used the slow super
- 7:08Speaker 2: secure RSA just to wrap up and safely exchange a
- 7:11Speaker 2: small shared session key.
- 7:13Speaker 1: Okay, So once they both have that shared key, they
- 7:16Speaker 1: just drop RSA entirely for the rest of the conversation.
- 7:19Speaker 2: Exactly, they switch over to a fast symmetric algorithm like
- 7:22Speaker 2: AES for the actual data. RSA does the secure handshake
- 7:26Speaker 2: and as does the heavy lifting, the best of both
- 7:28Speaker 2: worlds security and speed.
- 7:30Speaker 1: That idea of the secure handshake is a perfect transition
- 7:33Speaker 1: to our second algorithm, Diffy Hellman.
- 7:36Speaker 2: Right. If RSA is about securely wrapping and sending a key,
- 7:40Speaker 2: Diffy Hellman is about securely generating a key together out
- 7:43Speaker 2: of thin air.
- 7:44Speaker 1: And its advantage is that Alis and Bob don't need
- 7:46Speaker 1: any prior secret. They can create a shared secret over
- 7:49Speaker 1: a completely public channel.
- 7:51Speaker 2: It's a beautiful piece of mathematics. It solves the key
- 7:53Speaker 2: agreement problem.
- 7:54Speaker 1: So how do they pull off this track?
- 7:56Speaker 2: Well, again, it starts with public agreement. Alis and Bob
- 7:59Speaker 2: publicly agree on two numbers, a prime P and a
- 8:02Speaker 2: base G. Everyone can know these P.
- 8:04Speaker 1: ANDNG or public. Then they each bring their own secret
- 8:06Speaker 1: to the party exactly.
- 8:08Speaker 2: Alice secretly chooses a number let's call it X. Bob
- 8:11Speaker 2: secretly chooses a number y. These are never.
- 8:15Speaker 1: Ever shared got it X and Y stay private.
- 8:17Speaker 2: Now. Alice takes the public number G, raises it to
- 8:21Speaker 2: the power of her secret X, and calculates the remainder
- 8:24Speaker 2: modulo P. She sends that result R one to Bob.
- 8:28Speaker 1: Publicly, and Bob does the same thing with his secret Y.
- 8:31Speaker 2: He does the parallel calculation G to the power of
- 8:34Speaker 2: his secret Y modulo P. He gets a result R
- 8:38Speaker 2: two and sends that publicly to Alice.
- 8:41Speaker 1: So the whole world sees p G R one and
- 8:44Speaker 1: R two, but not X or Y.
- 8:47Speaker 2: Correct. Now for the magic Alice takes the number Bob
- 8:51Speaker 2: sent her R two and raises it to the power
- 8:54Speaker 2: of her own secret number X. Okay, And Bob takes
- 8:57Speaker 2: the number Alice sent him R one and raises it
- 9:00Speaker 2: to the power of his secret number.
- 9:01Speaker 1: Why and they get the same number.
- 9:03Speaker 2: The math guarantees they arrive at the exact same result,
- 9:06Speaker 2: the identical shared secret key.
- 9:08Speaker 1: Okay, that's a bit hard to visualize. The paint mixing
- 9:11Speaker 1: analogy is often used here, right, Yes.
- 9:13Speaker 2: It's perfect for this.
- 9:13Speaker 1: Yeah.
- 9:14Speaker 2: Let's say the public number G is a base color
- 9:16Speaker 2: of paint yellow.
- 9:17Speaker 1: Okay, public yellow paint.
- 9:19Speaker 2: Alice's secret number X is her secret color, say red.
- 9:22Speaker 2: She mixes the public yellow with her secret red, and she.
- 9:24Speaker 1: Gets orange R one. She sends that out in the
- 9:27Speaker 1: open YEP.
- 9:27Speaker 2: Now Bob's secret number, why is his secret color blue?
- 9:31Speaker 2: He mixes the public yellow with his secret blue.
- 9:34Speaker 1: He gets green R two and sends that to Alice.
- 9:36Speaker 2: Exactly so. Now Alice has Bob's public green paint, she
- 9:39Speaker 2: adds her own secret color to it.
- 9:41Speaker 1: She adds her secret red to the green and gets brown.
- 9:44Speaker 2: And Bob has Alice's public orange paint. He adds his
- 9:47Speaker 2: secret color to it.
- 9:48Speaker 1: He adds his secret blue to the orange. Yeah, and
- 9:51Speaker 1: also gets brown.
- 9:52Speaker 2: They both end up with the exact same secret color brown,
- 9:56Speaker 2: without ever revealing their private colors red and blue.
- 9:59Speaker 1: That's a fantastic Toch analogy. The math of exponents does
- 10:01Speaker 1: the same thing as mixing the paint.
- 10:03Speaker 2: It does, yeah, and it's a wonderfully clean and fast
- 10:06Speaker 2: way to generate that shared key K, which they can
- 10:09Speaker 2: then use for symmetric encryption.
- 10:11Speaker 1: It sounds too good to be true. There has to
- 10:13Speaker 1: be a catch. The process seems vulnerable to a man
- 10:16Speaker 1: in the middle attack.
- 10:17Speaker 2: There is a catch, and you're spot on. Divvy hillman
- 10:20Speaker 2: is a key generation mechanism, not an authentication mechanism. It
- 10:24Speaker 2: has no idea who's talking to.
- 10:26Speaker 1: This is where our eves dropper, Eve comes in.
- 10:28Speaker 2: This is Eve's moment to shine. Alice thinks she's talking
- 10:31Speaker 2: to Bob, but Eve can get right in the middle.
- 10:33Speaker 1: How does that work? How does Eve trick them both?
- 10:36Speaker 2: So when Alice sends her public result R one, intending
- 10:39Speaker 2: it for Bob, Eve intercepts it. She doesn't let it
- 10:42Speaker 2: go through. Instead, Eve generates her own secret number, let's
- 10:46Speaker 2: call it Z. She does the diffy Hillman math with
- 10:49Speaker 2: Alice's R one and sends a new result back to Alice,
- 10:53Speaker 2: pretending to be Bob.
- 10:54Speaker 1: Oh wow. So Alice completes the key exchange, but she's
- 10:57Speaker 1: actually creating a shared secret key K one.
- 11:00Speaker 2: With Eve, a shared secret between Alice and Eve. Alice
- 11:03Speaker 2: is completely unaware. She thinks she has a secure channel.
- 11:06Speaker 1: To Bob, and Eve does the same thing in the
- 11:08Speaker 1: other direction.
- 11:09Speaker 2: Simultaneously. She takes the R one she intercepted from Alice
- 11:12Speaker 2: and does a similar exchange with Bob, pretending to be Alice.
- 11:16Speaker 2: Bob ends up creating a shared secret key K two
- 11:19Speaker 2: with Eve.
- 11:20Speaker 1: So Eve has two separate secure channels one with Alice
- 11:23Speaker 1: using KEYK one and one with Bob using KYK two.
- 11:26Speaker 2: The devastation is complete. She sits in the middle. Alice
- 11:29Speaker 2: sends a message, encrypt it with K one. Eve decrypts it,
- 11:32Speaker 2: reads it, maybe even changes it, then re encrypts it
- 11:34Speaker 2: with K two and sends it on to Bob.
- 11:36Speaker 1: And neither Alice nor Bob have any idea their entire
- 11:39Speaker 1: conversation as being monitored, none at all.
- 11:41Speaker 2: This vulnerability really highlights the need for something more. You
- 11:44Speaker 2: can't just rely on key generation. You need endpoint authentication.
- 11:48Speaker 1: Alice needs to be sure that the person she's doing
- 11:50Speaker 1: the paint mixing with is actually Bob.
- 11:53Speaker 2: That's the critical lesson, and it's why in the real world,
- 11:56Speaker 2: like with the tlssso that secures your web browser, diffy Home,
- 12:00Speaker 2: is never used alone. You have to verify identity.
- 12:02Speaker 1: First, and that verification often comes back to digital certificates,
- 12:07Speaker 1: which are often signed using RSA.
- 12:09Speaker 2: Often, yes, RSA can be used to sign a certificate
- 12:13Speaker 2: proving that the key you just received really came from say,
- 12:16Speaker 2: your bank, and not from Eve. It stops the man
- 12:20Speaker 2: of the middle attack before it can even start.
- 12:21Speaker 1: It's all about that cryptographic teamwork. Each algorithm has a
- 12:24Speaker 1: specific job.
- 12:25Speaker 2: It really is.
- 12:26Speaker 1: So to wrap up for everyone listening, if you want
- 12:28Speaker 1: to understand how the secure internet works, these are the
- 12:31Speaker 1: two pillars. You have RSA providing a secure foundation based
- 12:34Speaker 1: on the difficulty of factoring, mostly used to securely exchange
- 12:38Speaker 1: a session key.
- 12:39Speaker 2: And you have Diffie Hellman, which gives us that beautiful
- 12:42Speaker 2: mathematical trick for generating a shared key directly as long
- 12:46Speaker 2: as we protect it with proper authentication to stop that
- 12:48Speaker 2: man in the middle attack.
- 12:50Speaker 1: Together, they really do form the backbone of the secure
- 12:52Speaker 1: communication we all rely on every single.
- 12:55Speaker 2: Day, and for anyone studying this, understanding why these systems
- 12:59Speaker 2: are paired to get is just as important as knowing
- 13:01Speaker 2: how they work individually.
- 13:03Speaker 1: Definitely, now here's a final thought for you to take away,
- 13:06Speaker 1: something to chew on the entire security of RSA. Hinges
- 13:10Speaker 1: on one fact that factoring a massive number n back
- 13:15Speaker 1: into its original primes Q and q is impossibly hard
- 13:19Speaker 1: for our current computers.
- 13:20Speaker 2: What if it wasn't. There's a lot of research into
- 13:23Speaker 2: quantum computers, which work on completely different principles. If a
- 13:26Speaker 2: powerful quantum computer could solve this factoring problem, almost instantly.
- 13:31Speaker 2: What does that imply about well, basically all of our
- 13:34Speaker 2: currently secure communications, and.
- 13:35Speaker 1: More importantly, what kind of fundamental mathematical problem could we
- 13:39Speaker 1: use to replace it. What's the next impossibly hard problem
- 13:42Speaker 1: that will become the bedrock of our future security? Something
- 13:46Speaker 1: to think about next time you see that little padlock
- 13:48Speaker 1: icon in your browser.