RSA Algorithm With C#

We sometimes need to hide our confidential data from other users. For that purpose we use encryption algorithms to encrypt our data. There are very many encryption algorithms but I am describing the Rivest, Shamir, Adleman (RSA) Algorithm.

About RSA

RSA is an encryption algorithm.

Developed in: 1977.

Developed by: Ron Rivest, Adi Shamir, and Leonard Adleman.

The RSA algorithm is the most commonly used public key encryption algorithm.

Public Key Encryption

It is also known as asymmetric cryptography.

Two keys are used: Public Key and Private Key.

Public Key: For encryption.

Private Key: For decryption, also known as a secret key.

public key encryption.png

The preceding diagram show how public key encryption works,

In the diagram 2 users are shown:

First: The Sender (who is sending something by the recipient's Public Key)

Second: The Receiver (who is receiving something from the sender using the private key)

So in a public key cryptosystem, the sender encrypts the data using the public key of the receiver and uses an encryption algorithm that is also decided by the receiver and the receiver sends only the encryption algorithm and public key. 

But by using the public key, data can only be encrypted but not decrypted, and the data is only decrypted by the private key that only the receiver has. So no one can hack our data.

In simple terms:

Public Key: Shared with the public that wants to send us data.

Private Key: Kept secret so that when someone sends us data encrypted by our Public Key, we can decrypt the data using the Private Key.

HOW RSA WORKS

Both users (sender and receiver) generates a public and private key.

The following is the procedure for generating a public and private key (see flowchart).

Generation of Public and Private key in RSA

flow chart RSA.png

The flowcharts above shows how to generate a public and private key using RSA.

After getting the public and private key the main thing is how to encrypt and decrypt using RSA.

Encryption and Decryption in RSA

encrypt and decrypt.png

Example of RSA: Here is an example of RSA encryption and decryption with generation of the public and private key.

Generate public and private key:

generation Example.png

Encryption and Decryption

encrypt and decrypt Example.png

How to use the RSA Algorithm in a C# Windows Forms application

1. Open Visual Studio.

2. Select "File" -> "New" -> "Project..." or press "Ctrl +Shift +N".

3. Now select "Windows Forms application" from the Visual C# templates.

4. Now design the Windows Forms form such as follows:

Form.PNG

You need to make:

3 TextBoxes for Plain Text, Encrypted Text and Decrypted Text

and 2 Buttons.

Now for the coding part.

Coding

1. To use the RSA algorithm in C#, we need to add the following namespace:

using System.Security.Cryptography;

2. Now make a function for Encryption.

static public byte[] Encryption(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
{
 try
 {
 byte[] encryptedData;
 using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
   {
    RSA.ImportParameters(RSAKey);
           encryptedData = RSA.Encrypt(Data, DoOAEPPadding);
   }   return encryptedData;
 }
 catch (CryptographicException e)
 {
 Console.WriteLine(e.Message);
 return null;
 }
}					

3. Now make a function for Decryption
 

static public byte[] Decryption(byte[]Data, RSAParameters RSAKey, bool DoOAEPPadding)
{
 try
 {
 byte[] decryptedData;
 using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
    {
     RSA.ImportParameters(RSAKey);
     decryptedData = RSA.Decrypt(Data, DoOAEPPadding);
    }
 return decryptedData;
 }
 catch (CryptographicException e)
 {
 Console.WriteLine(e.ToString());
 return null;
 }        
}

4. Now make some variables into the class that are:
 

UnicodeEncoding ByteConverter = new UnicodeEncoding();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
byte[] plaintext;
byte[] encryptedtext; 

5. Now handle the Click Event for the Encrypt Button with the following code:
 

private void button1_Click(object sender, EventArgs e)
{
plaintext = ByteConverter.GetBytes(txtplain.Text);
encryptedtext = Encryption(plaintext, RSA.ExportParameters(false), false);
txtencrypt.Text = ByteConverter.GetString(encryptedtext);
}

6. Now handle the Click Event for the Decrypt Button with the following code:
 

private void button2_Click(object sender, EventArgs e)
{
byte[] decryptedtex = Decryption(encryptedtext, RSA.ExportParameters(true), false);
txtdecrypt.Text = ByteConverter.GetString(decryptedtex);
}
 

Results

Plain Text:

plaintext result.PNG

Encrypted Text:

EncryptedText.PNG

Decrypted Text:

Decrypted Result.PNG

Up Next
    Ebook Download
    View all
    Learn
    View all
    sourabhsomani.com