I am using RSA to encrypt and decrypt small notepad file with 1 or 2 words. After processing file result has 3 question marks on the begging of result.
For example, if i encrypt and then decrypt notepad file with word "Hello" in it, result would be "???Hello". Where did that 3 question marks come from?
This is the code:
public partial class Form1 : Form
{
private RSAParameters publicKey;
private RSAParameters privateKey;
public string result;
public Form1()
{
InitializeComponent();
var rsa = new RSACryptoServiceProvider();
this.publicKey = rsa.ExportParameters(false);
this.privateKey = rsa.ExportParameters(true);
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
textBox1.Text = openFileDialog1.FileName;
}
private void button2_Click(object sender, EventArgs e)
{
FileStream fileStream = new FileStream(textBox1.Text, FileMode.Open);
byte[] buffer = new byte[fileStream.Length];
int len = (int)fileStream.Length;
fileStream.Read(buffer, 0, len);
var rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(publicKey);
var encrypted = rsa.Encrypt(buffer, false);
result = Convert.ToBase64String(encrypted);
MessageBox.Show(result);
}
private void button3_Click(object sender, EventArgs e)
{
var rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(privateKey);
byte[] toDecode = Convert.FromBase64String(result);
var decrypted = rsa.Decrypt(toDecode, false);
string msg = Encoding.ASCII.GetString(decrypted);
MessageBox.Show(msg);
}
}