How to Encrypt and Decrypt data using RSA in C#

By FoxLearn 7/26/2024 1:59:17 AM   19.35K
Encrypting and decrypting strings using the RSA algorithm in a C# Windows Forms Application involves generating RSA key pairs, encrypting the data with the public key, and decrypting it with the private key.

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.

How to encrypt and decrypt string using RSA algorithm in C#

To encrypt and decrypt strings using the RSA algorithm in C#, you can use the RSACryptoServiceProvider class provided by the .NET framework. Here's a basic example of how to do it:

Drag and drop the TextBox, Label and Button controls 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.

// c# encrypt rsa
byte[] Encrypt(byte[] data, RSAParameters RSAKey, bool fOAEP)
{
    byte[] encryptedData;
    // Create an instance of RSA
    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.

// c# decrypt rsa
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.

// c# rsa encrypt
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.

// c# rsa decrypt
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.

This example demonstrates how to generate a public and private key pair, encrypt a string using the public key, and decrypt the encrypted string using the private key.