asp.net digital signature instance code

  • 2020-05-17 05:10:54
  • OfStack

If A sends a message to B, A will encrypt the message with the password of A, and then send the encrypted string to B, and then B will decrypt the encrypted string with the password of B, and then determine whether the decrypted string matches the original text sent by A. The key problem is,
A is different from the password of B, which is the essence of digital signature. The password of A is the private key, and the password of B is the public key
Specific steps:
Generate a private key and a public key, and A will use the private key to encrypt, because A's private key is only available to A, so the encrypted string will be A's signature string, and then A will send B the signature string and the original text.
After obtaining the encrypted string, B USES the public key to decrypt it, and then determines whether the decrypted string and the original text are 1, which indicates that it is signed by A, otherwise it is not signed by A
If you are still a little confused, then read the following examples, before looking at the above text, probably understand
For example, the private key is S1, and the public key is G1 A
S1+123 enciphers a string that generates an A signature: AXXXX
A sends AXXXX and 1231 to B, and B USES G1 to decrypt AXXXX and determine whether the decrypted string is equal to 123
I believe you all understand the principle... Just get the idea. Hehe... Not to mention the rest, go to the code... Code is learning from others. It has been tested.
aspx code:
 
<form id="form1" runat="server"> 
 Randomly generated key: <asp:Button ID="btncreateMY" runat="server" Text=" Randomly generated key " OnClick="btncreateMY_Click" /><br /> 
 Public key: <asp:TextBox ID="tbxcreateMY_publicKey" runat="server" TextMode="MultiLine" Height="59px" ReadOnly="True" Width="711px"></asp:TextBox><br /> 
 The private key: <asp:TextBox ID="tbxcreateMY_key" runat="server" TextMode="MultiLine" Height="59px" ReadOnly="True" Width="710px"></asp:TextBox><br /><hr /> 
<br /> 
 Generate signature: <br /> 
 The original:   
<asp:TextBox ID="tbxContent" runat="server" TextMode="MultiLine" Height="59px" Width="711px"></asp:TextBox> <br /> 
 The private key:  
<asp:TextBox ID="tbxKey" runat="server" TextMode="MultiLine" Height="59px" Width="711px"></asp:TextBox><br /> 
 Signature:  
<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=" To generate the signature " /> 
<br /> 
<br /><hr /> 
<br /> 
 Verify signature: <br /> 
 The original: <asp:TextBox ID="tbxContentYZ" runat="server" TextMode="MultiLine" Height="59px" Width="711px"></asp:TextBox><br /> 
 Public key: <asp:TextBox ID="tbxPublickeyYZ" runat="server" TextMode="MultiLine" Height="59px" Width="711px"></asp:TextBox><br /> 
 Signature: <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=" Verify the signature " /> 
</form> 

CS code
 
/// <summary> 
///  To generate the signature  
/// </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); 
// A digital signature  
tbxSign.Text = BitConverter.ToString(objdsa.SignData(source)); 
} 
/// <summary> 
///  Randomly generated key  
/// </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> 
///  Verify the signature  
/// </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()); 
} 

Related articles: