asp.net如何实现数字签名(DSACryptoServiceProvider)
数字签名简单的说过程如下:
发送者A向接受者B发送数据,A首先将数据进行散列函数运算,生成信息摘要,然后用私钥进行加密,生成签名信息,A将要发送的信息与签名信息一起发送给B,B接收到后,也将数据进行散列运行,并生成信息摘要,然后用公钥与签名进行验证核对信息是否被撰改。
asp.net实现数字签名实例代码:
需引入命名空间:using System.Security.Cryptography;
以下为引用的内容: /// <summary> /// <summary> byte[] fileHashValue = new SHA1CryptoServiceProvider().ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(tbxContentYZ.Text)); objdsa.FromXmlString(tbxPublickeyYZ.Text); bool ret = objdsa.VerifySignature(fileHashValue, SignedHash); } |
html页面代码:
以下为引用的内容: <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> |
http://www.cn-web.com/shtml/article/net/quer/2009/07/28/921.shtml
转自:http://www.cnblogs.com/skyaspnet/archive/2010/10/06/1844610.html