RSA Encryption/Decryption in C#

RSA (Rivest-Shamir-Adleman) is a widely used public-key cryptosystem for secure data transmission. It involves two keys: a public key for encryption and a private key for decryption. Here's a step-by-step guide to implementing RSA encryption in C#.

Add Necessary References

Include the System.Security.Cryptography namespace in your C# file

using System.Security.Cryptography;
Create an RSACryptoServiceProvider Object

Instantiate an RSACryptoServiceProvider object to handle RSA operations:

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
Generate Key Pair (Optional)

If you don't have existing keys, generate a public/private key pair:

rsa.GenerateKeys(1024); // Adjust key size as needed
Retrieve Public and Private Keys (Optional)

If you have existing keys, import them:

// Public key RSAParameters publicKey = rsa.ExportParameters(false);
// Private key RSAParameters privateKey = rsa.ExportParameters(true);
Encrypt Data with Public Key
byte[] plaintext = Encoding.UTF8.GetBytes("This is the secret message."); byte[] encryptedData = rsa.Encrypt(plaintext, false); // Use OAEP padding
Decrypt Data with Private Key
byte[] decryptedData = rsa.Decrypt(encryptedData, true); string plaintextMessage = Encoding.UTF8.GetString(decryptedData);
Key Points:
  1. Adjust key size based on security needs.
  2. Use OAEP padding for enhanced security.
  3. Consider using a key container for key persistence.
  4. Handle exceptions appropriately.
  5. Explore additional features like signing and verifying data.
Full Source | C#
using System; using System.Text; using System.Security.Cryptography; public class RSAEncryptionExample { public static void Main() { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { // Generate a new key pair RSAParameters publicKey = rsa.ExportParameters(false); RSAParameters privateKey = rsa.ExportParameters(true); string message = "This is a confidential message."; byte[] encryptedData = EncryptData(message, publicKey); string decryptedMessage = DecryptData(encryptedData, privateKey); Console.WriteLine("Encrypted Data: " + Convert.ToBase64String(encryptedData)); Console.WriteLine("Decrypted Message: " + decryptedMessage); } } static byte[] EncryptData(string message, RSAParameters publicKey) { byte[] plaintext = Encoding.UTF8.GetBytes(message); using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.ImportParameters(publicKey); return rsa.Encrypt(plaintext, false); } } static string DecryptData(byte[] encryptedData, RSAParameters privateKey) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.ImportParameters(privateKey); byte[] decryptedData = rsa.Decrypt(encryptedData, false); return Encoding.UTF8.GetString(decryptedData); } } }
Output:
Encrypted Data: U2h+PNM1mjtUQwtBViExBP5BvXphtJsL3KLn53++A7r13PFlLGhGCbkLp643/Nr1E9DYF3+tSqfYk+qaT8Us9f+yrHcqKifMluoHPsuLUw4P8F+VSjgSsYNYHT1WUnz5oTAWuEoUMdOK5hqVfko9mtEmhDB/2XcSMXfo/SR8DKc= Decrypted Message: This is a confidential message.

Conclusion

RSA (Rivest-Shamir-Adleman) is a widely-used public-key cryptosystem that utilizes two keys, a public key for encryption and a private key for decryption. The security of RSA is based on the difficulty of factoring large composite numbers, providing a secure method for secure data transmission and digital signatures.