RSA encryption in C#
I want to make a simple RSA encryption algorithm embedded in .Net by means of. How I see it:
- Creating an RSA object.
- We get the public and private keys from it, and save them.
- We encrypt the message using the public key.
- The message encrypted in clause 3 can be decrypted using a private key.
I sketched a quick demo for this, in it in TextBox1 is the text that I want encrypt, by clicking on button1 encryption occurs and the result is placed in textBox2; in textBox3, respectively, we can put the encrypted message, and by clicking on button2 decrypt it and see the result in textBox4.
Source code (RSAEn (- De)crypt methods taken from the MSDN site):
public partial class Form1 : Form
{
RSAParameters privateKey;
RSAParameters publicKey;
public Form1()
{
InitializeComponent();
//Пункт 1
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Пункт 2
privateKey = RSA.ExportParameters(true);
publicKey = RSA.ExportParameters(false);
}
private void button1_Click(object sender, EventArgs e)
{
UnicodeEncoding byteConverter = new UnicodeEncoding();
byte[] input = byteConverter.GetBytes(textBox1.Text);
byte[] output;
//Пункт 3
output = RSAEncrypt(input, publicKey, false);
textBox2.Text = byteConverter.GetString(output);
}
private void button2_Click(object sender, EventArgs e)
{
UnicodeEncoding byteConverter = new UnicodeEncoding();
byte[] input = byteConverter.GetBytes(textBox3.Text);
byte[] output;
//Пункт 4
output = RSADecrypt(input, privateKey, false);
textBox4.Text = byteConverter.GetString(output);
}
static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Import the RSA Key information. This only needs
//toinclude the public key information.
RSA.ImportParameters(RSAKeyInfo);
//Encrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Import the RSA Key information. This needs
//to include the private key information.
RSA.ImportParameters(RSAKeyInfo);
//Decrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
}
It encrypts everything normally, but when decrypting it gives CryptographicException, " Additional information: The parameter was set incorrectly. " Googling the error did not give any results. What am I doing wrong?
1 answers
The error is located in byteConverter, the error occurs because the converter converts to a string and back changes the number of bytes.
To avoid this error, use better Base64String:
Convert.ToBase64String();
Convert.FromBase64String();
This guarantees the safety of the bytes.
Here is an example of a console application to test:
static void Main(string[] args)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Пункт 2
privateKey = RSA.ExportParameters(true);
publicKey = RSA.ExportParameters(false);
UnicodeEncoding byteConverter = new UnicodeEncoding();
string toEncrypt = "Hello, world";
Console.WriteLine($"To encode: {toEncrypt}");
byte[] encBytes = RSAEncrypt(byteConverter.GetBytes(toEncrypt), publicKey, false);
string encrypt = byteConverter.GetString(encBytes);
Console.WriteLine("Encrypt str: " + encrypt);
Console.WriteLine("Encrypt bytes: " + string.Join(", ", encBytes));
byte[] decBytes = RSADecrypt(encBytes, privateKey, false);
Console.WriteLine("Decrypt str: " + byteConverter.GetString(decBytes));
Console.WriteLine("Decrypt bytes: " + string.Join(", ", byteConverter.GetBytes(encrypt)));
Console.ReadKey();
}