我正在使用 RSA 加密和解密 1 或 2 个单词的小记事本文件。处理后的文件结果请求结果上有3个问号。
例如,如果我加密然后解密带有单词“Hello”的记事本文件,结果将是“???Hello”。这三个问号是从哪里来的?
这是代码:
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);
}
}