嗨,我一直在寻找整个互联网,但我找不到解决我的签名和验证问题的方法。首先,我想确保在理解发生的事情时我走在正确的轨道上,所以请纠正我。签名的第一件事是服务器首先创建两个密钥,一个是公共的,一个是私有的。然后服务器将数据文本、公钥、然后散列和加密(使用它的私钥)数据文本放在文件上。服务器将文件发送给客户端,客户端开始通过获取文件中的公钥来验证数据,并使用它来解密加密的数据。最后,客户端对文本数据使用哈希算法(与服务器相同),并将其与解密的数据进行比较。
如果这没问题,那么我不明白为什么我的代码不起作用:
服务器:
string name = textBox1.Text;
string GUID = textBox2.Text;
string startDate = textBox3.Text;
string EndDate = textBox4.Text;
string macAddress = GetMacAddress();
FileStream fs = File.Create(@"cert.txt");
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
UnicodeEncoding ByteConverter = new UnicodeEncoding();
StreamWriter write = new StreamWriter(fs);
write.Write(name + "\r\n");
write.Write(GUID + "\r\n");
write.Write(startDate + "\r\n");
write.Write(EndDate + "\r\n");
write.Write(macAddress + "\r\n");
string pkey = RSA.ToXmlString(false);
write.Write(pkey + "\r\n");
SHA1Managed Sha = new SHA1Managed();
string info = name + GUID + startDate + EndDate + macAddress;
byte [] hashed = Sha.ComputeHash(Encoding.UTF8.GetBytes(info));
byte []signature = RSA.SignData(hashed,CryptoConfig.MapNameToOID("SHA1"));
write.Write(Convert.ToBase64String(signature));
textBox5.Text = Convert.ToBase64String(hashed);
write.Close();
fs.Close();
客户:
FileStream fsSource = new FileStream(@"cert.txt", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fsSource);
string name = reader.ReadLine();
string GUID = reader.ReadLine();
string startDate = reader.ReadLine();
string EndDate = reader.ReadLine();
string macAddress = reader.ReadLine();
string pkey = reader.ReadLine();
string signed = reader.ReadLine();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(pkey);
string info = name + GUID + startDate + EndDate + macAddress;
SHA1Managed Sha = new SHA1Managed();
byte[] checkinghash = Sha.ComputeHash(Encoding.UTF8.GetBytes(info));
if (RSA.VerifyHash(checkinghash, CryptoConfig.MapNameToOID("SHA1"), Encoding.UTF8.GetBytes(signed)))
{
Console.WriteLine("verfied");
}
else
{
Console.WriteLine("denied");
}
Console.WriteLine();
//Console.WriteLine(signed);
Console.ReadKey();
这总是会产生拒绝,因为我不确定我是否丢失了公钥或者这是错误的方法。