C# 数字签名“未指定的错误”

BYD123 2008-06-04 09:30:32
源代码如下:
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)
{

}
}
}
错误的是红色部分:“未指定的错误”

请大家帮我啊。。。

...全文
157 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenyhong 2008-06-07
  • 打赏
  • 举报
回复
byte[] encryptedData = RSAalg.Encrypt(SigedToEcoding, false);
改为
byte[] encryptedData = RSAalg.Encrypt(SigedToEcoding, true);
xp下的填充和2000下不同。
BYD123 2008-06-04
  • 打赏
  • 举报
回复
具体是:CryptographicException
本人是初学者,望高手不吝赐教!感激不尽。

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧