Tutorial By :- Abhishek Pandey (abhishek@firmcodes.com)

Hello friends, you must have listen somewhere about “asymmetric key cryptography”. So let me explain you what each word says.

Cryptography refers to techniques used for secure communication between two people in presence of some third person who might steal our secret data and may use it in a wrong way.

So basically cryptography deals with encryption and decryption of data which could only be possible with some keys which sender and receiver both have their own.

So here, a question arises, how much secure is this cryptography. It all depends on key size. Greater the size of keys, greater is the encryption and harder to guess the keys.

Now if we discuss about asymmetric keys, these are a pair of keys that are used to encrypt(lock) or decrypt(unlock) the data. That different keys are called as private key (which is kept private) and public key(that can be given to anyone to decrypt the data.)

There is one very important concept about the asymmetric key encryption is that, data once encrypted with a private key can only be decrypted with its public key and vice versa is also true.

But you must be thinking that if I decrypted the data with private key then anyone can access that using my public key. Yes it’s 100 % true but if you backtrack this, you’ll find that the data which I decrypted using that public key, the sender of that data can be the only one person, who encrypted that data using the private key.

By this property of asymmetric key encryption, if someone encrypted the data using private key, in future he cannot deny that he encrypted that specific data.

Now if we go through the reverse way, means if we encrypted the data using public key, it can only be decrypted using the private key.

At this point of time, I think you must be very confused which public key and private key I’m talking about. So clearing that thing, let me tell you that any user can generate it’s private key, and from that private key, a public key is also generated. And whatever the encryption and decryption is performed, is with help of those pair of keys. You cannot do like encrypt data using your private key and decrypt it using your friend’s public key.

Now let’s see how to generate private and public key pair.

For this you must have a Linux based PC with openssl package installed in it.

Go to terminal and create a directory named ‘rsa’ or whatever you want.

Then type in command :

Above command will generate your private key in file name private.pem . Here 4096 refers to the no of bits of key you want to generate.

Output :-

Again type in command :

This command will generate your public key in file name public.pem

Output:-

Now we will use the same key files for encrypting the text files or any given file using a C program:

Now create a file that contains data to encrypt.

For example,

Create a file , encrypt.c

Compile using command –

Output : –

Now create another file,let say decrypt.c

Compile code using –

Run binary file –

Output –

RSA Length :512
Decrypted Text =Welcome to Firmcode
Decrypted Length =22

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~