Windows Forms: Encryption and Decryption using RSA in C#

This post shows you how to encrypt and decrypt string using RSA algorithm in c# .net windows forms application.

RSA is an asymmetric coding system developed by Ron Rivest, Adi Shamir and Leonard Adleman (its name is also the abbreviation of these three authors). It is widely used for encryption and electronic signature technology. It works by using a public key to share with everyone.

RSA operation is based on 4 main steps: key generation, key sharing, encryption and decryption.

This article will guide you in detail about algorithm c# rsa generate public and private key, then helps you encryption and decryption in c# with key.

Dragging TextBox, Label and Button from the Visual Studio toolbox into your winform designer, then you can design a simple UI that allows you to encrypt and decrypt a string using the RSA algorithm in c# code as shown below.

encryption and decryption in c# with key

Encryption and Decryption using RSA algorithm in C#

Through this c# cryptography tutorial, I will create an Encrypt method to encrypt your data using RSA algorithm.

byte[] Encrypt(byte[] data, RSAParameters RSAKey, bool fOAEP)
{
    byte[] encryptedData;
    using (RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider())
    {
        rSACryptoServiceProvider.ImportParameters(RSAKey);
        encryptedData = rSACryptoServiceProvider.Encrypt(data, fOAEP);
    }
    return encryptedData;
}

Similarly, Create the Decrypt method to decrypt your data using RSA algorithm.

byte[] Decrypt(byte[] data, RSAParameters RSAKey, bool fOAEP)
{
    byte[] decryptedData;
    using (RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider())
    {
        rSACryptoServiceProvider.ImportParameters(RSAKey);
        decryptedData = rSACryptoServiceProvider.Decrypt(data, fOAEP);
    }
    return decryptedData;
}

C# RSA encrypt with public key

Next, Declare unicodeEncodingrSACryptoServiceProviderdata and encryptData variables as shown below.

UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider();
byte[] data;
byte[] encryptData;

Finally, Use RSA to generate public and private key by calling the ExportParameters method.

rSACryptoServiceProvider.ExportParameters(false)

RSA algorithm performs encryption and decryption in c# with key.

Adding a click event handler to the Encrypt button allows you to encrypt data using RSA algorithm.

private void btnEncrypt_Click(object sender, EventArgs e)
{
    data = unicodeEncoding.GetBytes(txtInput.Text);
    encryptData = Encrypt(data, rSACryptoServiceProvider.ExportParameters(false), false);
    txtEncrypt.Text = unicodeEncoding.GetString(encryptData);
}

Adding a click event handler to the Decrypt button allowing you to decrypt data.

private void btnDecrypt_Click(object sender, EventArgs e)
{
    byte[] data = Decrypt(encryptData, rSACryptoServiceProvider.ExportParameters(true), false);
    txtDecrypt.Text = unicodeEncoding.GetString(data);
}

You can also create a web api or web services to support rsa encryption online.