RSA Algorithm With C#

This article describes the RSA Algorithm and shows how to use it in 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:
  1. using System.Security.Cryptography; 
2. Now make a function for Encryption.
  1. static public byte[] Encryption(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)  
  2. {  
  3.  try  
  4.  {  
  5.  byte[] encryptedData;  
  6.  using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())  
  7.    {  
  8.     RSA.ImportParameters(RSAKey);  
  9.            encryptedData = RSA.Encrypt(Data, DoOAEPPadding);  
  10.    }   return encryptedData;  
  11.  }  
  12.  catch (CryptographicException e)  
  13.  {  
  14.  Console.WriteLine(e.Message);  
  15.  return null;  
  16.  }  
  17. } 
3. Now make a function for Decryption
  1. static public byte[] Decryption(byte[]Data, RSAParameters RSAKey, bool DoOAEPPadding)  
  2. {  
  3.  try  
  4.  {  
  5.  byte[] decryptedData;  
  6.  using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())  
  7.     {  
  8.      RSA.ImportParameters(RSAKey);  
  9.      decryptedData = RSA.Decrypt(Data, DoOAEPPadding);  
  10.     }  
  11.  return decryptedData;  
  12.  }  
  13.  catch (CryptographicException e)  
  14.  {  
  15.  Console.WriteLine(e.ToString());  
  16.  return null;  
  17.  }          
  18. }
4. Now make some variables into the class that are:
  1. UnicodeEncoding ByteConverter = new UnicodeEncoding();  
  2. RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();  
  3. byte[] plaintext;  
  4. byte[] encryptedtext;
5. Now handle the Click Event for the Encrypt Button with the following code:
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     plaintext = ByteConverter.GetBytes(txtplain.Text);  
  4.     encryptedtext = Encryption(plaintext, RSA.ExportParameters(false), false);  
  5.     txtencrypt.Text = ByteConverter.GetString(encryptedtext);  
  6. }
6. Now handle the Click Event for the Decrypt Button with the following code:
  1. private void button2_Click(object sender, EventArgs e)  
  2. {  
  3.     byte[] decryptedtex = Decryption(encryptedtext,   
  4.     RSA.ExportParameters(true), false);  
  5.     txtdecrypt.Text = ByteConverter.GetString(decryptedtex);  
  6. }
Results
 
Plain Text
 
plaintext result.PNG
 
Encrypted Text
 
EncryptedText.PNG
 
Decrypted Text
 
Decrypted Result.PNG