## How Asymmetric Encryption Works

### What is Encryption

To ensure the message we want to send to someone gets there without being read on the way by unauthorized participants we are going to use encryption. In this article, we are going to cover the asymmetric encryption mechanisms. Also, this encryption is called public-key encryption or public-key cryptography.

From the beginning of human civilizations, people had a need to securely communicate. Before the modern encryption algorithms, we needed to go through a sort of security evolution.

From the Greeks and Spartans through Romans all the way to today's modern algorithms there were some important honorable mentions. Greeks used a messenger to send the message written on top of the messenger's head. That is an example of what we call today "stenography", not encryption because to hides the message, not changing its form.

Spartans used the tape wrapped around the stick, then write the message on it. When unwrapped the tape was useless unless wrapped around the stick with exact same dimensions.

Later on, the `Cezar's`

code was used when Romans used the shifting method - alphabet letters are shifted for X places against the original alphabet. Letter A becomes D, C becomes E, etc. This is an example if the encryption key was `shift by 3`

.

### Asymmetric Encryption Key

Unlike symmetric algorithms, the asymmetric algorithms are not using a single key for both, encryption, and decryption. With Asymmetric algorithms, the philosophy is a bit more complicated in the mathematics area. One key is used for encryption and a completely different key for decryption. There are ranges of math algorithms used for asymmetric encryption, most common and most recognized are the following:

- RSA Algorithm
- Diffie-Hellman Algorithm

**Asymmetric Algorithms Diagram:**

### RSA Algorithm

RSA was created by Ron **R**ivest, Ali **S**hamir, and Leonard **A**dleman whose names are engraved in the algorithm name. The basic idea relies on the prime numbers strength low probability of breaking the large prime numbers to its building blocks. However, due to the fact that this algorithm works with numbers so large, it is not good for data encryption. It is used mostly for digital signing and keys exchanging. Data encryption is usually performed by much faster algorithms like DES.

The math behind it is as follows:

- First, we generate the
*p*and*q*numbers. These must be different. - Then we calculate n = p * q
- Next we calculate t = (p - 1) * (q - 1)
- Then we choose number
*e*wth a rule it's between 1 and*n* - Next, we calculate d where it satisfies the following equation: d * e ≡ mod n
- Finally, we have a private key (
*n*,*d*) and public key (*n*,*e*)

How encryption works:

**Person A** has a key we have calculated above and they sent the public key to **Person B** with whom they want to exchange secure messages. Now **Person B** takes the message *m* and encrypts it like this:

*c *=* m*^*e *mod* n*

*c* is the ciphertext that **Person B** sends to **Person A**. Person A can now decrypt this message like this:

*m* = *c*^*d* mod* n*

*m* is now plain text decrypted message that they can read.

### Diffie-Hellman Algorithm

Whitfield Diffie and Martin Hellman have created the algorithm based on the specific math problem - discrete logarithm problem. This algorithm is used for keys exchange between parties in encrypted communication. Below is the simple math showing the algorithm logic:

Persons **A** and **B** are to generate the key for secure communication so they start with defining the two prime numbers (*g* and *n*). First, they agree on using *n* and *g*, so that `G={0, 1, ..., n-1}`

cyclic multiplicative group and its generator is the number *g*. These two are not a secret so, Persons A and B can agree on these numbers even over unsecured channels.

**Person A**chooses a random number*A*and generates the*X*to be sent to person B:*X*= g^*A*mod n**Person B**chooses a random number*B*and generates*Y*to be sent to Person A:*Y*= g^*B*mod n**Person A**calculates the*key-A*: key-A =*Y*^*A*mod n**Person B**calculates the*key-B*: key-B =*X*^*B*mod n

*key-A* and *key-B* are the same and can be used for signing messages between **Person A** and **Person B**.

**Recommendation**: Advanced Cryptography book is highly recommended for researching this field. It contains all of the modern cryptography algorithms with the math behind each.

Related article: How Symmetric Encryption Works