How does public key encryption work? C# Example Simplified

This article will explain how does public key encryption work. Public key systems underlay the security that you use every day to browse the web. The https (TLS) secure web links you use every day use public key cryptography to be able to exchange encrypted information.

From the book Essential Software Development Career + Technical Guide.

This article intends to show you a simplified example of how the keys are created and how encryption and decryption are done at a basic level.

Traditional symmetric encryption requires a shared key that both parties that need to communicate know. If, let’s say, we took a stupid simple cipher of adding 1 to everything, then the person on the receiving side would need to know to subtract 1. That means 1 is your shared secret.

To share that private secret, you needed to give the other person that number securely, either in person or via some secure, trusted communication channel that you know would not be intercepted.

Public key works differently. There is no shared secret, which means two people don’t need to meet and don’t initially need a secure communication channel to exchange a shared secret. For each user, there is a private key used to decrypt things encrypted with that user’s public key.

So, in this case, public keys can be posted for anyone to use, and the only person who can read messages encrypted with the public key is the person who holds the private key.

This has been an incredible leap for secure communications, allowing the setup of a secure way to communicate with anyone without the need to share a private secret.

Luckily, math came to the rescue to create this for us. It’s really not too complicated for a lot of folks to understand.

Example code below shows the relatively basic math that underlies public key systems that form the foundation of web security.

WARNING: To simplify the code to make it easier to understand, less than optimal algorithms were chosen, so this won’t work for a large prime. Also, for secure encryption, additional things are done to make the encryption harder to crack. This sample is just to show the core logic principles that make public/private key encryption work. Do not actually use this code as is for any encryption.

//IsPrime(n) is from Wikipedia (Primality Test—Wikipedia, n.d.) https://en.wikipedia.org/wiki/Primality_test

Thanks to this website (One Big Fluke › Simplest Explanation of the Math behind Public Key Cryptography, n.d.) ( https://www.onebigfluke.com/2013/11/public-key-crypto-math-explained.html) for explaining the math.

Don’t use this code anywhere near production. It was just a quick and dirty way to demonstrate at a low level the basic math/variables needed for public key. It doesn’t use cryptographic random number generation or other items.

Also note that with low prime numbers like shown here in testing, overlap and have issues with encrypting values in the message were higher than those components.

Here is a simplified example of using more realistic APIs to do the encryption and decryption without worrying about the underlying math: (Note does not do proper dispose handling or store the key securely, so don’t copy this code for actual use either, provided as a simplified demonstration. This portion was not included in the book)

This should give you a simplified understanding of how does public key encryption work with the math, and an understanding of usage.

Reference:

Essential Software Development Career + Technical Guide (Check out the book on Amazon)

Checkout our home page here for even more info.

(c)opyright 2023 Appjungle.net LLC