In this post, we will ‘**How are the public and private keys generated?**‘ Here you will learn about asymmetric cryptography. Besides this, you will also learn about the ways of encryption and decryption of a text file in different ways by using public and private keys along with, the usage of private keys with the passphrase and without the passphrase. But first of all, we must basic info about asymmetric cryptography. As you want to become a cyberman so you must have full knowledge of cryptography. Because it is the most important requirement of cybersecurity. It is also essential for the purpose of security. So we must have proper knowledge about cryptography.

Let’s learn more about Asymmetric Cryptography –

### What is Asymmetric Cryptography?

* Public-key cryptography, or asymmetric cryptography, is a cryptographic system that uses pairs of keys: public keys which may be disseminated widely, and private keys which are known only to the owner. The generation of such keys depends on cryptographic algorithms based on mathematical problems to produce one-way functions. Effective security only requires keeping the private key private; the public key can be openly distributed without compromising security. *says Wikipedia

Perhaps you will not be familiar with Wikipedia terms, so I will explain the Asymmetric Cryptography in very simple terms. So basically Asumatric Cryptography is a form of encryption which uses a pair of keys i.e. Private key and Public key. The public key is most reliable for the encryption purpose because of its distribution and authentication. It uses mathematical value rather than substitution and permutation like symmetric encryption. For the encryption view, the private key is most important because it is the head of Asymmetric encryption. We can create the public key with the help of the private key. But once, if private lost or forgotten then it can’t be recovered.

### How to Generate Public key or Private Key?

The building of public key and private key depends upon the algorithm type. Because Asymmetric Cryptography uses many algorithms like RSA, DSA, DES, AES, ECC etc. So whenever we create an encrypted file then we use one of these algorithms.

### What is the use of private and Public keys in encryption?

In Asymmetric Encryption, both private and public keys are important because one key is used for encryption while another used for decryption. If somehow any of this lost or forgotten then it becomes difficult to recover encrypted data. A public key is used for encryption and the private key is used for decryption. The public key is less important than Private because a public key can be generated through a private key. So Private is important because it is used for the creation of public-key as well to decrypt the file data.

### Terms and conditions to Encrypt a file?

- Plain Text.
- The text must be in a readable form.
- The text simply fed into algorithm encryption.

### Why we need to protect Private Key?

It is very important to keep the private key very secure. As we know we can create the public key with the help of private key and is also used to decrypt any encrypted file. So if somehow someone thefts the private key then it can create a security issue. It can lead to a heavy loss of data. For security purpose, we must have a secured private key. So through the RSA algorithm, we can protect both to our data and to the private key as well.

Above it was the theoretical concept of Asymmetric Cryptography or Encryption. It can increase your knowledge about encryption but unless you will not have a practical skill to perform it then it’s like garbage for you. So here I shall also share the practice of the Asymmetric Encryption in a Linux system. I have performed it on RHEL 5.

Below are the commands through which you can Asymmetric Encryption in very simple terms. So follow each and every step very carefully.

**RSA Algorithm :**

**Encryption of the file without Passphrases**

## Step 1. Create a File with Vim and write your content in it

[ root@rdsaini ~]#vim deepak.txt

Here, deepak.txt is a text file containing some valuable data.

## Step 2. How to create a private key?

[ root@rdsaini ~]#openssl genrsa -out privatekey.pem 1024

Here, (i) openssl is the command.

(ii). -out stands for output and 1024 is encryption bit level.

(iii). private.pem is name given to the private key. You can choose as of your choice.

## Step 3.How to create a Public key using a private key?

[ root@rdsaini ~]#openssl rsa -in privatekey.pem -out publickey.pem -outform PEM -pubout

Here (i) -in for input the name of private key. In this case, it is privatekey.pem

(ii) -out for name shall be given to your generated public key. In this case, it is publickey.pem

## Step 4. Now encrypt the file using Public Key.

[ root@rdsaini ~]#openssl rsautl -encrypt -inkey publickey.pem -pubin -in deepak.txt -out encryptfile.txt

Here, (i). -inkey for input key which will our public key. Here it is public.pem

(ii). -in for the input file which you want to encrypt.

(iii). -out = give a name to output file. Here it is encryptfile.pem

## Step 5. Now decrypt the encrypted file with Private Key.

[ root@rdsaini ~]#openssl rsautl -decrypt -inkey privatekey.pem -pubin -in encryptfile.txt -out decryptedfile.txt

Here, (i). -inkey for input key which will our private key. Here it is privatekey.pem

(ii). -in for the input file which will be our encrypted file.

(iii). -out = give a name to output file. Here it is decryptedfile.pem

**Encryption of the file with Passphrases**

## Step 1. Create a private key with a passphrase.

[ root@rdsaini ~]#openssl genrsa -des3 -out private.pem 1024 Generating RSA private key, 1024 bit long modulus ...++++++ ....++++++ e is 65537 (0x10001) Enter pass phrase for private.pem:Your PassphraseVerifying - Enter pass phrase for private.pem:Re-Enter passphrase

## Step 2. How to decrypt a passphrased private key.

[ root@rdsaini ~]#openssl rsa -in private.pem -out decryptprivatekey.pemEnter pass phrase for private.pem:Your PassphraseWriting RSA key

# DSA Algorithm :

**Encryption of the file without Passphrases**

## Step 1. Create a File with Vim and write your content in it

[ root@rdsaini ~]#vim deepak.txt

## Step 2. First, set the parameter to generate the private key?

[ root@rdsaini ~]#openssl dsaparam -out dsaparam.pem 2048Generating DSA parameters,2048 bit long prime This could take some time

## Step 3. How to create a private key?

[ root@rdsaini ~]#openssl gendsa -out deepak.pem dsaparam.pemGenerating DSA key, 2048

## Step 4. How to create a public key using private key?

[ root@rdsaini ~]#openssl dsa -in deepak.pem -outform PEM -pubout -out publickey.pemread DSA key writing DSA key

In this post, we have learnt How are the public and private keys Generated, What is Asymmetric Cryptography, What is the use of private and Public keys in encryption? Guys if you have doubt or any query with this post then don’t hesitate to contact me. And if you have any suggestion for me then please write to me the comment box. If you like my posts please share it with your friends.