时间:2021-07-01 10:21:17 帮助过:6人阅读
<form id="form1" runat="server">
随机生成密钥:<asp:Button ID="btncreateMY" runat="server" Text="随机生成密钥" OnClick="btncreateMY_Click" /><br />
公钥:<asp:TextBox ID="tbxcreateMY_publicKey" runat="server" TextMode="MultiLine" Height="59px" ReadOnly="True" Width="711px"></asp:TextBox><br />
私钥:<asp:TextBox ID="tbxcreateMY_key" runat="server" TextMode="MultiLine" Height="59px" ReadOnly="True" Width="710px"></asp:TextBox><br /><hr />
<br />
生成签名:<br />
原文:
<asp:TextBox ID="tbxContent" runat="server" TextMode="MultiLine" Height="59px" Width="711px"></asp:TextBox> <br />
私钥:
<asp:TextBox ID="tbxKey" runat="server" TextMode="MultiLine" Height="59px" Width="711px"></asp:TextBox><br />
签名:
<asp:TextBox ID="tbxSign" runat="server" TextMode="MultiLine" Height="59px" ReadOnly="True" Width="711px"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="生成签名" />
<br />
<br /><hr />
<br />
验证签名:<br />
原文:<asp:TextBox ID="tbxContentYZ" runat="server" TextMode="MultiLine" Height="59px" Width="711px"></asp:TextBox><br />
公钥:<asp:TextBox ID="tbxPublickeyYZ" runat="server" TextMode="MultiLine" Height="59px" Width="711px"></asp:TextBox><br />
签名:<asp:TextBox ID="tbxSignYZ" runat="server" TextMode="MultiLine" Height="59px" Width="711px"></asp:TextBox>
<br />
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="验证签名" />
</form>
CS代码
代码如下:
/// <summary>
/// 生成签名
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
DSACryptoServiceProvider objdsa = new DSACryptoServiceProvider();
objdsa.FromXmlString(tbxKey.Text);
byte[] source = System.Text.UTF8Encoding.UTF8.GetBytes(tbxContent.Text);
//数字签名
tbxSign.Text = BitConverter.ToString(objdsa.SignData(source));
}
/// <summary>
/// 随机生成密钥
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btncreateMY_Click(object sender, EventArgs e)
{
DSACryptoServiceProvider objdsa = new DSACryptoServiceProvider();
tbxcreateMY_publicKey.Text = objdsa.ToXmlString(false);
tbxcreateMY_key.Text = objdsa.ToXmlString(true);
}
/// <summary>
/// 验证签名
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button3_Click(object sender, EventArgs e)
{
DSACryptoServiceProvider objdsa = new DSACryptoServiceProvider();
byte[] fileHashValue = new SHA1CryptoServiceProvider().ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(tbxContentYZ.Text));
string[] strSplit = tbxSignYZ.Text.Split('-');
byte[] SignedHash = new byte[strSplit.Length];
for (int i = 0; i < strSplit.Length; i++)
SignedHash[i] = byte.Parse(strSplit[i], System.Globalization.NumberStyles.AllowHexSpecifier);
objdsa.FromXmlString(tbxPublickeyYZ.Text);
bool ret = objdsa.VerifySignature(fileHashValue, SignedHash);
Response.Write(ret.ToString());
// Qcd.Core.Web.Messages.ShowDialog(ret.ToString());
}