数字签名里验证签名的函数出问题了,求帮助
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//生成公私密钥对,并将其保存在密钥容器中,导出公钥
public static string GenKey_SaveInContainer(string ContainerName)
{
FileStream fs = new FileStream("e:\\Public_Key.txt", FileMode.Open, FileAccess.ReadWrite);
CspParameters cp = new CspParameters();
cp.KeyContainerName = ContainerName;
cp.KeyNumber = (int)KeyNumber.Signature; //"1" = Exchanger, 为交换密钥
cp.Flags = CspProviderFlags.UseMachineKeyStore;
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cp);
string str_Private_Key = Convert.ToBase64String(RSAalg.ExportCspBlob(true)); //true 私钥
string str_Public_Key = Convert.ToBase64String(RSAalg.ExportCspBlob(false));
StreamWriter sw = new StreamWriter(fs);
fs.SetLength(0);
sw.Write(str_Public_Key);
sw.Close();
fs.Close();
return str_Private_Key;
}
// 载入待签名的文件
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
if (file.ShowDialog() == DialogResult.OK)
{
textBox1.Text = file.FileName;
}
}
// 载入公钥
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
if (file.ShowDialog() == DialogResult.OK)
{
textBox2.Text = file.FileName;
}
}
// 载入数字签名
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
if (file.ShowDialog() == DialogResult.OK)
{
textBox3.Text = file.FileName;
}
}
// 生成数字签名
private void button4_Click(object sender, EventArgs e)
{
string ContainerName = "信安课设";
string privatekey = GenKey_SaveInContainer(ContainerName);
textBox4.Text = privatekey;
richTextBox1.Text = HashAndSign(textBox1.Text, ContainerName,privatekey);
}
//数字签名
private string HashAndSign(string path, string ContainerName, string privatekey)
{
ASCIIEncoding ByteConverter = new ASCIIEncoding();
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
CspParameters cp = new CspParameters();
cp.KeyContainerName = ContainerName;
cp.KeyNumber = (int)KeyNumber.Signature; //"1" = Exchanger, 为交换密钥
cp.Flags = CspProviderFlags.UseMachineKeyStore;
byte[] DataToSign = new byte[file.Length];
file.Read(DataToSign, 0, (int)file.Length); //将文档流读取到Buffer中
file.Close();
//byte[] DataToSign = ByteConverter.GetBytes(str_DataToSign); //字符串转字节流
try
{
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cp);
RSAalg.ImportCspBlob(Convert.FromBase64String(privatekey)); //导入RSA密钥
byte[] signedData = RSAalg.SignData(DataToSign, typeof(MD5)); //Hash 并签名
string str_SignedData = Convert.ToBase64String(signedData);
return str_SignedData;
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
// 导出数字签名
private void button5_Click(object sender, EventArgs e)
{
string Signed = richTextBox1.Text;
string path = "e:\\digital signature.txt";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
fs.SetLength(0);
sw.Write(Signed);
sw.Close();
fs.Close();
}
// 验证数字签名
private void button6_Click(object sender, EventArgs e)
{
try
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] DataToVerify = new byte[fs.Length];
fs.Read(DataToVerify, 0, (int)fs.Length);
fs.Close();
//string data = Encoding.ASCII.GetString(DataToVerify);
//DataToVerify = Encoding.UTF8.GetBytes(data);
FileStream fs1 = new FileStream(textBox3.Text, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] SignedData = new byte[fs1.Length];
fs1.Read(SignedData, 0, (int)fs1.Length);
fs1.Close();
//string signed = Encoding.ASCII.GetString(SignedData);
//SignedData = Encoding.UTF8.GetBytes(signed);
FileStream fs2 = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] Public_Key = new byte[fs2.Length];
fs2.Read(Public_Key, 0, (int)fs2.Length);
fs2.Close();
//richTextBox1.Text = Encoding.ASCII.GetString(Public_Key);
CspParameters cp = new CspParameters();
cp.KeyContainerName = "信安课设";
cp.KeyNumber = (int)KeyNumber.Signature; //"1" = Exchanger, 为交换密钥
cp.Flags = CspProviderFlags.UseMachineKeyStore;
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cp);
//RSAParameters Key = RSAalg.ExportParameters(true);
byte[] publickey = RSAalg.ExportCspBlob(false);
//string public_key = Convert.ToBase64String(Public_Key);
//string public_key = Encoding.ASCII.GetString(Public_Key);
string public_key = Convert.ToBase64String(publickey);
textBox4.Text = public_key;
RSAalg.ImportCspBlob(Convert.FromBase64String(public_key));
bool result = RSAalg.VerifyData(DataToVerify, typeof(MD5), SignedData);
//new RSACryptoServiceProvider().VerifyHash();
if (result)
{
richTextBox2.Text = "文件签名正确。";
}
else
{
richTextBox2.Text = "文件签名错误。";
}
}
catch (Exception x)
{
richTextBox2.Text = x.Message;
}
}
// “退出” 程序
private void button7_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("确定退出本系统?未保存的数据将会丢失!", "确认退出系统", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (result == DialogResult.OK)
{
Application.Exit();
}
}
}
}
补充:.NET技术 , C#