C# 数字签名“未指定的错误”
源代码如下:
using System;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SG
{
public partial class Form1 : Form
{
string str_DataToSign;
string str_SignedData;
byte[] decryptedData;
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
string str_Private_Key;
string str_Public_Key;
//对数据签名
public static string HashAndSign(string str_DataToSign, string str_Private_Key)
{
ASCIIEncoding ByteConverter = new ASCIIEncoding();
byte[] DataToSign = ByteConverter.GetBytes(str_DataToSign);
try
{
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
RSAalg.ImportCspBlob(Convert.FromBase64String(str_Private_Key));
byte[] signedData = RSAalg.SignData(DataToSign, new SHA1CryptoServiceProvider());
string str_SignedData = Convert.ToBase64String(signedData);
return str_SignedData;
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
//验证签名
public static bool VerifySignedHash(string str_DataToVerify, string str_SignedData, string str_Public_Key)
{
byte[] SignedData = Convert.FromBase64String(str_SignedData);
ASCIIEncoding ByteConverter = new ASCIIEncoding();
byte[] DataToVerify = ByteConverter.GetBytes(str_DataToVerify);
try
{
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
RSAalg.ImportCspBlob(Convert.FromBase64String(str_Public_Key));
return RSAalg.VerifyData(DataToVerify, new SHA1CryptoServiceProvider(), SignedData);
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return false;
}
}
public Form1()
{
InitializeComponent();
this.Text = "用RSA实现数字签名";
richTextBox1.ReadOnly = true;
richTextBox2.ReadOnly = true;
richTextBox3.ReadOnly = true;
richTextBox4.ReadOnly = true;
richTextBox5.ReadOnly = true;
richTextBox6.ReadOnly = true;
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
str_Private_Key = Convert.ToBase64String(RSAalg.ExportCspBlob(true));
richTextBox1.Text = str_Private_Key;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void richTextBox4_TextChanged(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
str_Public_Key = Convert.ToBase64String(RSAalg.ExportCspBlob(false));
richTextBox4.Text = str_Public_Key;
}
private void button2_Click(object sender, EventArgs e)
{
str_SignedData = HashAndSign(str_DataToSign, str_Private_Key);
richTextBox3.Text = str_SignedData;
}
private void richTextBox2_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
try
{
byte[] SigedToEcoding = Encoding.UTF8.GetBytes(str_SignedData);
byte[] encryptedData = RSAalg.Encrypt(SigedToEcoding, false);
richTextBox2.Text = Encoding.UTF8.GetString(encryptedData);
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
}
private void richTextBox3_TextChanged(object sender, EventArgs e)
{
}
private void richTextBox7_TextChanged(object sender, EventArgs e)
{
str_DataToSign = richTextBox7.Text;
}
private void button5_Click(object sender, EventArgs e)
{
byte[] SigedToEcoding = Encoding.UTF8.GetBytes(str_SignedData);
decryptedData = RSAalg.Decrypt(SigedToEcoding, false);
richTextBox5.Text = Encoding.UTF8.GetString(decryptedData);
}
private void button6_Click(object sender, EventArgs e)
{
if (VerifySignedHash(str_DataToSign, str_SignedData, str_Public_Key) == true)
richTextBox6.Text = "验证成功!" + "\n" + Encoding.UTF8.GetString(decryptedData);
}
private void richTextBox6_TextChanged(object sender, EventArgs e)
{
}
private void richTextBox5_TextChanged(object sender, EventArgs e)
{
}
}
}
错误的是红色部分:“未指定的错误”
请大家帮我啊。。。