The simple mathematics behind public key cryptography
Original version belong to this story appeared in Quanta Magazine.
For thousands of years, if you wanted to send a secret message, there was basically one way to do it. You will shuffle the message using a special rule that only you and your target audience know. This rule works like the key to a lock. If you have the key, you can decrypt the message; otherwise, you need to select the lock. Some locks so efficient, they could never be chosen, even with unlimited time and resources. But even those plans suffer from the same Achilles’ heel that plagues all such encryption systems: How do you get that key into the right hands while keeping it out of the wrong hands? bad?
Counterintuitive solution, called public key cryptographydoes not rely on keeping the key secret but on making it widely available. The trick is to use a second key that you never share with anyone, not even the person you’re communicating with. Only by using a combination of two keys—one public, one private—can someone scramble and decrypt the message.
To understand how this works, it is easier to think of the “key” not as an object inserted into the lock but as two additional components in an invisible ink. The first component makes the messages disappear and the second component makes them reappear. If a spy named Boris wanted to send his counterpart Natasha a secret message, he would write one and then use the first element to make it invisible on the page. (This was easy for him to do: Natasha had published a popular and easy-to-follow recipe for making ink disappear.) When Natasha received the paper in the mail, she applied the second ingredient. two caused Boris’s message to reappear.
In this plan, anyone can hide messages, but only Natasha can show them again. And because she never shared the recipe for the second ingredient with anyone—not even Boris—she could be sure that the message hadn’t been decoded along the way. When Boris wants to receive secret messages, he simply applies the same process: He publishes an easy formula to make messages disappear (which Natasha or anyone else can use), At the same time, keep another recipe for yourself to make them appear again.
In public key cryptography, the “public” key and the “private” key work like the first and second components in this special invisible ink: One component encrypts the message, the other decode them. But instead of using chemicals, public key cryptography uses mathematical puzzles called hatch function. These functions are easy to calculate in one direction and extremely difficult to invert. But they also contain “trap doors,” pieces of information that, if known, would make calculating functions in both directions trivially easy.
A popular trapdoor function involves multiplying two large prime numbers, an operation that is easy to perform. But inverting it—that is, starting from the product and finding each prime factor—is computationally impractical. To generate a public key, start with two large prime numbers. These are your traps. Multiply the two numbers together and then do some addition math operations. This public key can now encrypt messages. To decrypt them, you will need the corresponding private key, which contains prime factors—the necessary trapdoors. With those numbers, it’s easy to decode the message. Keep those two prime factors secret and the message will be kept secret.